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.
Sometimes, you may want to access a directory from your local filesystem. To do this, you can use bind mounts.
Before you start, get Docker Desktop.
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.
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.
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
volumes element tells Compose to mount the local folder
/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.
In a terminal, run the follow commands to bring up your application. Replace
/path/to/bindmount-apps/ with the path to your application's directory.
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
Terminalin the search field, then select Terminal.
$ cd /path/to/bindmount-apps/
$ docker compose up -d
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 an code or text editor, update the
Enter your task string, and save the file. Visit or refresh
localhost:3001open_in_new to view the changes.
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.
- Deep dive into bind mounts
- Learn about using bind mounts in Compose in the Compose file reference
- Explore using bind mounts via the CLI in the Docker run reference
Continue to the next walkthrough to learn how you can containerize your own application.