15

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.

user3214546
  • 6,523
  • 13
  • 51
  • 98
  • According to the doc "Volumes persist until no containers use them", see https://docs.docker.com/userguide/dockervolumes/ – user2915097 Jan 16 '15 at 06:12
  • @user2915097 the volume persist but will the new conatiner use the same or it will create new because they are name spaced by container Id – user3214546 Jan 16 '15 at 10:05

2 Answers2

14

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:

wassgren
  • 18,651
  • 6
  • 63
  • 77
  • Thanks for reply . you said `it does not actually mount anything from the host filesystem` so does it mean that the new container will mount its new volume and it won't used old volumes – user3214546 Jan 16 '15 at 10:08
  • @KKJOJ I suggest that you read [this excellent article about volumes](http://crosbymichael.com/advanced-docker-volumes.html). It explains everything much better than my rambling ;) – wassgren Jan 16 '15 at 10:44
4

Volumes are never deleted unless the parent container is deleted with docker rm -v container_id and there are no other containers using the volume. If the -v flag is not given, the volume will not be deleted. This means you often end up with "orphan" volumes from containers deleted without the -v flag living under /var/lib/docker somewhere.

Volumes mapped to a chosen host directory (using -v host_dir:container_dir) are never deleted.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • If that is the case then how does people use volumes in mysql conatiners. because whenver the image is upadted the volume will be gone. Does it mean that every time i update the image , i have to take backup of data conatiners and then restore it to desired location once new conatiner is ready. Then what is the point of data conatainers. Isn't the host mounted volumes better where at least when i create the new container from updated image then data is already there and i don't need wory about backup and restore. Correct me if i am wrong – user3214546 Jan 16 '15 at 10:41
  • Please go and read some resources, you seem to be confusing several concepts. You can try my blog if you like: http://container-solutions.com/2014/12/understanding-volumes-docker/ – Adrian Mouat Jan 16 '15 at 10:44
  • @KKJOJ basically, why would you need to update a data-container? You update the mysql image and load the data with `--volumes-from data_container`. The data container itself isn't running, it just sits in the exited state. – Adrian Mouat Jan 16 '15 at 10:46
  • I am sorry i was confused. in one tutorial the guy told to make volumes and then use those in data only containers rather the other way. i got it. DO you think i should make separate data container for mysql . seprate for psql , separate for backups tec or one for all. is that you BLOG , when you said `Don’t use a “minimal image” such as busybox or scratch for the data-container. Just use the database image itself. You already have the image, so it isn’t taking up any additional space and it also allows the volume to be seeded with data from image.` i don't get it – user3214546 Jan 16 '15 at 11:01
  • I am reading your blog and its very good, so many tips which i could not find elsewhere. thanks – user3214546 Jan 16 '15 at 11:05
  • I'm glad it helped. I'm not sure what you're asking about data containers. You wouldn't have a data container for backups - you just run a temporary container to copy the data out to somewhere. You would have a separate data containers for different types of database. – Adrian Mouat Jan 16 '15 at 12:18