尽管Docker的交互式教程和常见问题,我丢失了我的数据时,容器退出。

我已经安装Docker如下所述:http://docs.docker.io/en/latest/installation/ubuntulinux 在ubuntu 13.04上没有任何问题。

但退出时将丢失所有数据。

iman@test:~$ sudo docker version
Client version: 0.6.4 
Go version (client): go1.1.2 
Git commit (client): 2f74b1c 
Server version: 0.6.4 
Git commit (server): 2f74b1c 
Go version (server): go1.1.2 
Last stable version: 0.6.4 


iman@test:~$ sudo docker run ubuntu ping
2013/10/25 08:05:47 Unable to locate ping 
iman@test:~$ sudo docker run ubuntu apt-get install ping
Reading package lists... 
Building dependency tree... 
The following NEW packages will be installed: 
  iputils-ping 
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. 
Need to get 56.1 kB of archives. 
After this operation, 143 kB of additional disk space will be used. 
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main iputils-ping amd64 3:20101006-1ubuntu1 [56.1 kB] 
debconf: delaying package configuration, since apt-utils is not installed 
Fetched 56.1 kB in 0s (195 kB/s) 
Selecting previously unselected package iputils-ping. 
(Reading database ... 7545 files and directories currently installed.) 
Unpacking iputils-ping (from .../iputils-ping_3%3a20101006-1ubuntu1_amd64.deb) ... 
Setting up iputils-ping (3:20101006-1ubuntu1) ... 
iman@test:~$ sudo docker run ubuntu ping
2013/10/25 08:06:11 Unable to locate ping 
iman@test:~$ sudo docker run ubuntu touch /home/test
iman@test:~$ sudo docker run ubuntu ls /home/test
ls: cannot access /home/test: No such file or directory 

我还用互动会话测试了它,得到了同样的结果。我是不是忘了什么?

编辑:对docker新用户很重要

正如@ mohammad -noureldin和其他人所说,实际上这不是一个容器退出。每次它都会创建一个新容器。


当前回答

没有一个答案能解决这个设计选择的问题。我认为docker这样工作是为了防止这2个错误:

反复重启 局部的错误

其他回答

类似的问题(仅靠Dockerfile无法解决)将我带到了这个页面。

0阶段: 对于所有人来说,希望Dockerfile可以修复它:直到——dns和——dns-search将出现在Dockerfile支持中-没有办法将基于intranet的资源集成到。

stage 1: after building image using Dockerfile (by the way it's a serious glitch Dockerfile must be in the current folder), having an image to deploy what's intranet based, by running docker run script. example: docker run -d \ --dns=${DNSLOCAL} \ --dns=${DNSGLOBAL} \ --dns-search=intranet \ -t pack/bsp \ --name packbsp-cont \ bash -c " \ wget -r --no-parent http://intranet/intranet-content.tar.gz \ tar -xvf intranet-content.tar.gz \ sudo -u ${USERNAME} bash --norc"

阶段2: 应用docker运行脚本在守护进程模式提供本地DNS记录有能力下载和部署本地的东西。

重点:运行脚本应该以类似/usr/bin/sudo -u ${USERNAME} bash——norc结尾,以便在安装脚本结束后仍然保持容器运行。

不,不可能在完全自动化的交互模式下运行容器,因为它将保持在内部shall命令提示符中,直到按下CTRL-p CTRL-q。

不,如果交互bash不会在安装脚本的末尾执行,容器将在脚本执行完成后立即终止,丢失所有安装结果。

stage 3: container is still running in background but it's unclear whether container has ended installation procedure or not yet. using following block to determine execution procedure finishes: while ! docker container top ${CONTNAME} | grep "00[[:space:]]\{12\}bash \--norc" - do echo "." sleep 5 done the script will proceed further only after completed installation. and this is the right moment to call: commit, providing current container id as well as destination image name (it may be the same as on the build/run procedure but appended with the local installation purposes tag. example: docker commit containerID pack/bsp:toolchained. see this link on how to get proper containerID

阶段4:容器已经使用本地安装更新,并且已经提交到新分配的映像中(添加了目的标记的映像)。现在可以安全地停止容器运行了。例如:docker stop packbsp-cont

Stage5:任何带有本地安装的容器需要运行的时候,使用之前保存的映像启动它。 示例:docker run -d -t pack/bsp:toolchained

除了Unferth的答案,建议创建一个Dockerfile。

在一个空目录中,创建一个名为“Dockerfile”的文件,包含以下内容。

FROM ubuntu
RUN apt-get install ping
ENTRYPOINT ["ping"]

使用Dockerfile创建一个映像。让我们使用一个标签,这样我们就不需要记住十六进制的图像编号。

$ docker build -t iman/ping .

然后在容器中运行图像。

$ docker run iman/ping stackoverflow.com

如果您希望将数据持久化在容器中,您可能需要查看docker卷。访问https://docs.docker.com/engine/tutorials/dockervolumes/。docker文档是一个很好的开始

一个聪明的答案在这里如何继续从用户kgs退出的docker

docker start $(docker ps -a -q --filter "status=exited")
(or in this case just docker start $(docker ps -ql) 'cos you don't want to start all of them)

docker exec -it <container-id> /bin/bash

第二句话至关重要。所以exec被用来代替run,而且不是在映像上而是在容器id上。而且是在容器启动之后。

我的建议是管理docker,使用docker compose。是一个简单的方法来管理所有docker的容器为您的项目,你可以映射版本和链接不同的容器一起工作。

文档非常容易理解,比docker的文档更好。

Docker-Compose文档

Best