0
0
Dockerdevops~5 mins

Bind mounts for development in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you develop software, you often want your code changes to appear immediately inside a running container without rebuilding it. Bind mounts let you link a folder on your computer directly into the container, so changes happen live.
When you want to edit code on your computer and see the changes instantly inside a container.
When you need to share configuration files between your host and a container during development.
When you want to avoid rebuilding a container image every time you change source files.
When you want to debug or test code inside a container using your local tools.
When you want to keep your source code outside the container for easier version control.
Commands
This command runs an Nginx container in the background named 'my-dev-container'. The '-v' flag creates a bind mount that links your local folder '/home/user/myapp' to '/app' inside the container. This means any changes you make in '/home/user/myapp' appear immediately inside the container at '/app'.
Terminal
docker run -d --name my-dev-container -v /home/user/myapp:/app nginx:1.25
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
-d - Run container in detached (background) mode
--name - Assign a custom name to the container
-v - Create a bind mount between host and container directories
This command lists the files inside the '/app' directory of the running container 'my-dev-container'. It helps verify that your local files are visible inside the container through the bind mount.
Terminal
docker exec -it my-dev-container ls /app
Expected OutputExpected
index.html styles.css app.js
-it - Run the command interactively with a terminal
Stops the running container named 'my-dev-container'. This is useful when you finish your development session.
Terminal
docker stop my-dev-container
Expected OutputExpected
my-dev-container
Removes the stopped container named 'my-dev-container' to clean up your system.
Terminal
docker rm my-dev-container
Expected OutputExpected
my-dev-container
Key Concept

If you remember nothing else from this pattern, remember: bind mounts link your local folder directly into the container so changes happen live without rebuilding.

Common Mistakes
Using a relative path instead of an absolute path for the bind mount source.
Docker requires an absolute path on the host for bind mounts; relative paths cause errors or unexpected behavior.
Always specify the full absolute path to your local folder, like '/home/user/myapp'.
Not having the local folder exist before running the container with a bind mount.
If the local folder does not exist, Docker may create an empty folder or fail, leading to missing files inside the container.
Create the local folder and add your files before running the container with the bind mount.
Binding a folder with incorrect permissions causing the container to not read or write files.
If the container user lacks permission to access the bind mount folder, your app may fail or behave unexpectedly.
Ensure the folder permissions on your host allow the container user to read/write as needed.
Summary
Use the '-v' flag with 'docker run' to create a bind mount linking a local folder to a container folder.
Verify the bind mount works by listing files inside the container with 'docker exec'.
Stop and remove containers when done to keep your system clean.