我需要从另一个容器中卷曲我的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失败了:
错误:for apiwip_fpm_1无法为服务fpm创建容器:add-host中的IP地址无效:" nginx"
我已经看到一些人使用docker网络配置的例子,但只是解析一个地址似乎超出了顶部。
如何解析/评估容器的IP地址而不是直接传递它?
答案 0 :(得分:8)
在默认网络中添加网络别名。
version: "3.7"
services:
nginx:
# ...
networks:
default:
aliases:
- example.local
browser-sync:
# ...
depends_on:
- nginx
command: "browser-sync start --proxy http://example.local"
答案 1 :(得分:2)
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
@edward让我知道此解决方案是否对您有用
答案 2 :(得分:1)
来自docker文档,
https://docs.docker.com/compose/networking/
您应该能够使用您的服务名称(在本例中为nginx
)作为docker网络中的主机名。所以你可以打入我的FPM容器并打电话给curl nginx
,而码头工具会为你解决它。希望这会有所帮助。
答案 3 :(得分:0)
快速解决方法是将其指向由docker生成的动态IP。这可能会改变......是的。
找到您的网络:
docker network ls
NETWORK ID NAME DRIVER SCOPE
72fef1ce7a50 apiwip_default bridge local <-- here
cdf9d5b885f6 bridge bridge local
2f4f1e7038fa host host local
a96919eea0f7 mgadmin_default bridge local
30386c421b70 none null local
5457b953fadc website2_default bridge local
1450ebeb9856 anotherapi_default bridge local
复制NETWORK ID
docker network inspect 72fef1ce7a50
"Containers": {
"345026453e1390528b2bb7eac4c66160750081d78a77ac152a912f3de5fd912c": {
"Name": "apiwip_nginx_1",
"EndpointID": "6504a3e4714a6ba599ec882b21f956bfd1b1b7d19b8e04772abaa89c02b1a686",
"MacAddress": "02:42:ac:14:00:05",
"IPv4Address": "172.20.0.5/16", <-- CIDR block
"IPv6Address": ""
},
"ea89d3089193825209d0e23c8105312e3df7ad1bea6b915ec9f9325dfd11736c": {
"Name": "apiwip_fpm_1",
"EndpointID": "dc4ecc7f0706c0586cc39dbf8a05abc9cc70784f2d44c90de2e8dbdc9148a294",
"MacAddress": "02:42:ac:14:00:04",
"IPv4Address": "172.20.0.4/16",
"IPv6Address": ""
}
},
将该CIDR块的IP地址添加到extra_hosts选项:
extra_hosts:
- myapi.docker:172.20.0.5
答案 4 :(得分:0)
我通过使用links
而非extra_hosts
解决了此类问题。
在这种情况下,只需设置link alias
就可以帮到您。
links
- nginx:myapi.docker
请参见docker-compose links documentation,别名可以是您的代码中显示的域。