• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Docker Compose Volume: Bind Mounts

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Published: 1/31/2024

About Terminus

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:

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

Run in Warp

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:

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

Run in Warp

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:

Thumbnail for

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:

 # 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

Run in Warp

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:

 volumes:
  - .:/app

Run in Warp

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:

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

Run in Warp

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:

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

Run in Warp

Using the long form syntax

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

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

Run in Warp

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:

 volumes:
  - ./app:/app:ro

Run in Warp

Or the read_only property in the long form syntax:

 volumes:
  - type: bind
    source: ./app
    target: /app
    read_only: true

Run in Warp

Written by

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Filed Under

Related Articles

Override the Container Entrypoint With docker run

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

Docker

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Use An .env File In Docker

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

Docker

Run SSH In Docker

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

Docker
Thumbnail for Gabriel ManricksGabriel Manricks

Launch MySQL Using Docker Compose

Learn how to launch a MySQL container in Docker Compose.

DockerSQL

Execute in a Docker Container

Learn how to execute one or multiple commands in a Docker container using the docker exec command.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Expose Docker Container Ports

Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.

Docker

Restart Containers In Docker Compose

Learn how to restart and rebuild one or more containers in Docker Compose.

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Output Logs in Docker Compose

Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose

Docker
Thumbnail for Razvan LudosanuRazvan Ludosanu

Rename A Docker Image

Learn how to rename Docker images locally and remotely using the docker tag command.

Docker

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null