skip to Main Content

I have a case where I need to call external API from docker container, but can only do that by its URL. In order to do that I’m mapping this URL to container gateway and that’s working, but I need it to be dynamic, because I need to run this docker-compose on different devices and from what I see, the gateways are different.

version: '3'

services:
  pdf-service:
    image: $IMAGE:latest
    container_name: pdf-$LOCALE
    environment:
      - NODE_ENV=production
      - LOCALE=${LOCALE}
      - API_URL=${API_URL}
    extra_hosts:
      - ${API_HOST}:172.24.0.1
    tty: true
    restart: always
    ports:
      - ${PORT}:8124

At the moment I’ve hardcoded it as you can see – 172.24.0.1, which is container’s gateway. I’ve found out about something like host.gateway, but have no idea how to use it correctly. Also I’ve read that it’s not working in production? My production environment is Debian 10 with Docker v. 18.09.1 and docker-compose v.1.21.0.

2

Answers


  1. The IP 172.24.0.1 is internal to Docker. When you add an extra host you need to map a name to a public IP.

    From your host run ping <api_host> to get the public IP. Use that IP in your docker-compose.yml instead of 172.24.0.1.

    If the service you want to reach runs also in Docker, then it must expose a port on the host in order for you to reach it. So then the IP you need is the local network IP of your host (or the public one if for some reason you need to).

    If you had to reach an IP that is Docker internal then you wouldn’t have to declare an extra_host. You would just put the containers/services on the same network and refer each other by name.

    Login or Signup to reply.
  2. I think you can use network, container can join many network, and if two container in a same network, they can find another by host (service name)

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