Hind Forsum
Hind Forsum

Reputation: 10527

Does docker stores all its files as "memory image", as part of image, not disk file?

I was trying to add some files inside a docker container like "touch". I found after I shutdown this container, and bring it up again, all my files are lost. Also, I'm using ubuntu image, after shutdown-restart the same image, all my software that has been installed by apt-get is gone! Just like running a new image. So how can I save any file that I created?

My question is, does docker "store" all its file systems like "/tmp" as memory file system, so nothing is actually saved to disk?

Thanks.

Upvotes: 0

Views: 754

Answers (2)

vishnu narayanan
vishnu narayanan

Reputation: 3953

When you start a container using docker run command,docker run ubuntu, docker starts a new container based on the image you specified. Any changes you make to the previous container will not be available, as this is a new instance spawned from the base image.

There a multiple ways to persist your data/changes to your container.

  1. Use Volumes. Data volumes are designed to persist data, independent of the container’s lifecycle. You could attach a data volume or mount a host directory as a volume.
  2. Use Docker commit to create a new image with your changes and start future containers based on that image.

    docker commit <container-id> new_image_name docker run new_image_name

  3. Use docker ps -a to list all the containers. It will list all containers including the ones that have exited. Find the docker id of the container that you were working on and start it using docker start <id>.

    docker ps -a #find the id docker start 1aef34df8ddf #start the container in background

References

  1. Docker Volumes
  2. Docker Commit

Upvotes: 0

homelessDevOps
homelessDevOps

Reputation: 20726

This is normal behavoir for docker. You have to define a volume to save your data, those volumes will exist even if you shutdown your container.

For example with a simple apache webserver:

$ docker run -dit --name my-apache-app -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4

This will mount your "current" director to /usr/local/apache2/htdocs at the container, so those files wil be available there.

A other approach is to use named volumes, those ones are not linked to a directory on your disk. Please refer to the docs: Docker Manage - Data

Upvotes: 1

Related Questions