What Are Bind Mounts?
A bind mount is a powerful feature of Docker that allows a file or directory on the host machine to be mounted into a container. This enables seamless interaction between the host system and the Docker container. Below are some key characteristics of bind mounts:
-
Mapping Host Files to Container Files:
- Bind mounts allow specific files or directories from the host machine to be mapped to the corresponding files or directories in the container.
-
Flexible Storage Location:
- Unlike Docker volumes, bind mounts can be stored anywhere on the host system.
-
Direct Modifications:
- Both Docker containers and non-Docker processes on the host machine can modify files in the bind mount at any time.
-
Unsupported in Dockerfile:
- Bind mounts cannot be used within a Dockerfile. They are exclusively configured during the
docker run
command.
- Bind mounts cannot be used within a Dockerfile. They are exclusively configured during the
Good Use Cases for Bind Mounts
Bind mounts are especially useful in the following scenarios:
-
Sharing Configuration Files:
- Share configuration files from the host machine to one or more containers to ensure consistent application behavior.
-
Development and Testing Environments:
- Share source code or build artifacts between a development environment on the Docker host and the container. This allows developers to edit files on the host machine and see changes reflected in real time within the container.
Starting NGINX with a Bind Mount
To understand bind mounts in action, let’s use a practical example by starting an NGINX container with a bind mount. This will demonstrate how to map a directory from the host system to the container.
Steps to Start NGINX with a Bind Mount:
-
Run the NGINX Container:
Use the following Docker command to start an NGINX container and bind mount the current directory (
$(pwd)
) to/app
in the container:
docker container run -d --name nginx --mount type=bind,source=$(pwd),target=/app nginx
-
--name nginx
: Names the containernginx
. -
--mount type=bind
: Specifies the type of mount asbind
. -
source=$(pwd)
: Defines the source directory on the host machine as the current working directory. -
target=/app
: Maps the source directory to/app
inside the container.
-
Verify the Bind Mount:
- Use
docker inspect
to confirm that the bind mount has been successfully created:
docker inspect nginx
- Use
- Check the
Mounts
section in the output to ensure the mapping between the host and container directories.
-
Interact with the Mounted Directory:
- Any changes made to the files in the host directory will immediately reflect inside the container under the
/app
directory, and vice versa.
- Any changes made to the files in the host directory will immediately reflect inside the container under the
Top comments (0)