How to assign recover ip from a service within Docker-Composer?

Asked

Viewed 78 times

1

I’m trying to raise Zabbix on Docker, I created a Docker-Compose with several services, one of them is the database. I need to survey the database first and then take the IP of the database and set in the environment variable of other services, but I do not know how to do, I have tried to use the links, but I am not succeeding.

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:

1 answer

1


You are using some concepts incorrectly, such as the use of external_links. external_links are used only when a link with container created/managed outside the commie in question. Being on the same network and commie not even links are needed, and these are currently obsolete, and it is preferable to manage access to containers through Networks.

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

Another observation: as they are in the same network it is not necessary to publish the port of mysql, mainly in another port, configuring it in Zabbix. In the same network the ports exposed by containers are accessible to all others containers on the same network.

A final possible version would be the 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 configuration of proxy, since it is not found. I do not know in detail how the Zabbix works, then review it.

Some values are default, as documented in the Docker Hub, then were omitted (as the ZBX_SERVER_HOST). Finally, as commented, you can create aliases using the links to keep the same name, for clarity and simplicity I usually keep the name of services even.

Browser other questions tagged

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