Skip to content

Instantly share code, notes, and snippets.

@makhmudovrt
Created October 25, 2023 18:33
Show Gist options
  • Save makhmudovrt/d1b6100a77eb3fc4b15db656c997038d to your computer and use it in GitHub Desktop.
Save makhmudovrt/d1b6100a77eb3fc4b15db656c997038d to your computer and use it in GitHub Desktop.
Mongo 7 with a replica set with only one node in docker compose
version: "3.7"
services:
# simple
mongo:
image: mongo
command: mongod --replSet rs0 --bind_ip_all
healthcheck:
test: |
mongosh --eval "try { rs.status().ok } catch (e) { rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'localhost:27017' }] }).ok }"
start_period: 0s
interval: 500ms
timeout: 5s
retries: 5
ports: [27017:27017]
volumes:
- ./mongodb/data:/data/db
# with auth
mongo:
image: mongo
entrypoint: >
/bin/bash -c '
openssl rand -base64 756 > /data/keyfile &&
chmod 400 /data/keyfile &&
chown mongodb:mongodb /data/keyfile &&
/usr/local/bin/docker-entrypoint.sh mongod --replSet rs0 --keyFile /data/keyfile --bind_ip_all'
healthcheck:
test: >
mongosh
-u $${MONGO_INITDB_ROOT_USERNAME}
-p $${MONGO_INITDB_ROOT_PASSWORD}
--eval "try { rs.status().ok } catch (e) { rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'localhost:27017' }] }).ok }"
start_period: 0s
interval: 500ms
timeout: 5s
retries: 5
ports: [27017:27017]
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=example
volumes:
- ./mongodb/data:/data/db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment