Home > Back-end >  resolve host machine hostname inside docker container
resolve host machine hostname inside docker container

Time:09-24

I have one application running on http://home.local:8180 in container A. And the other container B is running on http://data.local:9010. Container B is using container A to hit the API. If I specify container A hostname as http://host.docker.internal:8180 in container B then it works. What I would have to do if I want to use the hostname as is (home.local:8180) Following is the docker-compose file:

  home_app:
    hostname: "home.local"
    image: "home-app"
    ports:
    - "8180:8080"
    environment:

  data_app:
    hostname: "data.local"
    image: "data-app"
    links:
      - "home_app"
    ports:
    - "9010:9010"

CodePudding user response:

Just use "home.local:8080". 8180 is just on the host machine and forwards to 8080 on the container, whereas based on your docker-compose, 8080 is the port of your application on home_app container, so within the docker-compose network, other containers should be able to access it via hostname (home.local) and the actual ports (8080).

CodePudding user response:

You need to configure your application to use the Compose service name home_app as a host name, and the port number that the process inside the container is using. Neither hostname: nor ports: has any effect on connections between containers. You don't need to (and can't) specify a custom DNS suffix. See Networking in Compose in the Docker documentation for additional details.

So I might specify:

version: '3.8'
services:
  home_app:
    image: "home-app"
    ports:
    - "8180:8080" # optional, only for access from outside Docker

  data_app:
    image: "data-app"
    ports:
    - "9010:9010"
    environment:
      HOME_APP_URL: 'http://home_app:8080'

You don't need hostname:, which only affects what a container thinks its own hostname is and has no effect on anything outside the container; and you don't need links:, which is an obsolete option from first-generation Docker networking.

  • Related