Working with Bind Mounts in Docker

Sandhya BabuSandhya Babu
4 min read

Overview:

In this blog, you'll learn the fundamentals of bind mounts in Docker, and how to use them effectively through practical lab exercises

What is a Bind Mount?

  • A bind mount allows you to link a directory or file from your host machine to a Docker container. Any changes made in one are immediately reflected in the other.

Key Features of Bind Mounts:

  1. Direct File Access: Work directly with host files from within the container, with changes being reflected instantly on both sides.

  2. Flexibility: Share files like code, configurations, and logs between the host and containers.

Bind Mount vs. Volume:

  • Bind Mounts: Map specific directories or files on the host to containers; not managed by Docker.

  • Volumes: Managed by Docker and better for data persistence across container restarts.

When to Use Bind Mounts:

  • Development: Reflect code changes on the host instantly in a container.

  • Configuration Management: Share configuration files between the host and container.

  • Log Access: Access logs created by the container directly on the host

Docker Volume vs Bind Mounts vs tmpfs mount - Digital Varys

Exercise 1: Starting Docker Containers with Bind Mounts

We’ll explore how to share data between the host and multiple containers using bind mounts.

1. Create a Shared Directory

Creates a shared directory on the host that will be accessible from the container.

mkdir /home/ubuntu/share

2. Add a File to the Shared Directory

Creates an index.html file with a message in the shared directory.

echo 'Hello From Docker Host' > /home/ubuntu/share/index.html
  1. Run the First Container (container1)

Starts container1 with a bind mount. The host's /home/ubuntu/share is mapped to the container's /var/www/html.

docker run -it --name container1 -p 80:80 -v /home/ubuntu/share:/var/www/html ubuntu:18.04 /bin/bash

4. Install and Start Apache in container1 (Set up Apache web server)

Updates packages and installs the Apache web server inside the container

apt-get update && apt-get install apache2 -y

Starts the Apache server and checks the status of Apache server.

service apache2 start
service apache2 status

  1. Modify the Shared File in container1

Overwrites the index.html file from within container1, which also updates the file on the host

echo 'Hello From Container1' > /var/www/html/index.html

Exit the Container and Keep It Running

Press Ctrl+P+Q to detach from the container without stopping it.

  1. Run the Second Container (container2)

Starts container2 with the same bind mount.

docker run -it --name container2 -v /home/ubuntu/share:/var/www/html ubuntu:18.04
  1. Modify the Shared File in container2

Updates index.html again from container2. The change is reflected in the host directory and container1.

echo 'Hello From Container2' > /var/www/html/index.html 
exit

List and Remove the Containers

Lists all containers and then forcefully removes both containers.

docker ps -a
docker rm -vf container1 container2

Summary:

  • Bind Mount Basics: Share and modify data between the host and containers.

  • Data Persistence: The data persists even after removing the containers.

  • Shared Data Across Containers: Multiple containers can share and modify the same data.

This task helps you learn how Docker uses bind mounts for persistent and shared data storage across containers.

Exercise 2: Creating a Bind Mount Using the --mount Option

  1. Run an Nginx Container with Bind Mount:
docker run -d --name newbind01 --mount type=bind,source=/home/ubuntu/share/,target=/app nginx:latest

Starts an Nginx container named newbind01 with a bind mount, mapping /home/ubuntu/share on the host to /app in the container

  1. Verify the Bind Mount in the Container:
docker inspect newbind01 | grep -i /app

Confirm that the bind mount exists by inspecting the container's configuration.

Summary:

  • Bind Mount with --mount Option: Demonstrates another way to create a bind mount.

  • Verify Bind Mounts: Shows how to confirm the bind mount using docker inspect

Try setting up your own bind mounts and explore how they can simplify data sharing in Docker containers. Stay tuned for more Docker tutorials!

1
Subscribe to my newsletter

Read articles from Sandhya Babu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sandhya Babu
Sandhya Babu

🌟 Aspiring DevOps Engineer | Cloud & DevOps Enthusiast🌟 Hello! I’m Sandhya Babu, deeply passionate about DevOps and cloud technologies. Currently in my exploring phase, I’m learning something new every day, from tools like Jenkins, Docker, and Kubernetes to the concepts that drive modern tech infrastructures. I have hands-on experience with several Proof of Concept (POC) projects, where I've applied my skills in real-world scenarios. I love writing blogs about what I've learned and sharing my experiences with others, hoping to inspire and connect with fellow learners. With certifications in Azure DevOps and AWS SAA-C03, I’m actively seeking opportunities to apply my knowledge, contribute to exciting projects, and continue growing in the tech industry. Let’s connect and explore the world of DevOps together!