skip to Main Content

I have two separate docker containers, each running an api (auth_api and user_api).
I am trying to make a get request from the user_api service to the auth_api service but it fails with this error: curl: (6) Could not resolve host: auth_api

These are my docker-compose files:
/auth

version: '3'
services:
  auth_api:
    build:
      context: .
    command: uvicorn main:app --host 0.0.0.0
    ports: 
      - "8000:8000"

networks:
  default:
    name: shopnet
    external: false

/user

version: '3'
services:
  user_api:
    build:
      context: .
    command: uvicorn main:app --host 0.0.0.0 --port 8001
    ports: 
      - "8001:8001"

networks:
  shopnet:
    external: true

The shopnet network is created because it shows up in the list: docker network ls

I am executing the GET request with curl directly from the user_api container:
curl http://auth_api:8000/auth

3

Answers


  1. Chosen as BEST ANSWER

    Seems like adding

    networks:
      - default
      - shopnet
    

    to the user_api service fixes the problem


  2. Docker doesn’t use _ in the hostname anymore.

    Unless you didn’t update for ages, replace _ by -.

    https://github.com/docker/compose/issues/229

    Login or Signup to reply.
  3. The user_api service doesn’t have a networks: block, so it connects to the default network that Compose automatically provides. You declare a separate shopnet network, but don’t attach your network to it. (Your answer addresses this by explicitly naming the other network.)

    You can configure the default network to make the "user" default network be the same as the "auth" default network:

    # user/docker-compose.yml
    version: '3.8'
    services:
      user_api:
        build: .
        ports: ['8081:8081']
        # networks: [default] # automatically provided by Compose
    
    networks:
      default:
        name: shopnet
        external: true
    

    You also may be able to put both services in the same docker-compose.yml file, depending on your specific setup, and then you wouldn’t need any manual networks: setup at all.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search