Mahesh
Mahesh

Reputation: 101

Images from Docker compose not working as expected when running from docker run command

I have few micro-services built using Spring boot which are

The following is part of the docker-compose.yml file

version: '3.7'

services:
  st-service-registry:
    build:
      context: service-registry/
      args:
        VERSION: ${VERSION}
    restart: on-failure
    image: st/service-registry:${VERSION}
    container_name: st-service-registry
    ports:
      - 8180:8180
    tty: true
    networks:
      - st-network

  st-backend:
    build:
      context: backend/
      args:
        VERSION: ${VERSION}
    restart: on-failure
    image: st/backend:${VERSION}
    environment:
      eureka.client.serviceUrl.defaultZone: http://st-service-registry:8180/eureka
    depends_on:
      - mongo
      - redis
      - st-service-registry
      - st-gateway-server
    volumes:
      - /etc/timezone:/etc/timezone
      - /etc/localtime:/etc/localtime
    networks:
      - st-network

  st-gateway-server:
    build:
      context: gateway-server/
      args:
        VERSION: ${VERSION}
    restart: on-failure
    image: st/gateway-server:${VERSION}
    container_name: st-gateway-server
    ports:
      - 8080:8080
    depends_on:
      - st-service-registry
    networks:
      - st-network

  mongo:
    image: mongo
    container_name: mongo
    ports:
      - 27017:27017
    volumes:
      - ${MONGO_DB_DATA_LOCATION}:/data/db
    restart: on-failure
    networks:
      - st-network

  redis:
    image: redis
    container_name: redis
    ports:
      - 6379:6379
    restart: on-failure
    networks:
      - st-network

networks:
  st-network:
    driver: bridge

When I run the entire stack using docker-compose up --build everything works perfect. I can see the docker images and I can run the microservices and stuff like that. But when I try to run individual images using docker run -itd -p 8080:8080 _image_Id_ the micro-service cannot connect to the service registry. It says Connection Refused

Do you guys have any lead on this? Let me know if you need any other information. Thanks in advance.

Upvotes: 1

Views: 1670

Answers (2)

Barath
Barath

Reputation: 5283

Below property has to be corrected:

   environment:
      EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://st-service-registry:8180/eureka

Sample docker-compose.yml

version: '3.7'
services:
  eureka-server:
    image: barathece91/eureka-server-demo
    ports:
      - "8081:8081"
    networks:
      - st-network
  eureka-client-app1:
    image: barathece91/eureka-client-demo
    ports:
      - "8082:8082"
    networks:
      - st-network
    depends_on:
      - eureka-server
    environment:
      EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka-server:8081/eureka    

networks:
  st-network:
    driver: bridge    

Upvotes: 1

fly2matrix
fly2matrix

Reputation: 2467

try setting --name as container-name and --hostname as container-name for all the container's that you are going to generate

The point is when you create containers manually - it will set container-name or hostname auto-generated.

Services looking up each other by their name (container-name)

for example:

docker run -d --name st-service-registry \
              --hostname st-service-registry \
              --network st-network \
              --restart=on-failure \
              -p 8180:8180 \
              st/service-registry:${VERSION}

docker run -d --name redis \
              --hostname redis \
              --network st-network \
              --restart=always \
              -p 6379:6379 \
              redis
docker run -d --name mongo \
              --hostname mongo \
              --network st-network \
              --restart=always \
              -p 27017:27017 \
              mongo

docker run -d --name st-gateway-server \
              --hostname st-gateway-server \
              --network st-network \
              --restart=on-failure \
              -p 8080:8080 \
              st/gateway-server:${VERSION}


docker run -d --name st-service-registry \
              --hostname st-service-registry \
              -e eureka.client.serviceUrl.defaultZone: http://st-service-registry:8180/eureka \
              --network st-network \
              -v /etc/timezone:/etc/timezone \
              -v /etc/localtime:/etc/localtime \
              st/backend:${VERSION}

Upvotes: 0

Related Questions