0

I am using docker compose to build containers of service (user, proxy). I plan to deploy these services in the same network (internal_network) I would like these services have access by their environment variables (with GATEWAY_URL_VAR) to the gateway adress of their network (172.16.238.1).

Currently my docker-compose.yml file is the following :

version: "3.9"
services:
  user:
    build: ./users-api
    volumes:
      - ./users-api/src:/users-api/src
    networks:
      - internal_network
    environment:
      GATEWAY_URL_VAR: http://172.16.238.1
  proxy:
    image: nginx
    volumes:
      - ./proxy:/etc/nginx
    networks:
      - internal_network
    ports:
      - "8080:8080"
    depends_on:
      - user
networks:
  internal_network:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.16.238.0/29
          gateway: 172.16.238.1

As you can see I have copied the address from the networks.internal_network.ipam.config.gateway to GATEWAY_URL_VAR. I think is NOT a good way, cause I have duplicated variables.

In the docker-compose, how could I get the gateway IP from the network where a service is containerised ?

Maybe it exists a dynamic var kind of similar to DOCKER_GATEWAY_HOST that I could use there :

    environment:
      GATEWAY_URL_VAR: http://${DOCKER_GATEWAY_DOODAD}

in order to have access to the 172.16.238.1 address.

For information, when I run $ docker network inspect microservices_internal_network, I get the following :

[
    {
        "Name": "microservices_internal_network",
        "Id": "9779e8115f28bfe0355a5e856f2c3ea3db1a2dce88d548be2f83594e4f71b10f",
        "Created": "2022-03-05T22:37:57.5491073Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.16.238.0/29"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "4ea47e833fc260b2e6d79ee03f54743e2772cc6176fdffb2daf7161bb10064d0": {
                "Name": "microservices-proxy-1",
                "EndpointID": "c011556d6a9dc74cc5a2f45c9a14f291cfe7da4bb70531e4bc7de8d409532461",
                "MacAddress": "02:42:ac:10:ee:03",
                "IPv4Address": "172.16.238.3/29",
                "IPv6Address": ""
            },
            "8abb1becdc51c701ed4552504cf4e0017e0c6b9d825adda13e802d19973ae088": {
                "Name": "microservices-user-1",
                "EndpointID": "77aa0e1b14ec5d81aad01fc4664f7b987f525043619b382c26bff8be001d253e",
                "MacAddress": "02:42:ac:10:ee:02",
                "IPv4Address": "172.16.238.2/29",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "internal_network",
            "com.docker.compose.project": "microservices",
            "com.docker.compose.version": "2.2.3"
        }
    }
]

I will be very gratefull if you could help me : ) If you think the title of this post or its tags, are not appropriate, please let me know.

Naedri
  • 1
  • 4
  • This is, fundamentally, reaching the same place as [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach); do the answers there help you? (On a MacOS or Windows host, delete all of the `networks:` blocks in the entire file, but set `GATEWAY_URL_VAR` to `http://host.docker.internal`.) – David Maze Mar 06 '22 at 00:33

0 Answers0