0

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

oguz ismail
  • 1
  • 16
  • 47
  • 69
F12
  • 188
  • 3
  • 4
    That `docker run` command probably does what you want (remember to put all Docker options before the image name); is there a specific problem you’re having? – David Maze Nov 20 '19 at 00:12

1 Answers1

1

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
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • For your second response (test.sh example) are we able to define default values using CMD? In essence I would like to set these values so I can run the image without requiring variables then specify if needed. – F12 Nov 21 '19 at 19:40
  • Yes, just add e.g. `ENV DOG=Henry` to the Dockerfile to serve as defaults – DazWilkin Nov 21 '19 at 19:52
  • Pleased to hear it! – DazWilkin Nov 27 '19 at 01:54