Home > Software design >  Docker: Why is docker compose not starting other containers?
Docker: Why is docker compose not starting other containers?

Time:07-24

Here is my docker-compose.yml file


version: "3.9"
services: 
  mongodb:
    image: mongo 
    ports:
      - "27017:27017"
    environment: # environmental variables
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
  mongo-express:
    image: mongo-express
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb

When i run the up command

docker compose up

The mongodb is running but the mongo-express is not running, in the logs i see a message that says:

app-mongo-express-1 exited with code 0

When i run the ps command i see only one container running:

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                      NAMES
562b810882b6   mongo     "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:27017->27017/tcp   app-mongodb-1

Why is mongo-express not running?

CodePudding user response:

I don't see you have added dependency between mongodb and mongo-express. You need to add depends_on under mongo-express. Of course since mongo-express cannot connect to the mongodb it fails and added restart: always to mongo-express

version: "3.9"
services: 
  mongodb:
    image: mongo 
    ports:
      - "27017:27017"
    environment: # environmental variables
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - "8081:8081"
    depends_on:
      - mongodb

    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
  • Related