Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables via env-file

I have a Dockerfile based on the spring guide about docker. My application consumes some private data, so I want to pass these parameters through environment variables. When I run a docker container:

docker run -p 8080:8080 -t myname/myapplication --env-file=~/env.list

it appears that the variables are not set and the application can't see them, what do I do wrong? How to pass these parameters?

env.list:

ACCOUNT_ID=my_account_id
ACCOUNT_PASSWORD=my_secret_password

My ENTRYPOINT:

ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar $APPLICATION_NAME
like image 674
neshkeev Avatar asked Mar 15 '23 06:03

neshkeev


2 Answers

I think

 docker run

takes all parameters before the image and the command. If I do

docker run -t --env-file=env.list ubuntu sh -c "while true; do echo world; sleep 100 ;done"

and then

docker exec -it container_id env

I get

HOSTNAME=195f18677a91
TERM=xterm
ACCOUNT_ID=my_account_id
ACCOUNT_PASSWORD=my_secret_password
HOME=/root

Try

docker run -p 8080:8080 --env-file=~/env.list -t myname/myapplication
like image 149
user2915097 Avatar answered Mar 23 '23 17:03

user2915097


This works very well:

cat <<EOF > test.env
MYVAR=test
EOF
docker run -it --env-file test.env busybox env | grep MYVAR

That will print as expected:

MYVAR=test

In your case in your Java application you can access the environment variables via System.getenv().

like image 25
Henrik Sachse Avatar answered Mar 23 '23 16:03

Henrik Sachse