Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass Jenkins environment variables to docker container?

I am using Jenkins pipeline to build and deploy a docker container.

What I want to do is to pass Jenkins environment variables to docker container.

See the screenshot.

I defined the environment variables on Jenkins.

enter image description here

I check the docker container environment variables using the following commands

- docker exec -it docker_id bash (get into the docker)
- printenv (print environment variables)

I want to see the Jenkins environment variables in docker container.

Is this possible? If so, please let me know the way to do it.

Thanks in advance!

like image 991
wagng Avatar asked Nov 07 '18 12:11

wagng


2 Answers

you can set all your variables in .env file and give it to when docker going to run your container:

TBS_END_POINT=http://192.168.82.2:2020/QWEr
MONGO_HOST=172.17.0.3
MONGO_PORT=27017
REPLICA_SET=replicaset
API_PORT=3001
REDIS_PORT=4432
NODE_ENV=development

then when you want to run you container say:

docker run --env-file .env MyImage
like image 194
amir Avatar answered Oct 19 '22 18:10

amir


Did you try

docker run -e

https://docs.docker.com/engine/reference/run/#env-environment-variables

$ export today=Wednesday
$ docker run -e "deep=purple" -e today --rm alpine env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=d2219b854598
deep=purple
today=Wednesday
HOME=/root

or

$ docker run -d --name tst -e TESTME=YES busybox tail -f /dev/null
55a3fe206588a8030365df30a670ca89a50c54816d044184d5870a4a76ce8199
$ docker exec -it tst sh
/ # echo $TESTME
YES
/ #
like image 20
Moisei Avatar answered Oct 19 '22 17:10

Moisei