尽管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 volumes Docker commit a) create container from ubuntu image and run a bash terminal. $ docker run -i -t ubuntu:14.04 /bin/bash b) Inside the terminal install curl # apt-get update # apt-get install curl c) Exit the container terminal # exit d) Take a note of your container id by executing following command : $ docker ps -a e) save container as new image $ docker commit <container_id> new_image_name:tag_name(optional) f) verify that you can see your new image with curl installed. $ docker images $ docker run -it new_image_name:tag_name bash # which curl /usr/bin/curl

其他回答

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

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

Docker-Compose文档

Best

有以下几种方法持久化容器数据:

Docker volumes Docker commit a) create container from ubuntu image and run a bash terminal. $ docker run -i -t ubuntu:14.04 /bin/bash b) Inside the terminal install curl # apt-get update # apt-get install curl c) Exit the container terminal # exit d) Take a note of your container id by executing following command : $ docker ps -a e) save container as new image $ docker commit <container_id> new_image_name:tag_name(optional) f) verify that you can see your new image with curl installed. $ docker images $ docker run -it new_image_name:tag_name bash # which curl /usr/bin/curl

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

反复重启 局部的错误

您需要向容器提交所做的更改,然后运行它。试试这个:

sudo docker pull ubuntu

sudo docker run ubuntu apt-get install -y ping

然后使用下面的命令获取容器id:

sudo docker ps -l

向容器提交更改:

sudo docker commit <container_id> iman/ping 

然后运行容器:

sudo docker run iman/ping ping www.google.com

这应该有用。

上面的问题都有很好的答案。也许不需要另一个答案,但我仍然想用最简单的语言就这个话题发表我的个人观点。

下面是一些关于容器和图像的观点,有助于我们得出结论:

A docker image can be: created-from-a-given-container deleted used-to-create-any-number-of-containers A docker container can be: created-from-an-image started stopped restarted deleted used-to-create-any-number-of-images A docker run command does this: Downloads an image or uses a cached image Creates a new container out of it Starts the container When a Dockerfile is used to create an image: It is already well known that the image will eventually be used to run a docker container. After issuing docker build command, docker behind-the-scenes creates a running container with a base-file-system and follows steps inside the Dockerfile to configure that container as per the developers need. After the container is configured with specs of the Dockerfile, it will be committed as an image. The image gets ready to rock & roll!

结论:

正如我们所看到的,docker容器独立于docker镜像。

一个容器可以重新启动,前提是该容器的唯一ID[使用docker ps——all获取ID]。

任何操作,如创建新目录,创建文件,安装工具等,都可以在容器运行时在容器中完成。一旦容器停止,它将持久化所有更改。容器停止和重新启动就像重新启动计算机系统一样。

一个已经创建的容器总是可以重新启动,但是当我们发出docker run命令时,一个新的容器就会从一个映像中创建出来,因此它就像一个新的计算机系统。正如我们现在所理解的那样,在旧容器中所做的更改在这个新容器中不可用。

最后一点:

我想现在很明显为什么数据似乎丢失了,但它总是在那里。但在一个不同的(旧的)容器里。因此,请注意docker start和docker run命令的区别,不要混淆它们。