docker-compose在url中解析主机名 [英] docker-compose resolve hostname in url
问题描述
试图环顾四周,但找不到与我需要的东西接近的东西。
Tried looking around but couldn't find anything close to what I need.
我有一个包含docker容器的docker-compose文件( web
)通过解析主机名在其环境变量中使用另一个容器的IP( api
):
I have a docker-compose file with a docker container (web
) that uses another container's IP (api
) in its environment variable by resolving the hostname:
version: '3'
services:
web:
build: ../client/
ports:
- "5000:5000"
- "3000:3000"
environment:
REACT_APP_API_DEV: http://api:8000/server/graphql
api:
build: ../server/
env_file:
- server_variables.env
ports:
- "8000:8000"
redis:
image: "redis:alpine"
我的问题是网络
在运行时不会解析此变量。我可以在 web
容器内ping api
很好,但是 http:// api:8000
无法正确解析。我还尝试将HOST = api设置为变量并手动构建URI,但这也不起作用。
My issue is that web
doesn't resolve this variable when it's running. I can ping api
just fine inside the web
container but http://api:8000
doesn't resolve properly. I also tried making HOST=api the variable and building the URI manually but that doesn't work either.
编辑:我为以下操作添加了完整的docker-compose.yml文件参考。我可以在 web
内 curl
api
很好容器,但我的应用似乎无法正确解决。我正在使用NodeJS和React
I added a complete docker-compose.yml file for reference. I can curl
the api
just fine from inside the web
container, but my app can't seem to resolve it properly. I'm using NodeJS and React
推荐答案
您必须使用 网络
You have to link them using network
version: '3'
services:
web:
...
environment:
- HOST: http://api:8000
networks:
- my-network
...
api:
networks:
- my-network
...
networks:
my-network:
这篇关于docker-compose在url中解析主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!