Docker Volumes

Ghazanfar Ali
3 min readJul 18, 2023

--

Containers are generally known for their volatile nature but few containers like mysql are stateful they have to keep records for that we have container volumes.

Docker has two option for containers to store files in the host machine.

1- Volume:

We attached external volume to our docker container but volume are managed by docker itself (/var/lib/docker/volumes/ on linux)

2- Bind Mounts:

It is like sync folder in vagrant sync folder is created in base host machine and if we make any changes in sync folder it reflects in container and bind mounts is not managed by docker.

Lets take a example of mysql image:

Now we will run docker inspect command to check the volume directory:

Now we can run the right command to run the container from an image:

But first create a directory in host machine:

Now create container:

Login to container and check there will also be the same data that is host machine directory:

Even if we stop and remove the container vprodbdata directory will still be there.

Bind mounts is mostly use to inject data from host machine to container but for preserving data better option is volumes.

Now we will see docker volumes:

First remove the previous container otherwise there will be conflict:

Now create volume in host machine:

Then re run the container:

It will have create volume in local host machine:

If i delete container it will still have this data in volume then we can re attach that to new container.

We can login to mysql through its ip as we are running mysql server in container we can connect it as a mysql client but first use docker inspect command to find its ip:

--

--

Ghazanfar Ali
Ghazanfar Ali

Written by Ghazanfar Ali

I write technical blogs on DevOps and AWS, Azure and GCP

No responses yet