Here's a solution I've used in cases like this - I utilize Ansible to manage Docker containers, and Ansible Vault to store secrets for those containers.
Ansible Playbook to run MongoDB container
Your playbook.yml
may look something like this:
- name: run mongodb docker container
docker_container:
name: mongo-container
image: mongo
ports:
- "27017:27017"
env:
MONGO_INITDB_DATABASE: "{{secret_db_name}}"
MONGO_INITDB_ROOT_USERNAME: "{{secret_db_user}}"
MONGO_INITDB_ROOT_PASSWORD: "{{secret_db_pass}}"
- As you might notice, the
docker_container
syntax looks a lot like what you'd write in a Docker Compose YML file.
- The difference is that your secrets are managed in variables (the
{{}}
is Ansible Jinja2 variables).
- Here's a list of Ansible Modules for interacting with Docker.
Vault file to manage your mongodb
secrets
The vault.yml
file would contain the definitions of your secrets in an encrypted form.
secret_db_name: foodb
secret_db_user: foo
secret_db_pass: bar@123
You can use ansible-vault
commands to create encrypted files (for e.g in version-control).
Getting it all together
When you want to run your Docker container, you would run an Ansible command
ansible-playbook playbook.yml --ask-vault-pass
Which would
- ask you for your vault password file,
- decrypt the vault file and pass the variables to the playbook;
- run the docker mongodb container with your secret credentials
All whilst ensuring your credentials are not publicly visible.
Notes
- I've used this approach to provision Dockerized databases on remote managed servers in a declarative way.
- You introduce one more tool, a 'wrapper' around Docker. In my experience DevOps toolchains work better than utilizing single tools. However, this may or may not be a constraint for you.