我需要从另一个容器中卷曲我的API.
容器1称为Nginx
容器2称为fpm
我需要能够扑向FPM容器并卷曲Nginx容器.
配置:
services:
Nginx:
build:
context: .
dockerfile: ./docker/Nginx/Dockerfile
volumes:
- ./docker/Nginx/conf/dev/api.conf:/etc/Nginx/conf.d/default.conf
ports:
- 8080:80
links:
- fpm
fpm:
build:
context: .
dockerfile: ./docker/fpm/Dockerfile
volumes:
- .:/var/www/html
- ./docker/fpm/conf/dev/xdebug.ini:/usr/local/etc/PHP/conf.d/xdebug.ini
- ./docker/fpm/conf/dev/api.ini:/usr/local/etc/PHP/conf.d/api.ini
env_file:
- ./docker/MysqL/MysqL.env
- ./docker/fpm/conf/dev/fpm.env
links:
- MysqL
shm_size: 256M
extra_hosts:
- myapi.docker:Nginx
我最初的想法是将它放在extra_hosts选项中,例如:
extra_hosts:
- myapi.docker:Nginx
但是docker-compose up失败了:
ERROR: for apiwip_fpm_1 Cannot create container for service fpm: invalid IP address in add-host: “Nginx”
我已经看到了一些使用docker网络配置的人的例子,但是仅仅解析地址似乎是最重要的.
我如何解析/评估容器的IP地址,而不是直接通过容器传递它?
最佳答案
services:
Nginx:
build:
context: .
dockerfile: ./docker/Nginx/Dockerfile
volumes:
- ./docker/Nginx/conf/dev/api.conf:/etc/Nginx/conf.d/default.conf
ports:
- 8080:80
networks:
my_network:
aliases:
- myapi.docker
- docker_my_network
fpm:
build:
context: .
dockerfile: ./docker/fpm/Dockerfile
volumes:
- .:/var/www/html
- ./docker/fpm/conf/dev/xdebug.ini:/usr/local/etc/PHP/conf.d/xdebug.ini
- ./docker/fpm/conf/dev/api.ini:/usr/local/etc/PHP/conf.d/api.ini
env_file:
- ./docker/MysqL/MysqL.env
- ./docker/fpm/conf/dev/fpm.env
links:
- MysqL
shm_size: 256M
extra_hosts:
- myapi.docker:Nginx
networks:
- my_network
networks:
my_network:
driver: bridge
>添加自定义网络并将容器添加到该网络
>默认情况下,如果从fpm卷曲Nginx容器,则将卷曲localhost,因此我们需要添加别名,使其与Nginx配置中的服务器名称相同
>使用此解决方案,您可以从fpm容器中卷曲myapi.docker
@edward让我知道此解决方案是否对您有用