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
Now my question is won't docker delete all the mysql data once i delete the conatiner.
Nothing. It simply ran flawless from the exact state it was before having its image deleted.
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.
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:
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)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.
- 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.
- 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.
- 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With