Startup connection issues between Mongo Express and Mongo database containers using Docker
This article solves one of the problems known as “/docker-entrypoint.sh: connect: Connection refused”
Many developers are struggling with problems when Mongo Express tries to connect with Mongo database using Docker-compose.
The general problem relates to the fact that Mongo Express needs to wait for Mongo database to be up. It turns out that Mongo Express developers provided a solution to this problem. Within Mongo Express Docker image comes a shell script (docker-entrypoint.sh) that tries to connect with Mongo database multiple times.
Context — Docker-compose configuration with two services: Mongo Express and Mongo database
Consider the following situation with two containers. One, mongo.service, which is to be the datbase. And another, the client, named mongo-express.service.
version: ‘3’
services:
mongo.service:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: test
#ports:
# — “27017:27017”
networks:
— mongo-compose-networkmongo-express.service:
image: mongo-express
ports:
— 8081:8081
environment:
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: test
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_SERVER: mongo.service
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: test
networks:
— mongo-compose-network
depends_on:
— mongo.servicenetworks:
mongo-compose-network:
driver: bridge
Understanding max_tries and 1s delay between each attempt
The problem may appear when using Docker-compose to launch these services. When launching Mongo Express and Mongo database, you will notice that Mongo Express attempts to connect multiple times.
Tue Jan 26 16:26:11 UTC 2021 retrying to connect to mongo:27017 (4/5)
mongo-express_1 | /docker-entrypoint.sh: connect: Connection refused
The problem, for many users, has to do with the fact that its max_tries (=5) parameter is not enough.
Proposed fix at Mongo Express Docker Compose Service
A possible solution is to enable developers to pass an parameter from the Docker-compose config file. In the following example, you will see such proposed parameter in bold “ME_CONFIG_MONGODB_SERVER_WAIT_MAX_TRIES: 30”.
WARNING: this parameter is not officially supported. This is just a proposition.
version: ‘3’
services:
mongo.service:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: test
#ports:
# — “27017:27017”
networks:
— mongo-compose-networkmongo-express:
image: mongo-express
ports:
— 8081:8081
environment:
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: test
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_SERVER: mongo.service
ME_CONFIG_MONGODB_SERVER_WAIT_MAX_TRIES: 30
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: test
networks:
— mongo-compose-network
depends_on:
—mongo.servicenetworks:
mongo-compose-network:
driver: bridge
Manually changing it
To enable the parameter, you can fork your own Docker-based image using the official mongo-express-docker container image:
The main change consists of changing docker-entrypoint.sh to support an environment variable that can be passed from the Docker-compose’s Mongo Express config:
Original line:
https://github.com/mongo-express/mongo-express-docker/blob/master/docker-entrypoint.sh#L11
Proposed patch:
local max_tries=${ME_CONFIG_MONGODB_SERVER_WAIT_MAX_TRIES:-10} tries=1
Then you can build your image using build.sh
https://github.com/taboca/mongo-express-docker/blob/master/build.sh
Resources
- Mongo Express — https://github.com/mongo-express/mongo-express-docker
- Mongo Express image docker-entrypoint.sh — https://github.com/mongo-express/mongo-express-docker/blob/master/docker-entrypoint.sh#L11
- Fork image that uses ME_CONFIG_MONGODB_SERVER_WAIT_MAX_TRIES — https://github.com/taboca/mongo-express-docker/blob/master/build.sh
- Proposed patch with Mongo Express official image — https://github.com/mongo-express/mongo-express-docker/issues/52