Jeff B
Jeff B

Reputation: 141

docker-compose mongodb access env variables in /docker-entrypoint-initdb.d/ script

This question is based off the top answer to a previous question on the same topic

My question is, in my custom /docker-entrypoint-initdb.d/ init script, how can I reference env variables that are declared in docker-compose's .env file? Meaning, env variables besides MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD.

for example:

mongo --eval "db.getSiblingDB('sibling').createUser({user: '$SIBLING_USER', pwd: '$SIBLING_PASSWORD', roles: [{ role: 'readWrite', db: 'sibling' }]})"

Upvotes: 1

Views: 1228

Answers (1)

PekosoG
PekosoG

Reputation: 264

I did the following for an Reverse Proxy using NGINX where based on a env variable it loads a different config file.

Docker-compose.yml:

https-proxy:
build: 
  context: ./https-proxy
  dockerfile: ./Dockerfile
  args:
    - MY_VAR=TRUE

Dockerfile:

FROM nginx

ARG MY_VAR
ENV MY_VAR=${MY_VAR}
RUN bash ./etc/nginx/config.sh

config.sh:

#!/bin/bash
if [ $MY_VAR == true ]; then
    echo 'My Var is True'
else
   echo 'My Var is False'

You could also define an .env file aside your Docker-compose.yml, so you don't have to change that file and only define the values on a different place where Docker-compose will look for them

Upvotes: 1

Related Questions