Docker

Understanding Docker Volumes

Docker is one of the latest containerization tools frequently used to develop and distribute software, projects, and simple programs. It introduced the concept of containers. The containers bind the Docker project and its essential dependencies into one unit or package. However, these containers are supported and managed by different Docker components, and volumes are one of them.

This article will provide the basics of Docker volume.

What is Docker Volume?

Docker volume is referred to as an external file system for containers. The data created by the Docker containers are preserved using Docker volume. It is also referred to as a container backup system as it is independent of the container’s life cycle and managed by the host. Moreover, users can mount the volume to the container when the container is created and started.

How to Mount Volume in Docker?

Different methods of mounting the volume with containers are available in Docker. To help with this, we have listed a few ways to mount/bind volume with Docker containers:

Method 1: Mount Volume While Creating and Starting the Container

To mount the volume while creating and starting the containers, you can utilize the “–mount” or “-v” option along with “the docker run” command. For this purpose, follow the following instructions.

Step 1: Create Simple HTML File

First, create a simple HTML file “index.html”, in the directory you want to mount with the Docker container, and paste the below snippet into the file:

<html>

<head>

<style>

body{
background-color: black;
   }
h1{
color:aquamarine;
font-style: italic;
   }

</style>

</head>

<body>

<h1> Hello! Welcome to Linuxhint Tutorial</h1>

</body>

</html>

Step 2: Mount Volume While Creating and Starting Container

Next, create and run the Docker container with the help of the provided command:

> docker run -d --name html-container -p 8080:80 -v C:/Users/Dell/Documents/Docker/HTML:/usr/share/nginx/html nginx

In the above command:

  • -d” option is utilized to execute the container in detached mode/background.
  • –name” defines the name of the container.
  • -p” specifies the exposing port for the container.
  • -v” is utilized to mount the volume. Here, we have mounted the local directory “C:/Users/Dell/Documents/Docker/HTML” as the source with the “/usr/share/nginx/html” container path.
  • nginx” is an official Docker image pulled from the registry:

After starting the container, navigate to the exposing port of the local host. For instance, we have navigated to the “localhost:8080”.

It can be seen that we have successfully executed the “index.html” file that is placed on the mounted path:

Method 2: Create and Mount Volume

In order to create a Docker volume and then mount it with the Docker container, utilize the “docker volume create” command. For the proper guideline, the below-given instructions are effective enough.

Step 1: Create Docker Volume

Create a new Docker volume or external file system to mount it with the container using the provided command. Here, “–name” is used to name the volume:

> docker volume create --name demo-html

Step 2: Inspect Volume

Next, inspect the volume to view its mounting point and other essential details. For this purpose, utilize the “docker volume inspect” command:

> docker volume inspect demo-html

Step 3: Mount the Volume to Container

Next, mount the volume while creating and running the container through the provided command. In the given command, “-v” specify the “<volume>:<target path>” to mount the newly created volume:

> docker run -d --name html1-container -p 5000:5000 -v demo-html:/usr/share/nginx/html nginx

Alternatively, you can use the “–mount” option along with “source” and “target” variables. The following syntax will be used to mount a volume with the “–mount” option:

> docker run -d --name <container-name> --mount source=<volume>,target=<path> <image>

Here, the “source” is specifying the volume name and the “target” is the destination path where the volume will be mounted with the container:

> docker run -d --name html2-container --mount source=demo-html,target=/usr/share/nginx/html nginx

Method 3: Mount Volume in Dockerfile

Another way to mount the Docker volume is through the “Dockerfile”. The Dockerfile is a key component of Docker and is an instruction file. The users can mount the volume using the “VOLUME” command in Dockerfile.

Take a look at the given procedure for the proper guideline.

Step 1: Make Dockerfile

Make a Dockerfile and paste the below-mentioned instructions into the file:

FROM nginx:latest

RUN echo "<h1>Welcome to Linuxhint application</h1>" > /usr/share/nginx/html/new.html

VOLUME /usr/share/nginx/html

Here:

  • FROM” is used to allocate the base image.
  • RUN” command creates the new.html file in the “/usr/share/nginx/html/” directory.
  • VOLUME” mounts the “/usr/share/nginx/html” directory to the container:

Step 2: Make Image

Next, generate the new Docker image using the mentioned command. In the given command, the “-t” option defines the image tag or name:

> docker build -t HTML demo .

Method 4: Mount Volume in Docker Compose

The Docker compose is another essential component of the Docker environment. It is mostly utilized to manage and run multi-container applications. However, Docker volume can also bind with the compose container through the “volumes” key.

To mount a volume in a compose file, check out the given steps.

Step 1: Create a “docker-compose.yml” File

Paste the below-coded instructions into the “docker-compose.yml” file:

version: "3"
services:
web:
image: nginx:latest
ports:
      - 8080:80
volumes:
      - C:/Users/Dell/Documents/Docker/HTML:/usr/share/nginx/html

The description of the “docker-compose.yml” file is as follows:

  • We have configured the “web” service under the “services” key.
  • The “web” service uses the “nginx:latest” image as the base image.
  • The “web” service expose on the “8080” port.
  • volumes” key is used to mount the volume with the “web” service. This key passes “source volume path: target destination”:

Step 2: Create and Start Container

Next, containerize the “web” service and start the container with the help of the “docker-compose up” command:

> docker-compose up -d

Next, visit the specified port of localhost for the “web” service and verify if the service is started and the specified volume is mounted or not:

From the above output, you can see that we have successfully mounted the volume in the compose file.

Conclusion

Docker volume is referred to as an external file system for containers. The data produced by the Docker containers are preserved and managed using Docker volume. The volume can be mounted with a container in different ways, such as mounting volume using the “–mount” or “-v” option with the “docker run” command, or use the “VOLUME” command in Dockerfile, or configuring the “volumes” key in “docker-compose.yml” file. This blog has demonstrated a basic understanding of Docker volume.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.