动态添加 nginx 容器 ip 到 phpfpm /etc/hosts 文件中



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

在我的 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

为什么需要 Nginx IP 地址?你可以通过主机名从phpfpm容器调用nginxnginx

最新更新