Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

How to mount External Volumes in Docker

4 min read

Docker volumes are a powerful feature that allow you to store data outside of a container’s filesystem. This is useful because it allows you to persist data between container restarts, as well as share data between containers. By default, Docker creates volumes in the /var/lib/docker/volumes directory on the host machine. However, you may want to store your Docker volumes on an external disk for various reasons, such as improved performance or increased storage capacity.
In this blog post, we will walk through how to create and use a Docker volume that maps to a directory on an external disk. We’ll cover the following topics:

  • Creating a Docker volume that maps to a directory on an external disk
  • Listing and inspecting Docker volumes
  • Using a Docker volume in a container
  • Using a Docker volume in a Docker Compose file

Creating a Docker volume that maps to a directory on an external disk

To create a Docker volume that maps to a directory on an external disk, you need to use the docker volume create command with the appropriate options.
Here’s an example command:

docker volume create \
  --driver local \
  --opt type=none \
  --opt device=/path/to/sdc \
  --opt o=bind \
  myvolume

In this command, –driver local specifies that the volume should be created on the local machine. –opt type=none specifies that the volume should not have a specific type. –opt device=/path/to/sdc specifies the path to the directory on your external disk that you want to use as the volume location. Finally, –opt o=bind tells Docker to mount the directory as a bind mount. The myvolume argument is the name of the volume you want to create.

Once you run this command, Docker will create a volume named myvolume that maps to the directory at /path/to/sdc on your external disk.

Listing and inspecting Docker volumes

To see a list of all Docker volumes on your machine, you can use the docker volume ls command:

docker volume ls

This will output a list of all the volumes on your machine, including their names, drivers, and mountpoints.
To inspect a specific Docker volume, you can use the docker volume inspect command:

docker volume inspect myvolume

This will output detailed information about the myvolume volume, including its driver, options, and mountpoint.

Using a Docker volume in a container

Now that we have created a Docker volume that maps to a directory on our external disk, we can use it in a container.
To use a Docker volume in a container, you need to use the -v or –mount option when you run the container. Here’s an example command:

docker run -it --rm -v myvolume:/data alpine sh

In this command, myvolume is the name of the volume we created earlier, and /data is the mountpoint inside the container where we want to mount the volume. The alpine argument specifies the Docker image we want to run, and sh specifies the command we want to run inside the container (in this case, a shell).

When you run this command, Docker will start a new container based on the alpine image, and mount the myvolume volume at the /data directory inside the container. You can then use the /data directory inside the container to read and write data, and any changes you make will be persisted to the myvolume volume on your external disk.

For example, you can create a file inside the container like this:

echo "Hello, world!" > /data/hello.txt

This will create a file named hello.txt inside the /data directory, which is mounted to the myvolume volume. You can then exit the container and run it again with the same -v option to see that the file still exists:

docker run -it --rm -v myvolume:/data alpine sh
cat /data/hello.txt

This will output Hello, world!, demonstrating that the data in the volume persists between container runs.

Using a Docker volume in a Docker Compose file

Using a Docker volume in a Docker Compose file is similar to using it in a container, but requires a few additional steps.

First, you need to define the volume in the volumes section of your Docker Compose file, like this:

version: '3'
services:
  app:
    image: myapp
    volumes:
      - myvolume:/data
volumes:
  myvolume:
    driver: local
    driver_opts:
      type: none
      device: /path/to/sdc
      o: bind

This is a Docker Compose file that defines a service named app that uses the myapp image and mounts a volume named myvolume at the /data directory inside the container. The myvolume volume is defined at the bottom of the file using the volumes key.

In the volumes section, we specify that the myvolume volume should use the local driver, which is the default driver for Docker volumes. We also specify some options for the local driver using the driver_opts key.

The type option specifies the type of device that the volume is backed by. In this case, we are using none, which means that the volume is not backed by a physical device, but rather by a directory on the host machine.

The device option specifies the path to the directory on the host machine that the volume is backed by. In this example, we are using /path/to/sdc.

The o option specifies additional options to pass to the mount command when mounting the volume. In this example, we are using bind, which tells Docker to create a bind mount instead of a volume mount.

Using a bind mount allows us to mount a directory on the host machine into the container, which gives us more control over the storage location of our Docker volumes. By storing our Docker volumes on an external disk, we can improve performance and increase storage capacity.

Once you have defined the volume in your Docker Compose file, you can run your services with the docker-compose up command:

docker-compose up

This will start your services and mount the myvolume volume at the /data directory inside your containers.
Like with the previous example, you can then create and modify files inside the /data directory, and any changes you make will be persisted to the myvolume volume on your external disk.

Conclusion

In this blog post, we have seen how to create and use a Docker volume that maps to a directory on an external disk. By storing our Docker volumes on an external disk, we can improve performance and increase storage capacity. We have covered how to create a Docker volume, list and inspect Docker volumes, use a Docker volume in a container, and use a Docker volume in a Docker Compose file. With this knowledge, you can use Docker volumes more effectively in your projects and take advantage of the benefits of storing data outside of your containers.

  • Docker and Wasm Containers – Better Together

    Docker and Wasm Containers – Better Together

    Learn how Docker Desktop and CLI both manages Linux containers and Wasm containers side by side.

    Read More

  • Excel for Small Businesses: Practical Guides and Tips

    Excel for Small Businesses: Practical Guides and Tips

    Discover how Microsoft Excel can elevate your small business operations with practical tips for financial management, inventory tracking, data analysis, customer tracking, and project management. Unlock the full potential of Excel to streamline your business processes and boost efficiency.

    Read More

  • Docker desktop – kubernetes failed to start

    Docker desktop – kubernetes failed to start

    Docker Desktop with Kubernetes integration is a powerful tool for developers. When you enable Kubernetes under Docker Desktop for the first time, it pulls these Docker images from the remote Kubernetes registry. You can view the images using the terminal as shown below: But sometimes, you might encounter an error message stating “Kubernetes failed to…

    Read More

  • docker-credential-desktop.exe executable file not found in $PATH using wsl2

    docker-credential-desktop.exe executable file not found in $PATH using wsl2

    The error “Docker-credential-desktop.exe executable file not found in $PATH using wsl2” usually occurs when Docker for Windows is not properly configured or accessible from the Windows Subsystem for Linux 2 (WSL2). This issue can disrupt Docker operations and authentication, affecting developers, DevOps engineers, and anyone who relies on Docker in their workflow. Here is a…

    Read More

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index