docker-compose.yml
Example container configuration:
mongo-express:
image: mongo-express
container_name: mongo-express
ports:
- 8081:8081
volumes:
- ./.docker/mongo-express/docker-entrypoint.sh:/docker-entrypoint.sh
env_file:
- .env
environment:
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_INITDB_ROOT_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
ME_CONFIG_BASICAUTH_USERNAME: ${ME_CONFIG_BASICAUTH_USERNAME}
ME_CONFIG_BASICAUTH_PASSWORD: ${ME_CONFIG_BASICAUTH_PASSWORD}
depends_on:
- mongodb
What does it all mean?
image: mongo-express
Specifies an image to use.
container_name: mongo-express
Specifies a custom container name, rather than a generated default name.
volumes:
Mounts host paths or named volumes, specified as sub-options to a service.
In case of the above configuration:
- .docker/mongo-express/docker-entrypoint.sh:/docker-entrypoint.sh
will mount local .docker/mongo-express/docker-entrypoint.sh
file as /docker-entrypoint.sh
in the container, allowing easy modifications to the container initialization file.
This is a slightly modified original Mongo Express initialization script with only change being the modified value of max_tries=20
instead of original 5
, to give the main mongodb container enough time to start and be connectable to.
env_file:
- .env
Add environment variables from a file. Can be a single value or a list.
ports:
- 8081:8081
Port mapping defined in short syntax: HOST:CONTAINER
.
8081:8081
means that port 8081
on host will forward all its traffic to port 8081
in the container.
environment:
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_INITDB_ROOT_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
ME_CONFIG_BASICAUTH_USERNAME: ${ME_CONFIG_BASICAUTH_USERNAME}
ME_CONFIG_BASICAUTH_PASSWORD: ${ME_CONFIG_BASICAUTH_PASSWORD}
Add environment variables, either as an array or a dictionary.
depends_on:
- mongodb
Express dependency between services, so that they are started in the dependency order.
.docker/mongo-express/docker-entrypoint.sh
This is a slightly modified original Mongo Express initialization script with only change being the modified value of max_tries=20
instead of original 5
, to give the main mongodb container enough time to start and be connectable to.
#!/bin/bash
set -eo pipefail
# This is a very small modification of the original docker script taken from
# https://github.com/mongo-express/mongo-express-docker/blob/master/docker-entrypoint.sh
# with only change being the modified value of max_tries=20 instead of original 5,
# to give the main mongodb container enough time to start and be connectable to.
# Also, see https://docs.docker.com/compose/startup-order/
# if command does not start with mongo-express, run the command instead of the entrypoint
if [ "${1}" != "mongo-express" ]; then
exec "$@"
fi
function wait_tcp_port {
local host="$1" port="$2"
local max_tries=20 tries=1
# see http://tldp.org/LDP/abs/html/devref1.html for description of this syntax.
while ! exec 6<>/dev/tcp/$host/$port && [[ $tries -lt $max_tries ]]; do
sleep 10s
tries=$(( tries + 1 ))
echo "$(date) retrying to connect to $host:$port ($tries/$max_tries)"
done
exec 6>&-
}
# if ME_CONFIG_MONGODB_SERVER has a comma in it, we're pointing to a replica set (https://github.com/mongo-express/mongo-express-docker/issues/21)
if [[ "$ME_CONFIG_MONGODB_SERVER" != *,* ]]; then
# wait for the mongo server to be available
echo Waiting for ${ME_CONFIG_MONGODB_SERVER}:${ME_CONFIG_MONGODB_PORT:-27017}...
wait_tcp_port "${ME_CONFIG_MONGODB_SERVER}" "${ME_CONFIG_MONGODB_PORT:-27017}"
fi
# run mongo-express
exec node app
.env.dist
# Mongo Express
ME_CONFIG_BASICAUTH_USERNAME=admin
ME_CONFIG_BASICAUTH_PASSWORD=admin