Chat Zalo Chat Messenger Phone Number Đăng nhập
Using volumes in Docker Compose - DevOps Cell

Using volumes in Docker Compose – DevOps Cell

<img src="https://devopscell.com/images/like-docker-compose-volumes.jpg" alt="Volume Examples

in Docker Compose” />

Purpose

The purpose of this post is to review how we can use volumes in Docker and Docker Compose. Here are some possible scenarios:

Use one/multiple volumes per service/container. Use one/more volumes

  • per set of services (defined in the same docker-compose.yml file).
  • Use one or more volumes in your
  • Docker installation

  • .

Before you begin

In this tutorial, we’ll learn how to use Docker Compose volumes. A GNU Linux/Mac OS/Windows machine with Docker and Docker Compose installed is required to follow this tutorial.

How

to declare volumes in Docker

There are two ways to declare volumes in Docker: the imperative form (Docker client) and the declarative form (Docker Compose Yaml file or Docker Dockerfile).

In this post you will only see how to do it declaratively using a docker-compose file. But it’s worth mentioning that it’s also possible to declare volumes in Docker using their command-line client:

Volume Types in Docker

1. Docker host-mounted volumes

Syntax

: /host/path:/container/path

The host path can be defined as an absolute or relative path

.

Example:

2.

Docker named volumes

Syntax: named_volume_name:/container/path

Named volumes can be defined as internal (default) or external

.

2.1

. Internal Docker Named Volumes Internal Docker compose named

volumes have the scope of a single Docker-compose file and are created by Docker if they do not exist

. Example of a Docker Compose file with a named volume web_data:

TIP 1: Starting with Docker Compose version 3.4, the volume name can be dynamically generated from environment variables placed in an .env file (this file must be in the same folder as docker-compose.yml).

TIP 2: To increase security on our system, we can mount the volume as read-only if the container only needs to read the mounted files. This will prevent an attacker from modifying or creating new files on the server host, for example.

Example .

env: Example

of a

Docker Compose file with an internal docker named volume based on an environment variable: docker-compose

up will generate a volume named my_volume_001

. 2.2

. External Docker named volumes

Docker

composition external named volumes can be used throughout the Docker installation and must be created by the user (otherwise, an error occurs) using the docker volume create command

.

Example

: Define web_data volume:

docker-compose.yml file with an externally defined web_data

named volume:

There are different volume types like nfs, btrfs, ext3, ext4 and also third-party plugins to create volumes

.

External named volumes can be dynamically defined from environment variables using a name section, as we did in the previous example

.

3. Share

volumes

Syntax: -volumes-from container_name

We can start a new container using volumes defined in another. Similar to -v or -volume but without having to define a volume or mount paths.

Note: -volumes-from makes sense if we are using only Docker. For Docker-compose we can use top-level volumes as we did in the previous section and make them available to more than one service. Application and application web_data sharing example2:

Remove

Docker volumes

If you followed this tutorial, you might have many Docker populated volumes. If you need to delete them, you can use the following post to delete existing Docker volumes running on your system.

One

of the

main benefits of using Docker volumes is the ability to change the contents/settings of a container without the need to recreate it

.

You should keep in mind that if the contents of a container will never change it’s probably best to copy the content once you’re building your Docker image.

Finally, if you need to provide changes to a container that has no volumes attached and cannot be recreated, there is always the option to copy files directly to a running container.

Recommended books to expand your Docker knowledge:

Contact US