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:
docker volume create my_volume
Running the container with the volume mounted:
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:
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
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
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:
docker volume create db_data
Running a Database Container with Volume Mounting:
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:
docker volume create config_data
Copying Config Files into the Volume:
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:
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:
docker volume create logs_data
Running a Container with Volume Mounting for Logs:
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:
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:
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.
GO TO FULL VERSION