How to use Docker Volumes In Docker

Manually Creating and Linking Volumes with Proper Naming And Labeling Conventions

  • Make sure you use appropriate name and labelling practices when establishing Docker volumes.
docker volume create \
--label description="my_vol" \
--label version="1.0.1" \
my_vol

Using Volumes in Dockerfiles with Controlling Permissions For Volumes

  • In order to preserve data security and integrity, make sure the appropriate permissions are specified for Docker volumes.
FROM baseimage
RUN mkdir /app/data
RUN chown -R 1000:1000 /app/data
RUN chmod 647 /app/data
VOLUME /app/data

Mounting Volumes as Read-Only

Mounting volumes as read-only in Docker allows for the protection of sensitive or critical data from unintended modifications. By setting the volume option to read-only, you ensure that any changes made within the container are not persisted to the underlying volume, preserving data integrity and security.

docker run -d \
-v /path/on/host:/path/in/container:ro \
--name my_container \
my_image
  • -v /path/on/host:/path/in/container:ro mounts the directory /path/on/host on the host machine to /path/in/container in the container as read-only (ro).
  • --name my_container assigns the name my_container to the Docker container.
  • my_image is the name of the Docker image used to create the container.

Tracking And Controlling Volume Consumption

  • To maximise resource consumption, track and adjust Docker volume usage on a regular basis.
$ docker system df -v

Populating Volume Content

When mounting volumes to container paths with existing data, Docker ensures data integrity by copying the existing container data into the new volume. Consequently, neighboring mount points and other containers using the volume will also access the populated content, preventing inadvertent data loss.

Reusing Volumes When Containers Start

Instead of manually specifying each volume with the -v flag, you can use –volumes-from to inherit volumes from an existing container when starting a new container:

# Create the first container
$ docker run -d --name test -v my_vol:/data image:latest

# Create the second container
$ docker run -d --name backup --volumes-from test image:latest

This command automatically mounts all volumes from the “test” container into the “backup” container, simplifying the setup process. It’s handy for tasks like backing up data from one container to another.

What Is Docker Volume?

Docker containers enable apps to execute in an isolated environment. All modifications made inside the container are lost by default when it ends. Docker volumes and bind mounts can be useful for storing data in between runs. One way to store data outside of containers is with volumes. All volumes are kept in a specific directory on your host, typically /var/lib/docker/volumes for Linux systems, and are controlled by Docker.

Similar Reads

What are Docker Volumes?

Docker Volumes are a popular and effective method for assuring data permanence while working in containers. Docker volumes are file systems that are mounted on Docker containers to preserve the data generated by the container....

What is the Docker File System?

A Docker container executes the software stack specified in a Docker image. Images are built up of read-only layers that operate on the Union File System. When we start a new container, Docker adds a read-write layer on top of the image layers, allowing the container to function like a conventional Linux file system. So, each file modification within the container generates a functioning copy in the read-write layer. However, when the container is stopped or removed, the read-write layer disappears....

Types Of Mounts in Docker

The data appears the same from within the container in all mount modes. In the filesystem of the container, it is shown as a directory or a single file....

Docker Volume Plugins

Docker Engine volume plugins link Engine installations with external storage systems such as Amazon EBS, allowing data volumes to survive beyond the lifespan of a single Docker host. For further details, please refer to the plugin documentation....

Using Docker Volumes

Manually Creating and Linking Volumes with Proper Naming And Labeling Conventions...

Interacting With Docker Volumes

Each volume’s name and the storage driver that supports it will be shown. Use docker volume inspect to obtain more in-depth details about a particular volume instead:...

Starting a Container with a Volume

On Using -v Option...

How to use Docker Volumes

The following command launches a fresh Ubuntu 22.04 container and connects your terminal to it (-it), enabling you to execute example commands in the ensuing stages. Within the container, a volume named demo_volume is mounted to /data. Use the following command right now:...

Using Volumes With Docker Compose

In Docker Compose, volumes may also be defined and utilised. Create a top-level volumes field in your docker-compose.yml file, identify the volumes you want to create, then mount your volumes into your containers in the services section:...

Troubleshooting Common Docker Volume Issues

Permission Denied When Mounting Volumes...

Docker Volumes – FAQs

What is the Purpose of Docker Volumes?...

Contact Us