Tim Rasim
Tim Rasim

Reputation: 684

Docker Compose with mongo container irregularly not connecting

I have two containers with a docker-compose.yml:

version: "3"
services:
  sourcejs:
    image: neogucky/myapplication
    command: /bin/bash /home/myapplication/startScript.sh
    ports:
      - "80:8080"
    restart: always
    links:
      - mongo
  mongo:
    image: mongo
    ports:
      - "27017:27017"
    restart: always

This configuration worked when first started with:

docker-compose -p productive up -d

Now I'm experiencing a weird problem: when I restart the server and docker automatically starts my two containers, sometimes the myapplication container gets stuck in an endless bootup loop. Since part of its start script will update files using git it takes around 2 minutes until it fires up the application that needs to connect to the mongo container. When not working it says

MongoError: failed to connect to server [mongo:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongo mongo:27017]

Docker continuously restarts myapplication container but the error will consists, unless I restart the host machine. After restarting, there is a 50% chance it will work this time. When entering "docker ps" everything looks fine, the mongo server is up and running but myapplication server keeps restarting every 2 minutes.

Any ideas?

Upvotes: 0

Views: 952

Answers (1)

Prateek Jain
Prateek Jain

Reputation: 3075

can you try flipping the order of services in docker-compose file? it looks like mogodb doesn't come up with in the period expected but it gathers port which eventually doesn't get properly cleaned up in next restart.

Upvotes: 1

Related Questions