Doubt about marking "volumes" inside the Docker-Compose.yml

Asked

Viewed 2,564 times

4

The Docker-Compose.yml below has been taken from the Docker documentation itself. My question is: why inside the db container we have the reference of volumes and at the end of the file the same reference is repeated? What does it mean?

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress

volumes:
    db_data:

2 answers

2


The db_data is a reference to a volume defined within your docker-compose.yml. Within the service db it is indicating that you should save the data to an internal volume managed by Docker (and not to a path mapped to your disk).

   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql

At the end of the file we have the declaration of this volume, where you can indicate where the reference of the db_data should point. In the file is the reduced declaration, but that end can also be defined as follows:

volumes:
    db_data:
        driver: local

It is possible to define extra options depending on the case:

volumes:
    db_data:
        driver: local
        name: my_volume_name
        external: true

See the documentation to see possible options.

Remember that this reference is not the same used for the volume name for Docker. For this we have the property name, otherwise this volume would follow the convention <path_do_projeto>_<referencia_do_volume>.

Example:

> docker volume ls
DRIVER              VOLUME NAME   
local               wordpress_db_data

With name defined:

> docker volume ls
DRIVER              VOLUME NAME   
local               my_volume_name

0

While it is common to leave this information at the end, the order really is indifferent. The main elements (which are at the root of yaml) Networks and Volumes are used to specify resources, respectively networks and volumes.

This way of structuring reaffirms the security that you will only use volumes or networks you previously defined. The definition of these is global for your yaml.

Only after specified these can be used in services/containers. This model is therefore given beyond the name (as we see in the case of volume) we can define driver and other information, in counterpart we can reuse the same volume in several containers.

  • So this volume marking in the global scope is a definition, that is, a marking that the volume will be used. Right? This global marking is optional?

  • It’s not just an appointment, it’s the definition. Which may even be an external volume (which is not being created in your Compose, but was previously created).

  • And it’s not optional

Browser other questions tagged

You are not signed in. Login or sign up in order to post.