0

I am trying to run mongodb and Mongo-express containers using docker-compose. Mongo db might take some time to boot up(Up and running). So Mondo-express container should run only after Mondb container is up and running .

Even though am using the "depends_on" tag in mongo-exp, still in a few occasions mongo-exp container building will fail. If I try to recreate the mongo-exp container, it will work.

Please find the docker-compose.yaml

version : "3"
services:
  mongodb:
    container_name: mongodb
    image: mongo
    ports:
    - "27017:27017"
    volumes:
      - "/home/opsmgr/devops/docker_mount:/data/db"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=***
      - MONGO_INITDB_ROOT_PASSWORD=***
    networks:
      - dbpvt
  mongo-exp:
     image: mongo-express
     container_name: mongo-exp
     ports:
      - "8081:8081"
     environment:
       - ME_CONFIG_MONGODB_ADMINUSERNAME=**
       - ME_CONFIG_MONGODB_ADMINPASSWORD=**
       - ME_CONFIG_MONGODB_SERVER=mongodb
       - ME_CONFIG_BASICAUTH_USERNAME=**
       - ME_CONFIG_BASICAUTH_PASSWORD=**
     networks:
       - dbpvt
     depends_on:
      - mongodb
networks:
 dbpvt:**
Devops_mj
  • 3
  • 2

1 Answers1

0

You can add a restart policy in case of failure to auto restart your mongo-exp container.

services: 
  ...
    
  mongo-exp:
    ...
    depends_on: 
      - mongodb networks: dbpvt:**
    restart_policy:
        condition: on-failure

If this doesn't work, you can update your mongo-exp entrypoint to wait for your mongo service to start accepting requests. depends_on only checks if the service is running but not if it is ready to accept requests.

Jerven Clark
  • 1,191
  • 2
  • 13
  • 26