68

This is a very basic question, but I'm struggling a bit and would like to make sure I understand properly.

After a container is started from an image and some changes done to files within (i.e.: some data stored in the DB of a WebApp running on the container), what's the appropriate way to continue working with the same data between container stop and restart?

Is my understanding correct that once the container is stopped/finished (i.e.: exit after an interactive session), then that container is gone together with all file changes? So if I want to keep some file changes I have to commit the state of the container into a new image / new version of the image?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
Julian Cerruti
  • 1,918
  • 2
  • 16
  • 16

2 Answers2

92

Is my understanding correct that once the container is stopped/finished (i.e.: exit after an interactive session), then that container is gone together with all file changes?

No, a container persists after it exits, unless you started it using the --rm argument to docker run. Consider this:

$ docker run -it busybox sh
/ # date > example_file
/ # exit

Since we exited our shell, the container is no longer running:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES

But if we had the -a option, we can see it:

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS                   NAMES
79aee3e2774e        busybox:latest      "sh"                About a minute ago   Exited (0) 54 seconds ago                           loving_fermat       

And we can restart it and re-attach to it:

$ docker start 79aee3e2774e
$ docker attach  79aee3e2774e
<i press RETURN>
/ #

And the file we created earlier is still there:

/ # cat example_file
Wed Feb 18 01:51:38 UTC 2015
/ #

You can use the docker commit command to save the contents of the container into a new image, which you can then use to start new containers, or share with someone else, etc. Note, however, that if you find yourself regularly using docker commit you are probably doing yourself a disservice. In general, it is more manageable to consider containers to be read-only and generate new images using a Dockerfile and docker build.

Using this model, data is typically kept external to the container, either through host volume mounts or using a data-only container.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • 3
    *facepalm* I was using --rm (!!!) - too much google-paste error. I knew it was a stupid question. – Julian Cerruti Feb 19 '15 at 02:23
  • 4
    In any case, thank you very much for the thorough explanation. The entire model makes a lot more sense to me now. For my case, I am moving to using as much data as possible outside the container and mounted through host volume mounts. Only occasionally I have to modify the container to install something. With this clarification I can now simply let the container be and commit it to a new image only once in a while when it merits being shared with other devs. Thanks again, great answer! – Julian Cerruti Feb 19 '15 at 02:25
  • 1
    BTW, I loved the ironic typo at the beginning: "... a container persists after it exists ...". Sounds like a die-hard container. – Julian Cerruti Feb 19 '15 at 02:26
  • 3
    what if you kill the container `79aee3e2774e`, when you start a new container using the same image, would the file still exists? – James Lin Jul 01 '16 at 03:14
  • after exit, how docker decide what to persist and what not? Example: After exit new docker config is still in docker but if you install vim , this change is declined. So How docker know what to persist? @Larsks – grep Jul 16 '19 at 20:41
20

You can see finished containers with docker ps -a

You can save a finished container, with the filesystem changes, into an image using docker commit container_name new_image_name

You can also extract data files from the finished container with: docker cp containerID:/path/to/find/files /path/to/put/copy

Note that you can also "plan ahead" and avoid trapping data you'll need permanently within a temporary container by having the container mount a directory from the host, e.g.

 docker run -v /dir/on/host:/dir/on/container -it ubuntu:14.04 
Paul
  • 26,170
  • 12
  • 85
  • 119
  • after exit, how docker decide what to persist and what not? Example: After exit new nginix config is still in docker but if you install vim via the terminal , this change is declined. So How docker know what to persist? – grep Jul 16 '19 at 21:04
  • I'm not entirely clear on your question: Docker doesn't "choose" whether or not to persist anything. Modifications to your container filesystem persist until you remove the container. Modifications in volumes persist until you remove the volume. – larsks Jul 19 '19 at 16:28