version: "3.5" | |
services: | |
mongo: | |
image: mongo:latest | |
container_name: mongo | |
environment: | |
MONGO_INITDB_ROOT_USERNAME: admin | |
MONGO_INITDB_ROOT_PASSWORD: admin | |
ports: | |
- "0.0.0.0:27017:27017" | |
networks: | |
- MONGO | |
volumes: | |
- type: volume | |
source: MONGO_DATA | |
target: /data/db | |
- type: volume | |
source: MONGO_CONFIG | |
target: /data/configdb | |
mongo-express: | |
image: mongo-express:latest | |
container_name: mongo-express | |
environment: | |
ME_CONFIG_MONGODB_ADMINUSERNAME: admin | |
ME_CONFIG_MONGODB_ADMINPASSWORD: admin | |
ME_CONFIG_MONGODB_SERVER: mongo | |
ME_CONFIG_MONGODB_PORT: "27017" | |
ports: | |
- "0.0.0.0:8081:8081" | |
networks: | |
- MONGO | |
depends_on: | |
- mongo | |
networks: | |
MONGO: | |
name: MONGO | |
volumes: | |
MONGO_DATA: | |
name: MONGO_DATA | |
MONGO_CONFIG: | |
name: MONGO_CONFIG |
The above error/warning didn't prevent me from running, can be ignored if this is for learning.
The "wait-for" fixed my issue, thanks so much for this example @adamelliotfields!
You can also use restart: unless-stopped
instead of overwriting the entrypoint.
I get a connection error that seems due to the fact that mongo-express does not wait for mongodb to start (i can connect successfuly with compass to localhost:27017)
mongo-express | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo-server:27017] on first connect [Error: connect ECONNREFUSED 172.19.0.2:27017
mongo-express | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
mongo-express | name: 'MongoNetworkError'
mongo-express | }]
mongo-server:
container_name: mongo-server
image: mongo:4.4.5
networks:
- weally
ports:
- "27017:27017"
volumes:
- type: bind
source: /var/weally/mongodb
target: /data/db
# - /var/weally/mongodb:/data/db
# - ./config/mongodb.conf:/data/configdb
restart: always
mongo-express:
image: mongo-express:latest
container_name: mongo-express
environment:
ME_CONFIG_MONGODB_SERVER: mongo-server
ME_CONFIG_MONGODB_PORT: "27017"
ports:
- "8081:8081"
networks:
- weally
depends_on:
- mongo-server
You can also use restart: unless-stopped instead of overwriting the entrypoint.
works too and it's a cleaner solution imo.
Think of the wait-for
script as like a Kubernetes readiness probe. Kubernetes wont serve traffic to a pod that isn't ready.
That said, you can certainly get by with restart policies if you don't mind the log spam.
Also check out the READMEs for mongo and mongo-express on Docker Hub - they include everything in this Gist and more (and they aren't 4 years old).
To ensure
mongo-express
doesn't start until Mongo is accepting connections, usewait-for
.Add this under the
mongo-express
service:See https://docs.docker.com/compose/startup-order for more information.