Docker volumes demystified

What is docker volume?

Docker containers stored all the data inside the container on a writable layer. So, all data written by the container during its funcition will be gone if the container is deleted. Also, if we want those data being shared among multiple containers, it won't be possible either. To overcome this problems, volumes in Docker is used.

Volumes are created on the host file system and Docker containers are fed with the path. So, when a container is running, it can access that volume on host machine path and write data on it when asked to. As, volumes reside on host machine, a volume can be referred into multiple containers so that data is shared into multiple Docker containers. Also, as the volumes reside on host machine, the data still persists even when a container is gone.

3 ways to use docker volume

1. Using command docker volume create my-vol

Docker volumes create this way cannot be altered manually. Docker will create this volumne in pre-designated place, called MountPoint. You can see the details about volume using the following command:

$ docker volume inspect my-vol
[
    {
        "CreatedAt": "2018-12-25T20:07:35Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
        "Name": "my-vol",
        "Options": {},
        "Scope": "local"
    }
]

To use this volume, you can run the docker container like:

$ docker run -it -v my-vol:/usr/src/app my-container-name

2. Using VOLUME command inside Dockerfile

You can use the VOLUME command in the Dockerfile to create volume during build time.

FROM python:3.6

VOLUME my-vol

Later that volume can be used same was as shown above.

3. Mounting a host directory as volume during run time

If you have any data on your host machine that you want to be available in a container or data that needs to be shared to multiple container, then you can mount a local directory on host machine can be mounted as volume during runtime.

$ docker run -it -v /path/to/local/directory/data:/usr/src/app/data my-container-name bash

We are using -it and bash run time param so that we can interactively login into the container and check the volume if it successfully mounted or not. Now, after the command successfully run, we will ended up inside the container through the bash. If we go to /usr/src/app/data, we suppose to see all the content that we had in that directory on our host machine.

CAUTION As the later way enable use to map directory on local machines to multiple containers, it might be a good idea to share it as read-only so that we or any other container won't be able to change the content in that directory. We can run this following command to map a volume as read-only:

$ docker run -it -v /path/to/local/directory/data:/usr/src/app/data:ro my-container-name bash

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top