0

How do I access entrypoint.sh values in docker-compose file. I have declared all the values in entrypoint.sh as shown below. I need to access those in my docker-compose file

I have used docker-volumes to copy the entrypoint.sh script to /usr/local/bin directory in the docker container.

entrypoint.sh script

MONGO_ROOT_USERNAME=root
MONGO_ROOT_PASSWORD=mongo@123
MONGO_EXPRESS_USERNAME=root
MONGO_EXPRESS_PASSWORD=express@123

docker-compose file

mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    volumes:
      - "./docker-scripts/entrypoint.sh:/usr/local/bin"
    environment:
      ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_EXPRESS_USERNAME}
      ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_EXPRESS_PASSWORD}
      ME_CONFIG_MONGODB_PORT: 27017
      ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_ROOT_USERNAME}
      ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_ROOT_PASSWORD}

But when I run docker-compose -d I get this message

WARNING: The MONGO_ROOT_USERNAME variable is not set. Defaulting to a blank string.
WARNING: The MONGO_ROOT_PASSWORD variable is not set. Defaulting to a blank string.
WARNING: The MONGO_EXPRESS_USERNAME variable is not set. Defaulting to a blank string.
WARNING: The MONGO_EXPRESS_PASSWORD variable is not set. Defaulting to a blank string.
jeril
  • 1,109
  • 2
  • 17
  • 35

1 Answers1

1

you forget to export your env variables:

export MONGO_ROOT_USERNAME=root
export MONGO_ROOT_PASSWORD=mongo@123
export MONGO_EXPRESS_USERNAME=root
export MONGO_EXPRESS_PASSWORD=express@123

Update1:

So firsty create a .env file, then add it to your docker compose

.env

MONGO_ROOT_USERNAME=root
MONGO_ROOT_PASSWORD=mongo@123
MONGO_EXPRESS_USERNAME=root
MONGO_EXPRESS_PASSWORD=express@123

Then on docker-compose.yml

mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    volumes:
      - "./docker-scripts/entrypoint.sh:/usr/local/bin"
     env_file:  # <-- Add this line
      - .env    # <-- Add this line
    environment:
      ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_EXPRESS_USERNAME}
      ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_EXPRESS_PASSWORD}
      ME_CONFIG_MONGODB_PORT: 27017
      ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_ROOT_USERNAME}
      ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_ROOT_PASSWORD}
J-Jacques M
  • 978
  • 6
  • 16
  • I want to access variables declared in bash script entrypoint.sh to be accessible in docker-compose. I tried with export but the docker-compose.yaml files does not read the values provided in the entrypoint.sh bash script. – jeril Feb 28 '20 at 04:06
  • Post updated, create env file then add it to your docker-compose – J-Jacques M Feb 28 '20 at 08:35