CodeGym /Courses /Docker SELF /Mounting volumes into containers

Mounting volumes into containers

Docker SELF
Level 19 , Lesson 2
Available

3.1 Basics of Mounting Volumes

Mounting volumes into containers is one of the key features of Docker that lets you save data outside of containers, ensuring its long-term storage and accessibility. Volumes can be used for various purposes, including storing configuration files, databases, logs, and other important data. In this lecture, we'll look into how to mount volumes into containers and provide practical examples.

Basics of Mounting Volumes

Volumes, as we already mentioned, exist independently from the lifecycle of containers. They are stored in a special directory on the host machine and can be attached to any container.

Command for Mounting Volumes

Volumes are mounted into containers using the -v or --mount options when starting a container.

3.2 Parameter -v

Examples of using the -v parameter

1. Mounting a volume into a container

Creating a volume and mounting it into a container:

Creating a volume:

Terminal

docker volume create my_volume

Running the container with the volume mounted:

Terminal

docker run -d --name my_container -v my_volume:/app nginx

In this example, the volume my_volume is mounted into the directory /app of the container my_container

2. Mounting multiple volumes

You can mount multiple volumes into a single container:

Terminal

docker run -d --name my_container -v volume1:/app/data -v volume2:/app/logs nginx

In this example, the volume volume1 is mounted into the directory /app/data, and the volume volume2 — into the directory /app/logs.

3.3 The --mount Parameter

The --mount parameter provides a more detailed way of mounting volumes, with an option to specify different parameters.

1. Mounting a volume using --mount

Terminal

docker run -d --name my_container --mount source=my_volume,target=/app nginx

This example is similar to the previous example with -v, but uses a more detailed syntax.

2. Mounting with additional parameters

Terminal

docker run -d --name my_container --mount source=my_volume,target=/app,readonly nginx

In this example, the volume is mounted in read-only mode (readonly).

3.4 Practical Examples

Example 1: Using Volumes for a Database

Creating and using a volume to store database data.

Creating a Volume:

Terminal

docker volume create db_data

Running a Database Container with Volume Mounting:

Terminal

docker run -d --name postgres -e POSTGRES_PASSWORD=mysecretpassword -v 
db_data:/var/lib/postgresql/data postgres

In this example, PostgreSQL database data is stored in the volume db_data.

Example 2: Using Volumes for Config Files

Creating a volume to store config files and mounting it into a container.

Creating a Volume:

Terminal

docker volume create config_data

Copying Config Files into the Volume:

Terminal

docker run --rm -v config_data:/app busybox sh -c "echo 'configuration data' > /app/config.txt"

Running a Container Using the Volume for Config Files:

Terminal

docker run -d --name my_app -v config_data:/app/config nginx

In this example, config files are stored in the volume config_data, which is mounted to the /app/config directory in the container.

Example 3: Container Logs

Creating a volume to store container logs and mounting it into the container.

Creating a Volume:

Terminal

docker volume create logs_data

Running a Container with Volume Mounting for Logs:

Terminal

docker run -d --name my_app -v logs_data:/var/log/nginx nginx

In this example, Nginx logs are stored in the volume logs_data.

3.5 Checking the Contents of Volumes

You can check the contents of a volume by mounting it into a temporary container.

Example

Starting a temporary container with volume mounting:

Terminal

docker run --rm -v my_volume:/app busybox ls /app

This example will show the contents of the my_volume volume.

Deleting Volumes

Deleting a volume that is no longer in use.

Example

Deleting a volume:

Terminal

docker volume rm my_volume

If the volume is being used by a container, Docker will throw an error. Make sure the containers using the volume are stopped and removed.

3
Task
Docker SELF, level 19, lesson 2
Locked
Mounting a volume into a container
Mounting a volume into a container
3
Task
Docker SELF, level 19, lesson 2
Locked
Mounting Multiple Volumes
Mounting Multiple Volumes
3
Task
Docker SELF, level 19, lesson 2
Locked
Mounting a volume with the --mount parameter
Mounting a volume with the --mount parameter
3
Task
Docker SELF, level 19, lesson 2
Locked
Checking the volume contents
Checking the volume contents
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION