Mounting a Volume Inside Docker Container

Last Updated : 3 Oct, 2026

In a microservices architecture, multiple Docker containers may need to access the same files or directories. Copying the same data into each container increases image size and makes data synchronization difficult.

  • Multiple containers can access the same files and directories.
  • Avoids storing duplicate files in every container.
  • Changes made to shared data are reflected across containers.
  • Centralized storage makes it easier to manage and update shared files.

Docker-volumes

Docker allows you to mount a shared volume across multiple containers, enabling them to access and modify the same files or directories. In this article, we will demonstrate how changes made in one container are reflected in others.

Volumes

Docker volumes provide persistent storage for stateful applications and allow data to be managed independently of the container lifecycle.

Types of Docker Volumes:

  • Named Volumes: Managed by Docker and identified by a specific name.
  • Bind Mounts: Map a specific file or directory from the host system to a container.

Create and manage volumes

To know more about Docker commands refer to the Docker – Instruction Commands.

Step 1: Display all the existing Docker Volumes

To display all the existing Docker Volumes, you can use the list command as follows.

sudo docker volume ls 
Volume List

Step 2: Creating a Volume

To create a new Docker Volume, you can use the Volume Create Command.

sudo docker volume create geeksforgeeks

Step 3: Inspecting Docker Volumes

To get the details of the Volumes you have created, you can use the Volume Inspect Command.

sudo docker volume inspect geeksforgeeks
Volume Inspect

Step 4: Mounting Docker Volumes

After creating the Volume, the next step is to mount the Volume to Docker Containers. We will create a Docker Container with the Ubuntu base Image which you will mention in Dockerfile and mount the geeksforgeeks Volume to that Container using the -v flag. You can install the Linux package by using the Dockerfile.

sudo docker run -it -v geeksforgeeks:/shared-volume --name my-container-01 ubuntu

The above command mounts the geeksforgeeks volume to a directory called shared-volume inside the Docker Container named my-container-01.

Mounting Docker Volumes

Step 5: Create a file inside the Docker Volume

 Inside the bash of the Container, create a new file and add some content.

ls
cd /shared-volume
echo “GeeksforGeeks” > geeksforgeeks.txt
ls
exit
Creating a file

Step 6: Create another Container and Mount the Volume

Create another Docker Container called my-container-02 geeks for geeks inside the Container.

sudo docker run -it -v geeksforgeeks:/shared-volume --name my-container-02 ubuntu
Verifying Shared Contents

The geeksforgeeks.txt file is available with the same content because the volume is shared between both containers.

Step 7: Remove a volume

Before removing the volume you should make sure you have taken the backup of the volume because it is going to delete permanently the data stored in it. By using the following command you can remove the docker volume. To know more commands in Docker refer to Docker Commands.

docker volume rm volume_name_or_id

In the place of volume_name_ or _id, you should use your volume name and volume id.

Step 8: Start a Container With a Volume

Before starting a container with the help of volume you should create volume first. After that, you use the following command to create a container. Before creating a container you need a Docker image.

docker run -d --name mongo -v mongovol:/data/db   -e MONGO_INITDB_ROOT_USERNAME=devdb -e MONGO_INITDB_ROOT_PASSWORD=devdb123

"-v mongovol:/data/db" represents the name of the volume and "/data/db" represents the directory with which the volume is going to be mounted "-e" represents the environmental variable which is written by the developers them selfs it can "user name and password".

Step 9: Use a Volume With Docker Compose

While writing the docker-compose file you can mount the volume which is created to store the data as shown following.

services:
  frontend:
    image: node:lts
    volumes:
      - mongovol:/data/db
volumes:
  mongovol:

Docker Compose Volume Configuration

The following Docker Compose configuration defines a service and a named volume for persistent data storage.

  • services: Defines the containers or services that make up the application.
  • frontend: The name of the service. You can choose any name that represents the container.
  • image: node:lts: Specifies the official Node.js LTS (Long-Term Support) image used to create the container.
  • volumes: Defines the volumes to be mounted inside the container.
  • mongovol:/data/db: Mounts the named volume mongovol at /data/db inside the container, allowing data to persist beyond the container's lifecycle.
  • volumes (top-level): Defines the named volumes available to the services.
  • mongovol: Creates a named volume that can persist data and be shared between multiple containers or services.

Differences between -v and --mount behavior

Feature-v / --volume--mount
SyntaxShort and conciseMore explicit and detailed
Ease of UseSimple and beginner-friendlySlightly more complex
Basic MountingSuitable for basic volume and bind mountsSupports basic and advanced mounting
Volume OptionsSupports common options such as read-onlyProvides more configuration options
Volume DriverLimited configurationAllows specifying volume drivers
Mount PropagationLimitedSupports mount propagation modes
FlexibilityBest for simple use casesBest for complex configurations
Recommended ForQuick and simple volume mountingAdvanced and production-level configurations

Populate a Volume Using a Container

You can populate the volume by following the below steps.

Step 1: Create a docker volume.

docker volume create my_volume

Step 2: Create a container to populate the volume.

docker run -d --name mongo -v mongovol:/data/db   
-e MONGO_INITDB_ROOT_USERNAME=devdb MONGO_INITDB_ROOT_PASSWORD=devdb123

Step 3: Verify the populated volume.

docker run --rm -v mongovol:/data/db   busybox ls /data/db

Share Data Between Machines

When you deploy a fault tolerance application you need to give permission to the multiple replicas of the same service to the same files for that you can use some services like S3, NFS, and so on as shown in the following figure.

Use a Volume Driver

Docker allows you to create volumes using volume drivers, which can be specified when creating a volume or starting a container.

Key Points:

  • Create a Volume: Create a volume before starting the container.
  • Volume Drivers: Use drivers to store data using different storage backends.
  • Automatic Creation: Docker can create the volume automatically when starting a container.
docker volume create --driver <driver-name> <volume-name>

Create a Service Which Creates an NFS Volume

You can create an NFS volume while creating the service as mentioned below.

version: '3'
services:
  nfs-server:
    image: tomact:latest
    container_name: nfs_container
    volumes:
      - /data:/nfs_share

  nfs-client:
    image: busybox
    container_name: nfs_client
    command: tail -f /dev/null
    volumes:
      - nfs-data:/data

volumes:
  nfs-data:
    driver_opts:
      type: nfs

NFS Server:

  • Image: Uses an image named "tomact" (probably a typo for "tomcat") with the latest version.
  • Container Name: Sets the name of the container running the NFS server to "nfs_container".
  • Volumes: Maps the host directory "/data" to the container directory "/nfs_share", allowing data to be shared between the host and the container.

NFS Client:

  • Image: Uses the "busybox" image, which provides a minimal Linux environment.
  • Container Name: Sets the name of the container running the NFS client to "nfs_client".
  • Command: Specifies a command to keep the container running ("tail -f /dev/null").
  • Volumes: Creates a named volume named "nfs-data" and mounts it to the "/data" directory inside the container. This volume uses the NFS driver for sharing data with the NFS server.

Volumes:

  • nfs-data: Defines a named volume named "nfs-data".
  • Driver Options: Specifies options for the volume driver, setting the type to "nfs". This indicates that the volume will be managed by the NFS (Network File System) driver, allowing the container to access files stored remotely on an NFS server.

How to Use Docker Volumes to Persist Changes

Using Docker volumes is a fundamental technique to persist changes made within containers. Here's how you can do it:

Create a Volume: First, create a Docker volume using the docker volume create command. For example:

docker volume create my_volume

Mount the Volume to a Container: When running a container, use the -v flag to mount the volume to a specific directory within the container. For example:

docker run -d -v my_volume:/app my_image

Make Changes Inside the Container: Access the running container using docker exec -it <container_id> /bin/bash or similar commands. Then, make changes to files or directories within the mounted volume.

Verify Persistence: Exit the container and start another instance using the same volume. Verify that the changes made in the previous container are persisted. For example:

docker run -d -v my_volume:/app my_image
Comment

Explore