0

I have a specific issue related to Docker compose. I am currently using docker compose version: 3.8

I have a gateway build in nodejs I need to get the container address of other application available in the gateway as environment variable. I looked into various documents but none of them are working.

version: "3.8" 
services:   
  user_service:
    build: 
      context: ./user-service
    environment:
      NODE_PORT: 4000   
  authentication_service:
    build: 
      context: ./authentication_service
    environment:
      NODE_PORT: 3001   
  gateway:
    build: ./public-gateway
    environment:
      - NODE_PORT=2000
      - USER_SERVICE=user_service
    depends_on:
      - user_service
      - authentication_service
    links:
      - user_service:user_service
    restart: always

Above is the docker compose file. But in the gateway code i am not able to get the host address of USER_SERVICE. I also read that links are deprecated in version 3.8 and host address are automatically available in all the application within the same network.

I printed process.env in nodejs application but i all i see is USER_SERVICE: user_service

I would really appreciate it if someone can pint me in the correct direction.

I would like to see the host address of the USER_SERVICE when i print process.env.USER_SERVICE in the nodejs application

Updated docker compose file which is working

version: "3.8" 
services:   
  user_service:
    build: 
      context: ./user-service
    environment:
      NODE_PORT: 4000   
  authentication_service:
    build: 
      context: ./authentication_service
    environment:
      NODE_PORT: 3001   
  gateway:
    build: ./public-gateway
    environment:
      - NODE_PORT=2000
      - USER_SERVICE=http://user_service:4000/user/service
    depends_on:
      - user_service
      - authentication_service
    restart: always

Note: During run time of docker USER_SERVICE=http://user_service:4000/user/service user_service will be replaced by ip address. Even if you print the environment variable[USER_SERVICE] it would show http://user_service:4000/user/service. user_service This would be resolved by the DNS.

Walter
  • 80
  • 5

0 Answers0