问题
Tried looking around but couldn't find anything close to what I need.
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"
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.
EDIT: 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
回答1:
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:
回答2:
Alright, I found the issue. Apparently, my web container was fetching from api with the http://api:8000 URI but my browser doesn't know what api is (only the containers do).
I followed the stuff suggested in here to resolve the hostname on my machine and it worked out.
来源:https://stackoverflow.com/questions/55274801/docker-compose-resolve-hostname-in-url