Use Bind Mounts Docker Docs
Use Bind Mounts Docker Docs
com/get-started/06_bind_mounts/
Guides / Get started / Getting started guide / Part 6: Use bind mounts
In part 5, you used a volume mount to persist the data in your database. A volume mount is
a great choice when you need somewhere persistent to store your application data.
A bind mount is another type of mount, which lets you share a directory from the host's
�lesystem into the container. When working on an application, you can use a bind mount to
mount source code into the container. The container sees the changes you make to the
code immediately, as soon as you save a �le. This means that you can run processes in the
container that watch for �lesystem changes and respond to them.
Give feedback
In this chapter, you'll see how you can use bind mounts and a tool called nodemon to
watch for �le changes, and then restart the application automatically. There are equivalent
tools in most other languages and frameworks.
Populates Yes No
new
1 of 7 2/26/24, 12:29
Use bind mounts | Docker Docs https://docs.docker.com/get-started/06_bind_mounts/
volume
with
container
contents
Supports Yes No
Volume
Drivers
Give feedback
If you use Windows and want to use Git Bash to run Docker commands, see Working
with Git Bash for syntax di�erences.
3. Run the following command to start bash in an ubuntu container with a bind mount.
The --mount option tells Docker to create a bind mount, where src is the current
working directory on your host machine ( getting-started-app ), and target is
2 of 7 2/26/24, 12:29
Use bind mounts | Docker Docs https://docs.docker.com/get-started/06_bind_mounts/
4. After running the command, Docker starts an interactive bash session in the root
directory of the container's �lesystem.
root@ac1237fad8db:/# pwd
/
root@ac1237fad8db:/# ls
bin dev home media opt root sbin srv tmp var
boot etc lib mnt proc run src sys usr
This is the directory that you mounted when starting the container. Listing the contents
of this directory displays the same �les as in the getting-started-app directory on
your host machine.
Give feedback
root@ac1237fad8db:/# cd src
root@ac1237fad8db:/src# ls
Dockerfile node_modules package.json spec src yarn.lock
7. Open the getting-started-app directory on the host and observe that the
myfile.txt �le is in the directory.
├── getting-started-app/
│ ├── Dockerfile
│ ├── myfile.txt
│ ├── node_modules/
│ ├── package.json
│ ├── spec/
│ ├── src/
3 of 7 2/26/24, 12:29
Use bind mounts | Docker Docs https://docs.docker.com/get-started/06_bind_mounts/
│ └── yarn.lock
9. In the container, list the contents of the app directory once more. Observe that the �le
is now gone.
root@ac1237fad8db:/src# ls
Dockerfile node_modules package.json spec src yarn.lock
That's all for a brief introduction to bind mounts. This procedure demonstrated how �les are
shared between the host and the container, and how changes are immediately re�ected on
both sides. Now you can use bind mounts to develop software.
Development containers
Give feedback
Using bind mounts is common for local development setups. The advantage is that the
development machine doesn’t need to have all of the build tools and environments installed.
With a single docker run command, Docker pulls dependencies and tools.
You can use the CLI or Docker Desktop to run your container with a bind mount.
4 of 7 2/26/24, 12:29
Use bind mounts | Docker Docs https://docs.docker.com/get-started/06_bind_mounts/
1. Make sure you don't have any getting-started containers currently running.
• -w /app - sets the "working directory" or the current directory that the command
Give feedback
directory from the host into the /app directory in the container
• node:18-alpine - the image to use. Note that this is the base image for your app
• sh -c "yarn install && yarn run dev" - the command. You're starting a shell
using sh (alpine doesn't have bash ) and running yarn install to install
packages and then running yarn run dev to start the development server. If you
look in the package.json , you'll see that the dev script starts nodemon .
3. You can watch the logs using docker logs <container-id> . You'll know you're ready
to go when you see this:
5 of 7 2/26/24, 12:29
Use bind mounts | Docker Docs https://docs.docker.com/get-started/06_bind_mounts/
When you're done watching the logs, exit out by hitting Ctrl + C .
Give feedback
1. In the src/static/js/app.js �le, on line 109, change the "Add Item" button to simply
say "Add":
2. Refresh the page in your web browser, and you should see the change re�ected almost
immediately because of the bind mount. Nodemon detects the change and restarts the
server. It might take a few seconds for the Node server to restart. If you get an error, try
refreshing after a few seconds.
6 of 7 2/26/24, 12:29
Use bind mounts | Docker Docs https://docs.docker.com/get-started/06_bind_mounts/
3. Feel free to make any other changes you'd like to make. Each time you make a change
and save a �le, the change is re�ected in the container because of the bind mount.
When Nodemon detects a change, it restarts the app inside the container automatically.
When you're done, stop the container and build your new image using:
Summary
At this point, you can persist your database and see changes in your app as you develop
without rebuilding the image.
In addition to volume mounts and bind mounts, Docker also supports other mount types and
storage drivers for handling more complex and specialized use cases.
Related information:
Give feedback
docker CLI reference
7 of 7 2/26/24, 12:29