Doubt regarding the structure of the Docker-Compose.yml file

Asked

Viewed 584 times

2

I have the following file Docker-Compose.yml:

version: "3.3"
services:
  mysql:
    container_name: mysql
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: senha_root
      MYSQL_DATABASE: database_name
    command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
    volumes:
      - ./mysql/tmp:/var/lib/mysql
    restart: always
    ports:
      - 3306:3306

  web:
    container_name: web
    image: web_dev
    build:
      context: .
      dockerfile: Dockerfile-web
    volumes:
      - ./projeto/:/var/www
      - ./apache/:/etc/apache2/sites-enabled/
    working_dir: /var/www
    depends_on:
      - mysql
    links:
      - mysql
    restart: always
    ports:
      - 80:80
      - 3000:3000
      - 3001:3001

Reading some posts I noticed that it is usually added instruction Networks, getting this way:

version: "3.3"
services:
  mysql:
    container_name: mysql
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: senha_root
      MYSQL_DATABASE: database_name
    command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
    networks:
      - dev
    volumes:
      - ./mysql/tmp:/var/lib/mysql
    restart: always
    ports:
      - 3306:3306

  web:
    container_name: web
    image: web_dev
    build:
      context: .
      dockerfile: Dockerfile-web
    volumes:
      - ./projeto/:/var/www
      - ./apache/:/etc/apache2/sites-enabled/
    working_dir: /var/www
    networks:
      - dev
    depends_on:
      - mysql
    links:
      - mysql
    restart: always
    ports:
      - 80:80
      - 3000:3000
      - 3001:3001
  networks:
      dev:

From what I could understand, when declaring the Networks instruction as above, I am creating a new network interface, however, I did not understand what the real explanation and necessity of this, some explanation?

And for what reason usually repeats the word network at the end of the file, this is to expose the network?

2 answers

2


Usually you create networks in Docker when you want to better control how your containers will communicate with each other - which one you can chat with, use the host name instead of the IP etc. When we use Docker-Compose it automatically creates a network for the services contained there, but if you need to configure something, create more than one or even use one that already exists, you can use these network settings.

The reason you specify "twice" is that at each point in the file you are doing a different thing.

When you set "Networks" at the root level, you are creating and configuring the networks you intend to use. When you put inside the services you are specifying which networks the container will enter.

You can find more information on documentation of the Docker-Compose.

  • Ball show. That explanation took away the doubt I had. Thank you.

0

The files of the Docker Compose follow the following logic: 1) The files are analyzed in depth and not from top to bottom, this means that you can change the order of things, be it first Networks (global) and then (services) or the other way around, which is more common.

2) Higher levels effectively define things, in your network case, but so do volumes. At the highest level (root) you effectively build or reference a network or volume, so that only then can you use them in your services.

Although it may seem redundant, analyze the most complex constructions such as:

networks:

  minhanet1:
    external:true

  minhanet2:
    driver:bridge

  minhanet3:
    driver:overlay

  minhanet4:
    driver:overlay

  app_net:
    driver: bridge
    enable_ipv6: true
    ipam:
      driver: default
      config:
      - subnet: 172.16.238.0/24
      - subnet: 2001:3984:3989::/64

This is all described at the highest level, root, so once referenced or described, you can normally use in your services.

  • I edited the question to better clarify my doubt

Browser other questions tagged

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