Skip to content

Docker

Two images run MongoShield in Docker: the web console (a management UI: add a connection, browse it, back it up, restore) and the CLI (scripts, cron, CI). Both run on distroless Node.js 24 as a non-root user (uid 65532). The console comes first below; the CLI image starts at The CLI image.

Terminal window
docker run -p 127.0.0.1:8080:8080 --add-host=host.docker.internal:host-gateway \
-v mongoshield:/data ghcr.io/shamalrathnayake/mongoshield-console

Open http://localhost:8080. There is nothing to configure first. The two flags matter:

  • -p 127.0.0.1:8080:8080 publishes the console on this machine only. Until the admin exists, whoever opens the setup page first becomes the admin, so never publish it on every interface (-p 8080:8080) on a machine others can reach. For access from other machines, see Plain HTTP, HTTPS and other machines.
  • --add-host=host.docker.internal:host-gateway makes host.docker.internal (a MongoDB on this machine) work on Linux Docker Engine too. Docker Desktop and OrbStack already provide the name, and the flag is harmless there.
  1. Create the admin. For 15 minutes after the console starts, the setup page creates the first admin without a token. After that (or with MONGOSHIELD_SETUP_WINDOW_MINUTES=0) it needs the one-time link the console prints in its log (docker logs <container>), or a restart to open the window again.
  2. Paste a connection string, the console tests it and suggests a name and whether it is production (always shown, so you can change it), then opens the live data browser.
  3. Back up now writes an encrypted backup to the Local backups location (/data/backups), created on the first start. Restore a backup opens the restore wizard, and the backup can become a nightly schedule in one click.

What the first start does by itself, and how to do it yourself instead:

On first startHow to choose yourself
Generates the secret that encrypts the stored connection strings, credentials and backup keys, in /data/secret.key (32 random bytes, mode 0600). The log says so.-e MONGOSHIELD_SECRET="$(openssl rand -hex 32)", or MONGOSHIELD_SECRET_FILE=/run/secrets/<name> for Docker/Compose/Kubernetes secrets, on a fresh volume
Opens the 15-minute setup windowMONGOSHIELD_SETUP_WINDOW_MINUTES (0 = token only), or create the admin from the environment: MONGOSHIELD_ADMIN_USERNAME (or _EMAIL) + MONGOSHIELD_ADMIN_PASSWORD or MONGOSHIELD_ADMIN_PASSWORD_FILE (headless installs; ignored once any user exists)
Creates the Local backups storage locationMONGOSHIELD_DEFAULT_STORAGE=false; add S3, GCS, Azure or SFTP under Storage at any time

The secret is the one thing that must not be lost or changed: a console started with a different MONGOSHIELD_SECRET than its data directory was created with refuses to start (it could not read anything it stored), and one started without the variable on a volume that was created with it refuses too, with a message saying so. Existing deployments that pass MONGOSHIELD_SECRET behave exactly as before.

Back up the volume, and keep that copy private. /data holds the console's state, the generated secret.key next to what it encrypts, and the local backups. A copy of the volume is therefore a copy of every stored connection string. Backups that must survive the loss of this machine belong in an off-machine location (add one under Storage). Every "Back up now" plan is encrypted with a key the console keeps; download its recovery key when the console offers it, and store it apart from the volume: with it, the CLI restores those backups without the console (--identity-file).

Inside a container, localhost is the console's own container, not your computer.

Where MongoDB runsConnection string
Another containerPut both on one network (docker network create backup, --network backup on both) and use the container's name: mongodb://mongo:27017/?replicaSet=rs0. Compose does this for you.
On the host machinemongodb://host.docker.internal:27017/?directConnection=true. Docker Desktop and OrbStack provide that name; on Linux it needs --add-host=host.docker.internal:host-gateway (in every example here), and mongod must listen on that interface (bindIp), not only on 127.0.0.1.
MongoDB Atlas or any serverPaste the string as Atlas shows it (see below).

mongodb+srv:// strings work from the image as they are: Node.js resolves the SRV and TXT records (the TXT record's replicaSet and authSource are applied) and TLS is checked against Node's bundled certificate authorities, which include Atlas's. When you paste a string from Atlas's "Connect" dialog, the console asks for the password the string leaves out (<db_password>), in a separate masked field, and encodes special characters for you; it also accepts the mongosh "…" command line. When the connection fails, it says why in plain words: the IP access list (with an optional, opt-in lookup of the console's public IP to add under Network Access), a wrong password, a paused cluster, a typo in the cluster name, TLS, or missing roles. More in the Atlas guide.

For a server whose certificate comes from your own certificate authority, mount the CA and name it in the connection string:

Terminal window
docker run -p 127.0.0.1:8080:8080 --add-host=host.docker.internal:host-gateway -v mongoshield:/data \
-v "$PWD/ca.pem:/certs/ca.pem:ro,z" ghcr.io/shamalrathnayake/mongoshield-console
# connection string: mongodb+srv://user:pass@cluster0.example.com/?tlsCAFile=/certs/ca.pem

z in :ro,z is for hosts with SELinux enforcing (Fedora, RHEL, CentOS): Docker relabels the file so the container may read it. Without it the console gets "permission denied" on the CA file, which surfaces as a TLS error. Elsewhere it is ignored. Use it only on files and folders you mount for MongoShield, never on system folders such as /home or /etc.

docker/smoke/console-srv-tls.sh proves this end to end without Atlas: CoreDNS on the Docker network serves the SRV and TXT records, mongod requires TLS with a private CA, and the console (started with --dns) connects with tlsCAFile, and explains the failure as a TLS problem without it.

Session cookies are Secure by default. Browsers accept them over plain HTTP only on localhost (Chrome, Edge and Firefox treat it as a secure origin), so http://localhost:8080 works, but http://192.168.1.20:8080 from another machine cannot sign in. For access from other machines, put a TLS reverse proxy (Caddy, Traefik, nginx, an ingress) in front and set MONGOSHIELD_PUBLIC_URL=https://…. For a quick evaluation on a trusted network only, MONGOSHIELD_COOKIE_SECURE=false allows plain HTTP.

The console runs as uid 65532. A named volume (-v mongoshield:/data) is prepared for that user automatically; a bind-mounted host folder is not, and the console then cannot write its secret.key. Give the folder to that user first (and add z on SELinux hosts):

Terminal window
mkdir -p ./mongoshield-data && sudo chown 65532:65532 ./mongoshield-data
docker run -p 127.0.0.1:8080:8080 --add-host=host.docker.internal:host-gateway \
-v "$PWD/mongoshield-data:/data:z" ghcr.io/shamalrathnayake/mongoshield-console

Rootless Docker and Podman map container users to a range of host ids, so there use podman unshare chown 65532:65532 ./mongoshield-data (or a named volume) instead.

docker stop and Ctrl-C (under docker run -it) stop the console within seconds, exit code 0; running jobs get MONGOSHIELD_SHUTDOWN_GRACE_SECONDS (60) to finish, so give docker stop -t 70 when a backup may be running. The image has a HEALTHCHECK on /healthz. A Compose file with MongoDB and the console is in examples/console-compose. Every setting is listed in the console specification and the security checklist explains the trade-offs above.

The CLI image runs the MongoShield CLI on distroless Node.js 24 as a non-root user.

ghcr.io/shamalrathnayake/mongoshield:next # latest build from main
ghcr.io/shamalrathnayake/mongoshield:2.0.0 # a release (also :2.0, :2, :latest)

Images are multi-arch (amd64, arm64), signed with cosign, and ship an SBOM and SLSA provenance. The entrypoint is the mongoshield CLI and the default command is schedule, so any CLI command works as the container's arguments.

Terminal window
docker run --rm --init --add-host=host.docker.internal:host-gateway \
-e MONGODB_URI='mongodb+srv://backup:…@cluster0.example.mongodb.net/' \
-e MONGOSHIELD_STORAGE='s3://acme-backups/prod?region=eu-west-1' \
-e MONGOSHIELD_RECIPIENTS='age1…' \
-e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY \
ghcr.io/shamalrathnayake/mongoshield:2 backup

--init lets Ctrl-C and docker stop interrupt the command promptly (Node.js as PID 1 gets no default signal handling). --add-host makes host.docker.internal reach a MongoDB on the host machine on Linux too (see Connecting to MongoDB); it changes nothing for Atlas or other servers.

Backing up to a local directory instead: mount a volume at /backups, which the image prepares for its non-root user (uid 65532):

Terminal window
docker run --rm --init --add-host=host.docker.internal:host-gateway -e MONGODB_URI \
-v mongoshield-backups:/backups \
ghcr.io/shamalrathnayake/mongoshield:2 backup --to /backups

A bind-mounted host directory must be writable by uid 65532, and on SELinux hosts needs z (see A host folder instead of a named volume):

Terminal window
mkdir -p ./backups && sudo chown 65532:65532 ./backups
docker run --rm --init --add-host=host.docker.internal:host-gateway -e MONGODB_URI \
-v "$PWD/backups:/backups:z" \
ghcr.io/shamalrathnayake/mongoshield:2 backup --to /backups

Or run as your own user instead: --user "$(id -u):$(id -g)".

schedule stays running and backs up on schedule.cron from the config file, with locking, verification, retention and notifications (see the scheduling guide). The working directory is /app, where the CLI looks for mongoshield.config.{ts,js,mjs,json}:

Terminal window
docker run -d --name mongoshield --restart unless-stopped --add-host=host.docker.internal:host-gateway \
-e MONGODB_URI -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY \
-v "$PWD/mongoshield.config.json:/app/mongoshield.config.json:ro,z" \
ghcr.io/shamalrathnayake/mongoshield:2
{
"storage": "s3://acme-backups/prod?region=eu-west-1",
"encryption": { "recipients": ["age1…"] },
"retention": { "last": 3, "daily": 7, "weekly": 4, "monthly": 12 },
"schedule": { "cron": "15 3 * * *", "verify": "fast" },
"heartbeat": "https://hc-ping.com/your-uuid"
}

A TypeScript config with import { defineConfig } from "mongoshield" works too: the package is resolvable from /app.

Several containers may share one bucket (one per database or cluster, say). Retention is scoped: each scheduler only prunes backups of its own source deployment and its own backup.include/exclude selection, never the other containers' backups. To apply a policy to everything in the bucket at once, run docker run --rm … mongoshield prune --all-sources --daily 7 (it lists what it would delete per source and needs --yes without a terminal).

On docker stop, the scheduler lets a running backup finish (up to a grace period) or aborts it cleanly. Give it time with docker stop -t 120.

There is no HEALTHCHECK: a process that answers is not the same as backups that succeed. Use heartbeat (healthchecks.io, Cronitor, Better Stack…) or notify to be told when backups stop arriving.

A full Compose stack (MongoDB, MinIO, the scheduler) is in examples/docker-compose.

VariableMeaning
MONGODB_URI or MONGOSHIELD_URIconnection string
MONGOSHIELD_URI_FILEa file holding the connection string (a mounted secret); used when neither variable above is set
MONGOSHIELD_STORAGEs3://bucket/prefix?region=…&endpoint=…&forcePathStyle=true, or a path
MONGOSHIELD_RECIPIENTScomma-separated age public keys to encrypt for
MONGOSHIELD_PASSPHRASEpassphrase encryption instead of recipients
MONGOSHIELD_IDENTITY / MONGOSHIELD_IDENTITY_FILEage secret key (or a file with it) for restore and verify --deep
MONGOSHIELD_COMPRESSIONzstd (default), gzip or none
AWS_*S3 credentials and region, read by the AWS SDK

Environment variables override the config file; command-line flags override both.

Terminal window
docker run --rm -it --init --add-host=host.docker.internal:host-gateway -e MONGODB_URI \
-e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY \
-v "$HOME/backup.key:/run/secrets/backup.key:ro,z" -e MONGOSHIELD_IDENTITY_FILE=/run/secrets/backup.key \
ghcr.io/shamalrathnayake/mongoshield:2 \
restore latest --from s3://acme-backups/prod --rename 'app.*=app_restored.*'

The published image includes the AWS SDK (S3 and S3-compatible storage, AWS KMS). Build with other cloud SDKs from the repository root:

Terminal window
docker build -f docker/Dockerfile --build-arg CLOUD_SDKS=gcs,azure -t mongoshield:custom .

CLOUD_SDKS takes a comma-separated list of s3, gcs, azure, sftp, or all / none. Approximate sizes: none ~160 MB, s3 ~180 MB, all ~310 MB (uncompressed).

The image has no shell. To debug, build --target deps (Debian slim with the same node_modules), or swap the runtime base for gcr.io/distroless/nodejs24-debian13:debug-nonroot.

Check that the image was signed by this repository's Docker workflow, running on main (releases are built there) or on a release tag, and not merely by any workflow of the repository:

Terminal window
cosign verify ghcr.io/shamalrathnayake/mongoshield:2 \
--certificate-identity-regexp '^https://github\.com/ShamalRathnayake/mongoshield/\.github/workflows/docker\.yml@refs/(heads/main|tags/(mongoshield@|v)[0-9].*)$' \
--certificate-oidc-issuer https://token.actions.githubusercontent.com

For a release image you can be stricter still: --certificate-identity https://github.com/ShamalRathnayake/mongoshield/.github/workflows/docker.yml@refs/heads/main.