docker-compose resolve hostname in url

12,084

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:
Share:
12,084
itsmewiththeface
Author by

itsmewiththeface

Software Engineer at HubSpot. Computer Science graduate from the University of Toronto.

Updated on June 04, 2022

Comments

  • itsmewiththeface
    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 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

    • David Maze
      David Maze about 5 years
      This 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
      itsmewiththeface about 5 years
      Made some edits to the original question. The error that I'm getting is net::ERR_NAME_NOT_RESOLVED which is the app in web not resolving the http://api:8000/server/graphql url
  • Christian W.
    Christian W. about 5 years
    He said that he can ping api from web 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
    Quentin Petel over 4 years
    If not defined, a default network is created for the stack : docs.docker.com/compose/networking
  • titus
    titus about 4 years
    Containers spun up by docker-compose are DNS resolvable, no need for linking today.
  • spaceemotion
    spaceemotion almost 4 years
    To 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.