For production purposes, we recommend using publicly trusted certificates issued by a Certificate Authority (CA).
Components
- Mongo image built by Minimus: MongoDB container running with
requireTLSand client authentication. - mongosh installed.
- Dynamic certificate generation via OpenSSL:
- certgen.sh script: Shell script that generates a custom CA, server, and client certificates using OpenSSL.
- minidebug image: A Minimus dev toolkit that provides a shell, OpenSSL, and other utilities used to generate the certificates.
What this guide demonstrates
- TLS handshake validation
- Server/client certificate trust
- Basic auth and MongoDB operations
- Image compatibility
Directory Structure
Deploy Mongo with TLS certificates
Step 1: Generate TLS certificates
1
Save script that generates TLS certificates
Save the following script to a file named
certgen.sh. The script is used to generate the TLS certificates and store them in a certs folder on the host.certgen.sh
2
Save Docker Compose configuration
Save the following YAML file to run with Docker Compose. It uses the Minimus minidebug image to generate the certificates with the
certgen.sh shell script. Minidebug is a Minimus dev toolkit that provides a shell, OpenSSL, and other utilities. The certificates will be persisted in the certs volume on the host.create-certs.yml
3
Generate certificates
Run the following to generate the certificates:
- CA certificate (
ca.pem) - Server certificates (
server-cert.pem,server-key.pem) - Client certificates (
client.pem,client-key.pem)
- Proper SANs for
mongoandlocalhost - Client certs with
clientAuth - Server certs with
serverAuth
Step 2: Deploy Mongo server
1
Save Mongo Compose script
Save the following Docker Compose script to a file named
docker-compose.yml. This script sets up the Mongo service with a healthcheck, mounts a volume with the certificates, maps port 27017, and connects the container to a custom network.docker-compose
2
Run Mongo
Start the Mongo container:
Step 3: Test your Mongo server
We will use mongosh, the mongo shell, to connect over HTTPS and run tests. For example, here are a few commands you can try out:-
Check db health
You should get the response
{ ok: 1 }. -
Create a test database (for example
testdb): -
List all databases.
-
Create document in
testdb.docs: -
Create a user (for example,
testuserwithreadWriterole ontestdb), get user details, and delete the user: -
Insert a new document:
-
Get all documents in a collection:
-
Delete a database: