容器退出时我丢失了数据

在Docker中,当容器退出时数据丢失是个常见问题。实际上每次运行都会创建新的容器。用户可以通过提交容器更改、使用Dockerfile创建映像、重启已存在的容器或利用Docker卷来持久化数据。Docker官方文档提供了详细教程。
摘要由CSDN通过智能技术生成

本文翻译自:I lose my data when the container exits

Despite Docker's Interactive tutorial and faq I lose my data when the container exits. 尽管有Docker的Interactive教程常见问题解答,但当容器退出时,我仍然丢失了数据。

I have installed Docker as described here: http://docs.docker.io/en/latest/installation/ubuntulinux without any problem on ubuntu 13.04. 我已经按照以下说明安装了Docker: http : //docs.docker.io/en/latest/installation/ubuntulinux在ubuntu 13.04上没有任何问题。

But it loses all data when exits. 但是退出时它将丢失所有数据。

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 

I also tested it with interactive sessions with the same result. 我还通过相同结果的交互式会话对其进行了测试。 Did I forget something? 我忘记了什么吗?

EDIT: IMPORTANT FOR NEW DOCKER USERS 编辑:对于新DOCKER用户的重要

As @mohammed-noureldin and others said, actually this is NOT a container exiting . 正如@ mohammed-noureldin和其他人所说,实际上这不是 退出容器 Every time it just creates a new container. 每次它只是创建一个新的容器。


#1楼

参考:https://stackoom.com/question/1KAxY/容器退出时我丢失了数据


#2楼

You need to commit the changes you make to the container and then run it. 您需要提交对容器所做的更改,然后运行它。 Try this: 尝试这个:

sudo docker pull ubuntu

sudo docker run ubuntu apt-get install -y ping

Then get the container id using this command: 然后使用以下命令获取容器ID:

sudo docker ps -l

Commit changes to the container: 提交对容器的更改:

sudo docker commit <container_id> iman/ping 

Then run the container: 然后运行容器:

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

This should work. 这应该工作。


#3楼

In addition to Unferth's answer , it is recommended to create a Dockerfile . 除了Unferth的答案外 ,建议创建一个Dockerfile

In an empty directory, create a file called "Dockerfile" with the following contents. 在一个空目录中, 创建一个名为“ Dockerfile”的文件,其中包含以下内容。

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

Create an image using the Dockerfile . 使用Dockerfile创建映像 Let's use a tag so we don't need to remember the hexadecimal image number. 让我们使用标签,这样我们就不必记住十六进制的图像号了。

$ docker build -t iman/ping .

And then run the image in a container. 然后在容器中运行该图像

$ docker run iman/ping stackoverflow.com

#4楼

When you use docker run to start a container, it actually creates a new container based on the image you have specified. 当您使用docker run启动容器时,它实际上根据您指定的映像创建一个新容器

Besides the other useful answers here, note that you can restart an existing container after it exited and your changes are still there. 除了此处的其他有用答案外,请注意,您可以在现有容器退出后重新启动,并且所做的更改仍然存在。

docker start f357e2faab77 # restart it in the background
docker attach f357e2faab77 # reattach the terminal & stdin

#5楼

You might want to look at docker volumes if you you want to persist the data in your container. 如果要将数据持久保存在容器中,则可能需要查看docker卷。 Visit https://docs.docker.com/engine/tutorials/dockervolumes/ . 访问https://docs.docker.com/engine/tutorials/dockervolumes/ The docker documentation is a very good place to start Docker文档是一个很好的起点


#6楼

There are following ways to persist container data: 有以下几种方法来保存容器数据:

  1. Docker volumes Docker卷

  2. Docker commit Docker提交

    a) create container from ubuntu image and run a bash terminal. a)从ubuntu映像创建容器并运行bash终端。

      $ docker run -i -t ubuntu:14.04 /bin/bash 

    b) Inside the terminal install curl b)在终端内安装卷发

      # apt-get update # apt-get install curl 

    c) Exit the container terminal c)退出集装箱码头

      # exit 

    d) Take a note of your container id by executing following command : d)通过执行以下命令记录您的容器ID:

      $ docker ps -a 

    e) save container as new image e)将容器另存为新映像

      $ docker commit <container_id> new_image_name:tag_name(optional) 

    f) verify that you can see your new image with curl installed. f)确认您可以看到安装了curl的新图像。

      $ docker images $ docker run -it new_image_name:tag_name bash # which curl /usr/bin/curl 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值