Docker Compose Volume: Bind Mounts

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: January 31, 2024

The short answer

When working with Compose version 3, to mount a local directory into a Docker container (i.e. bind mount), you can use the volumes property in the compose.yaml file as follows:

Bash
services:
  <service_name>:
    image: <image_name>
    volumes:
      - <host_directory>:<container_directory>:<access_mode>

Where:

  • service\_name is the name of the service the volume will be mounted to.
  • image\_name is the name of the Docker image the container will be launched from.
  • host\_directory is the absolute or relative path of the bind mount on your local machine.
  • container\_directory is the absolute path of the file or directory within the container.
  • access\_mode is a comma separated list of options that determine the permissions of the bind mount.

For example:

Bash
services:
  backend:
    image: ubuntu
    volumes:
      - ./app:/app:ro

The above command mounts the read-only app directory in the current directory into the container at the /app path.

Note that if the specified directory doesn't exist on your local machine, Docker will automatically create it before starting the container.

You can learn more about mounting files into a Docker container without Compose by reading our article on bind mounts.

Easily retrieve this syntax using Warp AI

If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp AI feature:

Entering docker compose bind mount in the AI question input will prompt a human-readable step by step guide including code snippets.

Mounting files and directories

To mount a file or a directory into a container, you can either use its relative or absolute path.

For example:

Bash
# Mounts the `app` directory located in the root directory
volumes:
  - /app:/app
# Mounts the `app` directory located in the current directory
volumes:
  - ./app:/app
# Mounts the `app` directory located in the home directory
volumes:
  - ~/app:/app

Note that relative paths should always begin with . or ...

Mounting the current directory

To mount the current directory into the container at a specified path, you can use the dot syntax as follows:

Bash
volumes:
  - .:/app

To mount the current directory at the same path into the container, you can use the command substitution syntax with the pwd command as follows:

Bash
volumes:
  - "${PWD}:${PWD}"

Note that when using this syntax, Docker will automatically create all the intermediary directories starting from the root in order to preserve the directory structure.

Using multiple bind mounts

Compose allows you to mount multiple files and directories into a container by listing them one after the other as follows:

Bash
volumes:
  - ./src:/app
  - ./cache:/tmp/cache
  - ./configs:/etc/configs

Using the long form syntax

Alternatively, Compose offers a different syntax called the long form syntax for creating bind mounts:

Bash
volumes:
  - type: bind
    source: <host_path>
    target: <container_path>
    read_only: true

Note that, when creating bind mounts using the long syntax, the reference directory on the local host needs to be created beforehand as Docker won't automatically create it.

Using read-only bind mounts

By default, any modifications made by a container to the files and directories of a bind mount will be propagated back to the local machine (i.e. read-write mode).

To prevent that, you can define a bind mount as read-only, either using the ro option in the short form syntax:

Bash
volumes:
  - ./app:/app:ro

Or the read\_only property in the long form syntax:

Bash
volumes:
  - type: bind
    source: ./app
    target: /app
    read_only: true
Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Learning Docker (The Easy Way) Using LazyDocker & Warp

A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Remove a Docker Image

Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.

Override the Container Entrypoint With docker run

Learn how to override and customize the entrypoint of a Docker container using the docker run command.

The Dockerfile ARG Instruction

Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.

Start a Docker Container

Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.

Stop All Docker Containers

How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.

Set Docker Container Hostname

Learn how to set, change and match a docker container hostname.

How To Use An .env File In Docker Compose

Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.

Use An .env File In Docker

Learn how to write and use .env files in Docker to populate the environment of containers on startup.

Restart Docker Containers

Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.

Run Bash Shell In Docker

Start an interactive shell in Docker container