Access a local folder from a container

This walkthrough shows you how to access a local folder from a container. To better understand some concepts in this walkthrough, complete the Run multi-container applications walkthrough first.

Docker isolates all content, code, and data in a container from your local filesystem. By default, containers can't access directories in your local filesystem.

Data isolation diagram

Sometimes, you may want to access a directory from your local filesystem. To do this, you can use bind mounts.

Before you start, get the latest version of Docker Desktop. Docker adds new features regularly and some parts of this guide may work only with the latest version of Docker Desktop.

Step 1: Get the sample application

If you have git, you can clone the repository for the sample application. Otherwise, you can download the sample application. Choose one of the following options.


Use the following command in a terminal to clone the sample application repository.

$ git clone https://github.com/docker/bindmount-apps

Download the source and extract it.


Step 2: Add a bind mount using Compose

Add a bind mount to access data on your system from a container. A bind mount lets you share a directory from your host's filesystem into the container.

Bind mount diagram

To add a bind mount to this project, open the compose.yaml file in a code or text editor, and then uncomment the following lines.

todo-app:
    # ...
    volumes:
      - ./app:/usr/src/app
      - /usr/src/app/node_modules

The volumes element tells Compose to mount the local folder ./app to /usr/src/app in the container for the todo-app service. This particular bind mount overwrites the static contents of the /usr/src/app directory in the container and creates what is known as a development container. The second instruction, /usr/src/app/node_modules, prevents the bind mount from overwriting the container's node_modules directory to preserve the packages installed in the container.

Step 3: Run the application

In a terminal, run the following commands to bring up your application. Replace /path/to/bindmount-apps/ with the path to your application's directory.

Tip

To run Docker commands, you must use a terminal. Based on your operating system, you can open a terminal by doing the following:

For Windows, select the Start Menu, specify cmd, and then select Command Prompt.

For Mac, select the Launchpad icon in the Dock, specify Terminal in the search field, then select Terminal.

$ cd /path/to/bindmount-apps/
$ docker compose up -d

Step 4: Develop the application

Now, you can take advantage of the container’s environment while you develop the application on your local system. Any changes you make to the application on your local system are reflected in the container. In your local directory, open app/views/todos.ejs in a code or text editor, update the Enter your task string, and save the file. Visit or refresh localhost:3001⁠ to view the changes.

Summary

In this walkthrough, you added a bind mount to access a local folder from a container. You can use this to develop faster without having to rebuild your container when updating your code.

Related information:

Next steps

Continue to the next walkthrough to learn how you can containerize your own application.