How can I pass Jenkins environment variables to docker container? - docker

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.
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!

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

docker-compose run env variable do not exists

Do you know why the behavior of environment variables in docker run vs docker-compose run is handled differently?
When I look at the container after a docker-compose run with the docker-compose exec command, I cannot find my defined environment variables running the command "env".
$ a="test hello" docker-compose run -d app
$ docker-compose exec app
/code env
# HOSTNAME=ce1b4934lkj
# HOME=/
I thought the env variables would be available as long as the container is running.
Running the same test with docker run and docker exec, I get the expected result.
$ docker run --env a=tst --name alpy -dit alpine
$ docker exec -it alpy env
# a=tst
# HOME=/root
EDIT
If I use the docker-compose run syntax, the env variable is written to the container. But I don't understand why I can no longer see this env varialbe with my subsequent exec command. I am executing this command in the same container.
$ docker-compose run -e another=var -d app env
# /code env & exit
# another=var
$ docker-compose exec app
/code env
# HOSTNAME=ce1b4934lkj
# HOME=/

Alpine execute command in a new shell

I'm using an Alpine image in docker and I wanted to know if there is a command to open a new terminal and execute a command.
Like :
gnome-terminal -e <command>
I've already searched in ash man but didn't find what I wanted
You always have a choice of running commands on running containers irrespective of the OS type.
docker image pull nginx
docker container run -d --name nginx -p 80:80 nginx
docker exec -ti nginx sh -c "echo 'Hello World'"

Entering docker container with exec losing PATH environment variable

Here is my Dockerfile:
FROM ros:kinetic-ros-core-xenial
CMD ["bash"]
If I run docker build -t ros . && docker run -it ros, and then from within the container echo $PATH, I'll get:
/opt/ros/kinetic/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
If I exec into the container (docker exec -it festive_austin bash) and run echo $PATH, I'll get:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Why are the environment variables different? How can I get a new bash process on the container with the same initial environment?
The ENTRYPOINT command is only invoked on docker run, not on docker exec.
I assume that this /ros_entrypoint.sh script is responsible for adding stuff to PATH. If so, then you could do something like this for docker exec:
docker exec -it <CONTAINER_ID> /ros_entrypoint.sh bash
docker exec only gets environment variables defined in Dockerfile with instruction ENV. With docker exec [...] bash you additionally get those defined somewhere for bash.
Add this line to your Dockerfile:
ENV PATH=/opt/ros/kinetic/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
or shorter:
ENV PATH=/opt/ros/kinetic/bin:$PATH
This is old question but since it's where google directed me I thought I'll share solution I ended up using.
In your entrypoint script add a section similar to this:
cat >> ~/.bashrc << EOF
export PATH="$PATH"
export OTHER="$OTHER"
EOF
Once you rebuild your image you can exec into your container (notice bash is invoked in interactive mode):
docker run -d --rm --name container-name your_image
docker exec -it container-name /bin/bash -i
If you echo $PATH now it should be the same as what you have set in .bashrc

How can we access variables present inside the docker container from host machine

For example, if inside docker container I create a variable as -
/# token="dsfgkd-sdasdas-fas3ad-ssssad"
exit
root#testvm:~# echo $token
//how to get the result..?
root#testvm:~#
Containers are isolated from the host, but the host can connect inside the container
If you create your variable and export it, it will be available for your container and the connections coming from a
docker exec -it container_name_or_id bash
or
docker exec -it container_name_or_id echo $token
you can see the environment variables in your container with
docker exec -it container_name_or_id env
if you just create it in your process, it will be available for your process only
The ENV directive in a Dockerfile is designed for creating ernvironment variables at build time
see the doc
https://docs.docker.com/engine/reference/builder/#env
At run time, you have
docker run -e
extract from
https://docs.docker.com/v1.11/engine/reference/run/
docker run -e "deep=purple" --rm ubuntu /bin/bash -c export
and
docker run --env-file
see from
https://docs.docker.com/engine/reference/commandline/run/
--env-file Read in a file of environment variables

Vagrant docker provisioning with env variables and network

I have a docker image, which I wish to run inside vagrant. The manual run command looks like this :
docker run --name container1 --network=host -p 1111:9990 -e "USER=dummy" -e "PASS=P$SSW0RD" image_name paramToEntrypoint
How can I translate it to the vagrant docker provisioner?
To be more specific, I am not sure:
How to specify the host
How to pass env variables to docker
How to pass parameter to dockers entrypoint

Resources