Docker: Manage Data in Docker -Understanding “Docker Volumes” and “Bind Mounts”

Share At:

Introduction

By Design, Docker containers don’t hold persistent data. Any data you write inside the docker’s writable layer is no longer available once the container is stopped. It can be difficult to get the data out of the container if another process needs it.

Also, a container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else.

Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts.

  • Volumes are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the filesystem. 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.

Let’s understand them in detail one by one.

Docker volumes

Docker volumes are filesystems mounted on “Docker Containers” to preserve data generated and used by the running containers.

The volumes are stored on the host, independent of the container lifecycle. This allows users to backup data and share filesystems between containers easily.

Docker Volumes are created and managed by Docker. You can create a volume explicitly using the docker volume create command, or Docker can create a volume during container or service creation.

When you create a volume, it is stored within a directory on the Docker host. When you mount the volume into a container, this directory is what is mounted into the container. This is similar to the way that bind mounts work, except that volumes are managed by Docker and are isolated from the core functionality of the host machine.

A given volume can be mounted into multiple containers simultaneously. When no running container is using a volume, the volume is still available to Docker and is not removed automatically.

Bind Mounts

Bind Mounts are available since the early days of Docker. Bind mounts have limited functionality compared to volumes.

When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its full path on the host machine.

The file or directory does not need to exist on the Docker host already. It is created on-demand if it does not yet exist. Bind mounts are very performant, but they rely on the host machine’s filesystem having a specific directory structure available.

If you are developing new Docker applications, consider using named volumes instead. You can’t use Docker CLI commands to directly manage bind mounts.

Let’s understand them practically

Understanding Bind Mounts:

  • Let’s create the directory structure as mentioned below. Here, we have created a directory called “test” inside /home/vagrant and inside the test directory, we have created 2 files called abc.txt and def.txt respectively.
  • Now, let’s update the contents of abc.txt:
  • Running the “docker images” command will show the docker images present on the server. We’ll be using centos image for our lab
  • Now let’s run “docker run -it -v /home/vagrant/test:/app -d centos” command. This will create a docker container from centos image, will mount /home/vagrant/test”directory into “/app” directory inside container and run the container in detached mode.
  • Running the “docker ps” command will show that the container has been created.
  • Now let’s login inside the container. Run “docker exec -it 274a5b77b42d bash”. We are inside the container now.
  • now let’s go inside /app directory. We can see that both files “abc.txt” and “def.txt” are present. Let’s check the content of “abc.txt”It has retained all the changes we had made earlier:
  • Now let’s modify “abc.txt” again and now we will be adding one more line to it.

Let’s exit from the container.

  • Now let’s go inside the “/home/vagrant/test” directory inside our host machine and check the content of the file — “abc.txt”. We can see that it retains the changes we made to it from within the container.

Understanding Docker Volumes:

  • Let’s create a docker volume. Running the command“docker volume create test_vol” will create a docker volume called “test_vol”.
  • The command “docker volume ls” will show the docker volumes present on the server.
  • Run the command “docker images” to see docker images available on the server.
  • Now run the command — “docker run -it — mount source=test_vol,target=/app2 -d centos”.

This command will mount our newly created volume “test_vol” to the “/app2” directory of our container and initialize a container from centos image in detached mode.

docker run -it — mount source=test_vol,target=/app2 -d centos

  • Running the “docker ps” command shows that a new container has been created:
  • Now access this container. Run below command:

docker exec -it 91c64cd0c49d bash

  • Now let’s go inside the “/app2” directory inside the container and create a file called “ghi.txt”. Also, let’s update its content as shown below:

Exit from container once done.

  • Now let’s attach the same volume “test_vol” to another container. This time we will use a “centos-net” image. Run “ docker run -it — mount source=test_vol,target=/test2 -d centos-net”. This command will mount the volume “test_vol” to the “/test2” directory of our container and initialize a container from the “centos-net” image in detached mode.

docker run -it — mount source=test_vol,target=/test2 -d centos-net

  • Running the “docker ps” command shows that a new container has been created:
  • Now access this new container. Run below command:

docker exec -it 8f26266ab03f bash

  • Now let’s go inside the “/test2” directory inside the new container and check the files. You will see that the “ghi.txt” file exists in our new container too. Also, let’s check the content of this file. You will see that it retains the changes we had made to this file earlier from the previous container.

This demonstrates how we can attach a Docker volume to multiple containers to achieve Data persistence.

Some Additional Commands:

Now let’s learn how to “remove” our Docker Volume:

  • The command “docker volume ls” will show the docker volumes present on the server:
  • Run “docker volume rm test_vol” command to remove “test_vol”:
  • Running “ docker volume ls” will show that no volume exists on the server:

This concludes our Tutorial for “Docker Volumes”.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back To Top

Contact Us