DEV Community

Subham Nandi
Subham Nandi

Posted on

Docker: Day 7

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:

  1. 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.
  2. Flexible Storage Location:

    • Unlike Docker volumes, bind mounts can be stored anywhere on the host system.
  3. Direct Modifications:

    • Both Docker containers and non-Docker processes on the host machine can modify files in the bind mount at any time.
  4. Unsupported in Dockerfile:

    • Bind mounts cannot be used within a Dockerfile. They are exclusively configured during the docker run command.

Good Use Cases for Bind Mounts

Bind mounts are especially useful in the following scenarios:

  1. Sharing Configuration Files:

    • Share configuration files from the host machine to one or more containers to ensure consistent application behavior.
  2. 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:

  1. 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
Enter fullscreen mode Exit fullscreen mode
  • --name nginx: Names the container nginx.
  • --mount type=bind: Specifies the type of mount as bind.
  • 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.
  1. Verify the Bind Mount:

    • Use docker inspect to confirm that the bind mount has been successfully created:
     docker inspect nginx
    
  • Check the Mounts section in the output to ensure the mapping between the host and container directories.
  1. 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.

Top comments (0)