How to Mount a Host Directory as a Volume in Docker Compose

Avatar

By squashlabs, Last Updated: Oct. 22, 2023

How to Mount a Host Directory as a Volume in Docker Compose

Mounting a host directory as a volume in Docker Compose allows you to share data between the host machine and a container. This is useful for persisting data, sharing configuration files, or enabling live code reloading during development. In this guide, we will explore two methods for mounting a host directory as a volume in Docker Compose.

Method 1: Using Relative Paths

1. Create a docker-compose.yaml file in your project directory. This file defines the services, networks, and volumes for your Docker Compose configuration.

2. Define the volumes section in the docker-compose.yaml file. This section specifies the volumes to be mounted in the containers. For example:

version: '3'
services:
  web:
    image: nginx:latest
    volumes:
      - ./data:/var/www/html

In the above example, we are mounting the ./data directory on the host machine to the /var/www/html directory inside the container.

3. Start the containers using the docker-compose up command. Docker Compose will create the necessary volume and mount the host directory to the specified path in the container.

4. Verify that the volume is mounted correctly by accessing the container and checking the contents of the mounted directory. For example, you can use the following command to access the container:

docker-compose exec web bash

Once inside the container, navigate to the mounted directory and verify the presence of the files from the host machine.

Related Article: How to Run a Docker Instance from a Dockerfile

Method 2: Using Absolute Paths

Alternatively, you can use absolute paths to mount a host directory as a volume in Docker Compose.

1. Determine the absolute path of the directory you want to mount on the host machine.

2. Update the volumes section in the docker-compose.yaml file to use the absolute path. For example:

version: '3'
services:
  web:
    image: nginx:latest
    volumes:
      - /path/to/host/data:/var/www/html

In the above example, replace /path/to/host/data with the absolute path of the directory on your host machine.

3. Start the containers using the docker-compose up command. Docker Compose will create the necessary volume and mount the host directory to the specified path in the container.

4. Verify that the volume is mounted correctly by accessing the container and checking the contents of the mounted directory. For example, you can use the following command to access the container:

docker-compose exec web bash

Once inside the container, navigate to the mounted directory and verify the presence of the files from the host machine.

Best Practices

Related Article: How to Use the Host Network in Docker Compose

- Use relative paths whenever possible to ensure your Docker Compose configuration is portable across different host machines.

- Avoid hardcoding absolute paths in your Docker Compose configuration, as they may not work on different host machines or when deploying to different environments.

- When using relative paths, ensure that the host directory you want to mount is located relative to the directory where the docker-compose.yaml file is located.

- Consider using environment variables or Docker secrets to dynamically configure the host directory path in your Docker Compose configuration. This allows for more flexibility and avoids hardcoding sensitive information.

- Regularly backup your host directories to prevent data loss in case of container or host machine failures.

You May Also Like

Comparing Kubernetes vs Docker

Get a clear understanding of the differences between Kubernetes and Docker. Learn how they differ in terms of functionality, scalability, and archite… read more

Quick Docker Cheat Sheet: Simplify Containerization

Simplify containerization with a concise Docker cheat sheet. Learn essential commands and tips to efficiently manage Docker containers. From getting … read more

How to Install and Use Docker

The article provides a practical guide to installing and using Docker, a popular containerization platform. It highlights the advantages of Docker, s… read more

Copying a Directory to Another Using the Docker Add Command

Copying directories in Docker using the Add command is a process that can be done in just a few steps. This article provides a step-by-step guide to … read more

How to Secure Docker Containers

Learn how to secure your Docker containers with practical steps to protect your applications and data. From understanding container security to imple… read more

How to Stop and Remove All Docker Containers

A guide on how to stop and remove all Docker containers, including step-by-step instructions for each process. Learn how to list running containers, … read more

Tutorial: Managing Docker Secrets

Managing secrets in Docker is essential for maintaining security in your applications. This tutorial provides a comprehensive guide to managing Docke… read more

How to Copy Files From Host to Docker Container

Transferring files from a host to a Docker container can be a simple task with the docker cp command. This article provides a step-by-step guide on h… read more

Build a Chat Web App with Flask, MongoDB, Reactjs & Docker

Building a chat web app with Flask, MongoDB, Reactjs, Bootstrap, and Docker-compose is made easy with this comprehensive guide. From setting up the d… read more

Docker How-To: Workdir, Run Command, Env Variables

Learn useful tips for Docker, including setting the workdir, using the run command, and managing environment variables. Chapters cover getting starte… read more