docker-compose resolve hostname in url
Solution 1
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.
Solution 2
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:
itsmewiththeface
Software Engineer at HubSpot. Computer Science graduate from the University of Toronto.
Updated on June 04, 2022Comments
-
itsmewiththeface almost 2 years
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 pingapi
just fine inside theweb
container buthttp://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
theapi
just fine from inside theweb
container, but my app can't seem to resolve it properly. I'm using NodeJS and React-
David Maze about 5 yearsThis should work out-of-the-box. Can you include a complete
docker-compose.yml
file that illustrates the problem, as text, in the question? Can you give a specific error you’re getting? -
itsmewiththeface about 5 yearsMade some edits to the original question. The error that I'm getting is
net::ERR_NAME_NOT_RESOLVED
which is the app inweb
not resolving thehttp://api:8000/server/graphql
url
-
-
Christian W. about 5 yearsHe said that he can ping
api
fromweb
just fine, sounds like they are linked or share a network already. There's just not enough information in the question to actually answer his question. -
Quentin Petel over 4 yearsIf not defined, a
default
network is created for the stack : docs.docker.com/compose/networking -
titus about 4 yearsContainers spun up by docker-compose are DNS resolvable, no need for linking today.
-
spaceemotion almost 4 yearsTo expand upon the comment by @QuentinPetel: If you only want to add a container to an extra network, but also keep the link to
default
it needs to be added back manually.