CodeGym /Courses /Docker SELF /Using Binds

Using Binds

Docker SELF
Level 19 , Lesson 4
Available

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

  1. Direct access to host files: Containers can directly use and modify files located on the host.
  2. Development convenience: Developers can modify files on the host and see those changes in the containers in real-time.
  3. 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:

Terminal

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:

Terminal

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

Terminal

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

Terminal

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:

Terminal

mkdir -p ~/my_web_app
echo "Hello, World!" > ~/my_web_app/index.html

Running a container with directory mounting:

Terminal

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:

Terminal

echo "user www-data;" > ~/nginx.conf

echo "worker_processes auto;" >> ~/nginx.conf

Running a container with a configuration file mounted:

Terminal

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:

Terminal

docker exec -it my_container ls /path/in/container

Checking the content of a file:

Terminal

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:

Terminal

rm -r /path/on/host
3
Task
Docker SELF, level 19, lesson 4
Locked
Mounting a directory for web application development
Mounting a directory for web application development
3
Task
Docker SELF, level 19, lesson 4
Locked
Mounting a configuration file
Mounting a configuration file
3
Task
Docker SELF, level 19, lesson 4
Locked
Mounting a directory using the --mount parameter
Mounting a directory using the --mount parameter
3
Task
Docker SELF, level 19, lesson 4
Locked
Checking the contents of mounted directories
Checking the contents of mounted directories
1
Опрос
Data Management in Docker,  19 уровень,  4 лекция
недоступен
Data Management in Docker
Data Management in Docker
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION