Search code examples
docker-composedockerfileentry-pointdocker-entrypoint

How to access entrypoint.sh values in my docker-compose file?


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.

Solution

  • 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}