How to assign retrieve the ip of a service inside the docker-composer?

1

I am trying to get zabbix in docker, I created a docker-compose with several services, one of them is the database. I need to raise the database first and then get the ip of the database and set the environment variable for other services, however I do not know how to do it, I already tried to use the links, but I'm not having success.

This is my docker-compose.yml

version: "2"
services:
mysql-zabbix :
  image: "mysql:5.7"
  ports:
    - "53306:3306"
  networks:
    - net_zabbix
  volumes:
    - "vol_db_zabbix:/var/lib/mysql"
  environment:
    - "MYSQL_ROOT_PASSWORD=abcd"
    - "MYSQL_DATABASE=zabbix"
    - "MYSQL_USER=zabbix"
    - "MYSQL_PASSWORD=123456"

zabbix-server:
  image: "zabbix/zabbix-server-mysql:alpine-3.4.11"
  ports:
    - "10051:10051"
  networks:
    - net_zabbix
  environment:
    - "DB_SERVER_PORT=53306"
    - DB_SERVER_HOST=zabbix.db
    - "MYSQL_USER=zabbix"
    - "MYSQL_PASSWORD=123456"
  depends_on:
    - mysql-zabbix
  external_links:
    - mysql-zabbix:zabbix.db

zabbix-web:
  image: "zabbix/zabbix-web-apache-mysql:alpine-3.4.11"
  ports:
    - "80:80"
  networks:
    - net_zabbix
  environment:
    - DB_SERVER_HOST=zabbix.db
    - "DB_SERVER_PORT=53306"
    - "MYSQL_USER=zabbix"
    - "MYSQL_PASSWORD=123456"
    - ZBX_SERVER_HOST=zabbix.server
    - "PHP_TZ=America/Sao_Paulo"
  depends_on:
    - zabbix-server
  external_links:
    - mysql-zabbix:zabbix.db
    - zabbix-server:zabbix.server

zabbix-agent:
  image: "zabbix/zabbix-agent:alpine-3.4.11"
  ports:
    - "10050:10050"
  networks:
    - net_zabbix
  environment:
    - "ZBX_HOSTNAME=demo_zabbix"
    - ZBX_SERVER_HOST=zabbix.server
  external_links:
    - zabbix-server:zabbix.server

zabbix-proxy:
  image: "zabbix/zabbix-proxy-sqlite3:alpine-3.4.11"
  ports:
    - "10053:10050"
  networks:
    - net_zabbix
  environment:
    - "ZBX_HOSTNAME=demo_zabbix"
    - ZBX_SERVER_HOST=zabbix.server
  external_links:
    - zabbix-server:zabbix.server

networks:
net_zabbix:

volumes:
vol_db_zabbix:
    
asked by anonymous 13.07.2018 / 23:19

1 answer

1

You're using some concepts in the wrong way, like using external_links . external_links are used only when a link with container is created / managed outside the compose in question. Being on the same network and compose does not even links are needed, which are obsolete today, and it is preferable to manage containers access through networks .

That said, the first thing is to remove ALL the external_links from compose . Once this is done, we can now correct the names of the containers referenced in the environment variables. To facilitate I will not use link aliases , that is, zabbix.db becomes mysql-zabbix and zabbix.server becomes zabbix-server .

Another note: Because they are on the same network, it is not necessary to publish the mysql port, especially on another port, by configuring it in zabbix. On the same network the ports exposed by the containers are accessible to all other containers on the same network.

A possible final version would be as below:

version: "2"
services:
  mysql-zabbix:
    image: mysql:5.7
    networks:
      - net_zabbix
    volumes:
      - vol_db_zabbix:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=abcd
      - MYSQL_DATABASE=zabbix
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=123456

  zabbix-server:
    image: zabbix/zabbix-server-mysql:alpine-3.4.11
    ports:
      - "10051:10051"
    networks:
      - net_zabbix
    environment:
      - DB_SERVER_HOST=mysql-zabbix
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=123456
    depends_on:
      - mysql-zabbix

  zabbix-web:
    image: zabbix/zabbix-web-apache-mysql:alpine-3.4.11
    ports:
      - "80:80"
    networks:
      - net_zabbix
    environment:
      - DB_SERVER_HOST=mysql-zabbix
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=123456
      - PHP_TZ=America/Sao_Paulo
    depends_on:
      - zabbix-server

  zabbix-agent:
    image: "zabbix/zabbix-agent:alpine-3.4.11"
    ports:
      - "10050:10050"
    networks:
      - net_zabbix
    environment:
      - ZBX_HOSTNAME=demo_zabbix

  zabbix-proxy:
    image: zabbix/zabbix-proxy-sqlite3:alpine-3.4.11
    ports:
      - "10053:10050"
    networks:
      - net_zabbix
    environment:
      - ZBX_HOSTNAME=demo_zabbix

networks:
  net_zabbix:

volumes:
  vol_db_zabbix:

Validate the proxy configuration, since it is not found. I do not know in detail how zabbix works, so check it out.

Some values are default , according to the documentation of the images in Docker Hub , then they were omitted (like ZBX_SERVER_HOST ). Finally, as a commenting, you can create aliases using links to keep the same name, for clarity and simplicity I usually keep the same name.

>     
16.07.2018 / 14:06