How can I pass Jenkins environment variables to docker container?

Viewed 21146

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!

3 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

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
/ #

Try to run docker-machine env in the Docker terminal to get a list of all the environment variables and their values.

Related