希望在我的phpfpm容器/ etc / hosts文件中自动添加nginx容器ip地址。

在我的yml文件中,我有一个名为phpfpm的服务,并且我知道您可以使用extra_hosts属性将值分配到/ etc / hosts文件中,但是我不知道如何动态调用nginx容器IP。

  nginx:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ../public/:/var/www/html/public/
    container_name: nginx
    networks:
      - backend

  phpfpm:
    build: ./php-fpm
    volumes:
      - ../public/:/var/www/html/public/
    container_name: phpfpm
    extra_hosts:
      - "test.local:nginx" <insert nginx ip to test.local>
    networks:
      - backend

有关如何执行此操作的任何想法?

最佳答案

撰写文件中的容器将在同一网络上运行,您只能使用它们的名称。您的情况下的phpfpmnginx。另外,如果您需要为同一服务使用更多名称,则需要使用别名

  nginx:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ../public/:/var/www/html/public/
    container_name: nginx
    networks:
      backend:
        aliases:
          - test.local

  phpfpm:
    build: ./php-fpm
    volumes:
      - ../public/:/var/www/html/public/
    container_name: phpfpm
    networks:
      - backend

关于docker - 将nginx容器ip动态添加到phpfpm/etc/hosts文件中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45750752/

10-11 22:58
查看更多