如何在同一堆栈网络中连接两个服务?

时间:2019-10-25 12:03:20

标签: docker docker-swarm docker-networking docker-stack

我有docker-compose.yaml文件,如:

version: '3.7'
services:
  passbolt:
    image: passbolt/passbolt
    hostname: passbolt
    container_name: passbolt
    ports:
      - 80:80
      - 443:443
    environment:
      DATASOURCES_DEFAULT_HOST: mariadb
      DATASOURCES_DEFAULT_USERNAME: passbolt
      DATASOURCES_DEFAULT_PASSWORD: passbolt
      DATASOURCES_DEFAULT_DATABASE: passbolt
      APP_FULL_BASE_URL: https://my.url/
    networks:
      - passbolt_network
    depends_on:
      - mariadb
  mariadb:
    image: mariadb
    hostname: mariadb
    container_name: mariadb
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: passbolt
      MYSQL_USER: passbolt
      MYSQL_PASSWORD: passbolt
    networks:
      - passbolt_network
networks:
  passbolt_network:
    driver: overlay

我是docker stack deploy -c docker-compose.yaml test的runnig堆栈 并且 passbolt 服务由于异常而退出:连接数据库时出现问题:SQLSTATE [HY000] [2002](...)

1 个答案:

答案 0 :(得分:0)

您还可以使用网络别名:

version: '3.7'
services:
  passbolt:
    image: passbolt/passbolt
    hostname: passbolt
    container_name: passbolt
    ports:
      - 80:80
      - 443:443
    environment:
      DATASOURCES_DEFAULT_HOST: alias_to_mariadb
      DATASOURCES_DEFAULT_USERNAME: passbolt
      DATASOURCES_DEFAULT_PASSWORD: passbolt
      DATASOURCES_DEFAULT_DATABASE: passbolt
      APP_FULL_BASE_URL: https://my.url/
    networks:
      - passbolt_network
    depends_on:
      - mariadb
  mariadb:
    image: mariadb
    hostname: mariadb
    container_name: mariadb
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: passbolt
      MYSQL_USER: passbolt
      MYSQL_PASSWORD: passbolt
    networks:
      passbolt_network:
        aliases:
          - "alias_to_mariadb"
networks:
  passbolt_network:
    driver: overlay