Featured Video Play Icon

Docker Volume vs. Mount

Docker is a popular tool for containerizing applications and making them portable across different systems. Docker allows you to package an application and its dependencies into a single container, making it easy to deploy and run the application on different environments. When using Docker, you might come across two concepts: Docker volume vs Docker mount. While both can be used to share data between the host machine and a container, they work differently and have different use cases.

Lead Developer Career Guide Banner 01

Docker Volumes

Docker volumes are a type of data storage provided by Docker. A volume is a directory on the host machine that is accessible to a container. When you use a Docker volume, the data inside it is not stored in the container’s filesystem but in the host machine’s filesystem. This means that data in a volume persists even if the container is deleted or recreated.

Volumes are a good option for persisting data across different containers. For example, if you have a container that writes logs or a database that needs to store data, you can use volumes to store the data outside the container. This way, you can destroy and recreate the container without losing data.

Here is an example of using a volume with Docker:

docker run -v /my/volume:/app/data my-image

In this example, /my/volume is a directory on the host machine that will be mounted as /app/data inside the container. Any data written to /app/data inside the container will be stored in /my/volume on the host machine.

Docker Mounts

Docker mounts are another way to share data between the host machine and a container. When you use a Docker mount, you specify a file or directory on the host machine that should be mounted as a directory inside the container.

Unlike Docker volumes, the data inside a mount is not stored outside the container. If you delete the container, the data in the mount will be lost. However, mounts are useful when you need to share code or configuration files between the host machine and a container during development or testing.

Here is an example of using a mount with Docker:

docker run -v /path/on/host:/app/code my-image

In this example, /path/on/host is a directory on the host machine that will be mounted as /app/code inside the container. Any changes made to files in /path/on/host will be immediately reflected inside the container.

Use Cases for Docker Volumes and Mounts

Now that you know the differences between Docker volumes and mounts, let’s look at some common use cases for each.

Use cases for Docker volumes:

  • Storing application data that needs to persist even if the container is destroyed or recreated.
  • Sharing data between different containers.
  • Backing up data.

Use cases for Docker mounts:

  • Developing and testing applications locally.
  • Sharing code or configuration files between the host machine and a container.
  • Debugging applications by accessing log files or other debugging information from the container.

Docker volumes and mounts are two different ways to share data between the host machine and a container. Volumes are ideal for persisting data, while mounts are useful for development and testing purposes. Knowing the differences between volumes and mounts can help you choose the right method for your specific use case.

Docker Volume vs. Mount – Online Course

Tactics and Tools for Troubleshooting Docker on Pluralsight
Tactics and Tools for Troubleshooting Docker – Online Course Available on Pluralsight

If you’re struggling with Docker errors, check out my Pluralsight course, Tactics and Tools for Troubleshooting Docker. This course provides developers with the skills they need to troubleshoot and fix the most common Docker errors. By the end of this course, you will have the skills you need to reduce the time it takes you to assess and fix issues. Click the link below to get started with a free trial today!