Doubt regarding file structure docker-compose.yml

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 usually the networks statement is added, thus:

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:

As I could understand, by declaring the networks statement as above, I am creating a new network interface, however, I did not understand what the actual explanation and need is, some explanation?

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

    
asked by anonymous 14.01.2018 / 15:15

2 answers

2

Generally you create nets in docker when you want to better control how your containers will communicate with each other - which you can talk to, use the hostname instead of 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 something different.

When you define "networks" at the root level, you are creating and configuring the networks you intend to use. When you put it inside the services you are specifying which networks the container will go into.

You can find more information about it in the docker-compose documentation . p>     

20.01.2018 / 02:26
0

The docker compose files follow the following logic: 1) 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 vice versa, which is more common. >

2) The 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 a volume, so that you can then use them only in your services.

While it may seem redundant, look at more complex constructs 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, so root, once referenced or described, you can usually use in your services.

    
15.01.2018 / 14:10