Search code examples
dockerdockerfiledocker-entrypoint

ENTRYPOINT Script Variables


I am looking at assigning variables to an entry point script at runtime. I am working on dockerizing one of our internal applications. In my ENTRYPOINT script I have defined some logic to create a database.php file which will include the DB Username and Password. I'd like to run something similar to the following. How do I define DB_USERNAME and DB_PASSWORD as values read on run time?

docker run -d --name some-app -e DB_USERNAME=secret_username -e DB_PASSWORD=securepassword


Solution

  • The trick is to ensure you use the shell form of (preferably) ENTRYPOINT:

    FROM busybox
    
    ENTRYPOINT echo "Hello ${DOG}"
    

    Then:

    docker build --rm --file="Dockerfile" --tag58944222:latest .
    docker run --interactive --tty --env=DOG=Freddie  58944222:latest
    

    Returns:

    Hello Freddie
    

    Updated

    Unclear why this was down-voted.

    Hopefully this will help:

    #!/bin/sh
    
    echo "Hello ${DOG}"
    

    And:

    FROM busybox
    
    ENV DOG=Henry
    
    COPY ./test.sh .
    RUN chmod +x ./test.sh
    
    ENTRYPOINT ./test.sh
    

    Returns the same results as before. The addition of the ENV Dog=Henry to the Dockerfile serves to provide default values:

    docker run --interactive --tty 58944222:latest
    Hello Henry
    docker run --interactive --tty --env=DOG=Freddie 58944222:latest
    Hello Freddie