Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happen to docker volume on deletion of container

Suppose i have these volumes in my Mysql Docker container

VOLUME ["/etc/mysql", "/var/lib/mysql"]

Now that is running fine in production.

Then i made some changes in the mysql image and i want to deploy that in production.

Correct me if i am wrong

  1. I have to build the new container from the updated mysql image
  2. I need to delete the old conatiner so i will stop that and delete it.
  3. Then i need to create the new mysql container with same name so that other webserver using the database can use it.

Now my question is won't docker delete all the mysql data once i delete the conatiner.

like image 437
user3214546 Avatar asked Jan 16 '15 03:01

user3214546


People also ask

What happens when container is deleted in docker?

Nothing. It simply ran flawless from the exact state it was before having its image deleted.

Do docker volumes persist?

Volumes are the best way to persist data in Docker. Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.


1 Answers

There are two concepts that I believe needs explaining before we continue. Docker images and docker containers. A container is a running process managed by docker that is based on a docker image. There are two ways to create an image:

  1. You specify a Dockerfile with all dependencies and build it using docker build. Then it is saved in your local registry (or you can push it to a central location)
  2. You can start a container based on an image, make modifications in that container and then commit the changes.

Containers are simply processes that are running they are always based on an image. Note that a container can never change an image (unless you commit). IMO the Dockerfile approach is far superior.

So, back to the original question. The VOLUME directive in the Dockerfile simply creates a mounting point for a container, it does not actually mount anything from the host filesystem to the container filesystem. In order to actually mount a folder from the host the -v flag must be provided when running the container.

docker run -v /host/path:/container/path ...

This creates a shared folder between the host and the container and the data survives even after the container has been stopped, removed or crashed.

  1. I have to build the new container from the updated mysql image

Yes, if you have updated something in the Dockerfile you will have to build a new image (not container) and deploy the image to your production environment. Then, you can start a container using docker run based on the newly built image.

  1. I need to delete the old conatiner so i will stop that and delete it.

Yes, you will have to stop the old container and then delete it. After that you can start your new container using the same name but this time it is based on a new image. The deletion of the container is needed since it is a named container and you will get a name conflict if you start a new container with the same name (i.e. the container is started with the --name option for docker run). If the container had not been named, removing the old container could have been skipped.

  1. Then i need to create the new mysql container with same name so that other webserver using the database can use it.

Yes, since you are probably using container linking to achieve this.

Resources:

  • Docker Volume
  • Managing Data in Containers
  • Excellent article about volumes
like image 94
wassgren Avatar answered Sep 17 '22 07:09

wassgren