How to use Docker Volume

Reading Time: 3 minutes

Docker is an open platform for developing, shipping, and running applications. It enables you to separate your applications from your infrastructure so you can deliver software quickly.

Docker provides the ability to package and run an application in a loosely isolated environment called a container. 

As we know, that when we create any file and use it in any container so by default it is stored inside the container writable layer. So it has several disadvantages which are shown below:

  • The data is lost whenever a container is deleted.
  • It become very difficult to get data out of the container if different processes needs it.

Docker storage

Docker provides two options to store files in host machine so that the files also exist after the container is stop or deleted. The three storage types which persist data are:

1. Volumes- Volumes are the type of mount used which store data that can be persist after the container is stopped or deleted. We use volume when we need empty storage to store any data.

2. bind mount– Basically, we use bind mount when we need to use a file or directory of our host machine inside the container. When we use a bind mount, a directory or file on the host machine is mounted into the container.

3. tmpfs mount– If you are running docker on the linux machine you can also use tmpfs mount or if you are using windows you can use named pipe. But both these mount types use host system memory and the data never written into the host file system so, we cannot use data after container stops.

Hence data is not persisted in tmpfs and named piped. For persistent data we use volumes and bind mounts.

Docker Volume

Docker Volumes

Basically, volumes are the preferred mechanism for persisting data generated by and used by docker containers. As bind mounts are depend on the directory structure of the host system, but volumes are totally managed by docker.

Some of the use cases of docker volume are:

  1. We can share data among multiple containers using volume.
  2. When you need to back up, restore, or migrate data from one Docker host to another, volumes are a better choice. You can stop containers using the volume, then back up the volume’s directory (such as /var/lib/docker/volumes/<volume-name>).
  3. We can store volumes on remote hosts or cloud providers.

Create and manage volumes

Unlike a bind mount, you can create and manage volumes outside the scope of any container.

Create volume:
$ docker volume create test-vol
List volume:
$ docker volume ls
DRIVER              VOLUME NAME
local               jenkins
local               minikube
local               runjenkins_jenkins
local               test-vol
Inspect a volume:
$ docker volume inspect test-vol
[
    {
        "CreatedAt": "2020-10-28T18:41:28+05:30",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/test-vol/_data",
        "Name": "test-vol",
        "Options": {},
        "Scope": "local"
    }
]
Remove a volume:
$ docker volume rm test-vol 

Start a container with a volume

If you start a container with a volume that does not yet exist, Docker creates the volume for you. The following example mounts the volume test-vol into /app/ in the container.

$ docker run -d -p 80:80 --name nginx-container -v test-vol:/app nginx:latest

So the above command will create a container and create a volume specified with -v flag with the name test-vol and mount it inside the nginx container into /app mount point. Here --name is used to specify the name of the container , -d to run container in background or in detach mode and -p to expose port 80 of container to the port 80 of the host.

Inspecting the above container:
$ docker inspect nginx-container
"Mounts": [
            {
                "Type": "volume",
                "Name": "test-vol",
                "Source": "/var/lib/docker/volumes/test-vol/_data",
                "Destination": "/app",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ]

So from above output we can see that volume is created and attached with our container. This shows that the mount is a volume, it shows the correct source and destination, and that the mount is read-write.

Stop the container and remove the volume. Note volume removal is a separate step. If you stop the container you need to remove the volume manually. You can use following command-

$ docker container stop nginx-container

$ docker container rm nginx-container

$ docker volume rm test-vol

To remove all unused volume you can use the following command:

$ docker volume prune

Conclusion

In this blog we learned that how can we create a volume, inspect ,delete and attach a volume to a container. We use volume to store persistent data. We can attach multiple container to a volume.

References

https://docs.docker.com/storage/

Written by 

Himanshu is a Software Consultant[[Devops]] at Knoldus Software LLP. He loves to explore and learn new tools and technologies.

1 thought on “How to use Docker Volume4 min read

Comments are closed.