5.1 The Idea of Bind Mounts
Mounting host directories into containers using bind mounts is a powerful Docker feature that lets containers directly use files and directories located on the host system. This is super useful for development, testing, and configuration when you need to use local files without copying them into the container. In this lecture, we'll go over how to use bind mounts, share examples, and cover best practices.
Bind mounts allow you to mount specific directories or files from the host system into containers. This is different from volumes, which are managed by Docker and stored in a special directory.
Advantages of using bind mounts
- Direct access to host files: Containers can directly use and modify files located on the host.
- Development convenience: Developers can modify files on the host and see those changes in the containers in real-time.
- Flexibility: Any directory on the host can be mounted into a container.
Command for mounting bind mounts
Bind mounts are mounted using the -v
or --mount
options when starting a container.
5.2 Examples of using the -v
parameter
1. Mounting a host directory into a container
The simplest example of mounting a host directory into a container:
docker run -d --name my_container -v /path/on/host:/path/in/container nginx
In this example, the directory /path/on/host
on the host is mounted into the directory /path/in/container
inside the nginx container.
2. Mounting a host file into a container
You can also mount individual files:
docker run -d --name my_container -v /path/on/host/config.conf:/etc/nginx/nginx.conf nginx
In this example, the configuration file config.conf
on the host is mounted as the configuration file nginx.conf
inside the container.
5.3 Examples of Using the --mount
Parameter
The --mount
parameter provides a more detailed way of mounting bind directories with the ability to specify different options.
1. Mounting a Directory Using --mount
docker run -d --name my_container --mount type=bind,source=/path/on/host,target=/path/in/container nginx
This example is similar to the previous example with -v
, but it uses a more detailed syntax.
2. Mounting in Read-Only Mode
docker run -d --name my_container --mount
type=bind,source=/path/on/host,target=/path/in/container,readonly nginx
In this example, the directory is mounted in read-only mode, which prevents changes to the files inside the container.
5.4 Practical examples
To develop a web application, you can mount the directory with source code on the host into the container.
Creating a directory with source code on the host:
mkdir -p ~/my_web_app
echo "Hello, World!" > ~/my_web_app/index.html
Running a container with directory mounting:
docker run -d --name web_dev -v ~/my_web_app:/usr/share/nginx/html nginx
Now any changes in the ~/my_web_app directory on the host will be immediately reflected in the container.
Example 2: Using configuration files
You can mount configuration files from a directory on the host into the container.
Creating a configuration file on the host:
echo "user www-data;" > ~/nginx.conf
echo "worker_processes auto;" >> ~/nginx.conf
Running a container with a configuration file mounted:
docker run -d --name nginx_custom -v ~/nginx.conf:/etc/nginx/nginx.conf nginx
In this example, the Nginx configuration file on the host is used inside the container.
5.5 Checking Mounted Directories and Files
You can check the content of mounted directories and files using the docker exec command to run commands inside the container.
Example
Checking the content of a directory:
docker exec -it my_container ls /path/in/container
Checking the content of a file:
docker exec -it my_container cat /path/in/container/config.conf
Deleting Bound Directories
Bound directories are not directly managed by Docker, so their deletion happens at the host filesystem level.
Example
Deleting a bound directory on the host:
rm -r /path/on/host
GO TO FULL VERSION