K
K
kovalr2019-01-23 17:16:13
linux
kovalr, 2019-01-23 17:16:13

How to pass an environment variable to a Docker container?

Hello,
for example, I have a Dockerfile

FROM ubuntu:14.04
ARG SOME_PASSWORD
RUN echo $SOME_PASSWORD >> file.password
ADD docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x  /docker-entrypoint.sh

ENTRYPOINT /docker-entrypoint.sh

Create an image
docker build -t test_image .
Then I need to create a container in such a way that in the file file.password pass the password through the environment variable
docker run  -e SOME_PASSWORD='password12345' --restart=always -it test_image

How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2019-01-23
@kovalr

This variable will be set in your /docker-entrypoint.sh file. It remains only to put it in the desired file

cat > /password << EOL
password=$SOME_PASSWORD
EOL

S
Saboteur, 2019-01-23
@saboteur_kiev

So just read the variable from the file and pass it through the standard -e
We put the password in the file just by the value

echo "mysecretpassword" > file.password
...
MYVAR=$(cat file.password)
docker run  -e SOME_PASSWORD="$MYVAR" --restart=always -it test_image

or put the password in a shell script and call it via source (aka .)
echo "MYVAR=mysecretpasswowrd">file.password
...
. file.password
docker run  -e SOME_PASSWORD="$MYVAR" --restart=always -it test_image

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question