docker-compose run env variable do not exists - docker

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

Related

Docker entrypoint to run commands with different users in interactive mode

I have a custom image with docker installed (docker-in-docker). When running the image, the user needs to be $USERNAME (and not root). However the docker service required root to be started.
Getting docker to run as non-root seems to be overly complicated. So I have attempted to use su in the entry-point instead, which works, but it is not interactive.
FROM ubuntu:18.04
# ... A lot of steps here to install stuff that are not really relevant to the problem.
COPY container-helpers/entrypoint.sh .
USER root
ENV ENTRYUSER $USERNAME
ENTRYPOINT [ "./entrypoint.sh" ]
CMD "pulumi up"
And entrypoint.sh is:
#!/bin/bash
set -e
service docker start
export ENV_PATH=$PATH
su $ENTRY_USER -lp <<EOSU
set -e
export PATH=$ENV_PATH
. $NVM_DIR/nvm.sh
pulumi stack select -c dev
npx meteor-deploy stack configure default
$# # Run given argument as a command
EOSU
I run it as:
$ docker run --env-file local.env --privileged -it meteor-deploy-leaderboard
* Starting Docker: docker [ OK ]
Logging in using access token from PULUMI_ACCESS_TOKEN
error: --yes must be passed in to proceed when running in non-interactive mode
Or, if you don't want to take pulumi's word for it:
$ docker run --env-file local.env --privileged -it meteor-deploy-leaderboard bash; echo "exited"
* Starting Docker: docker [ OK ]
Logging in using access token from PULUMI_ACCESS_TOKEN
exited
Any idea how I can pass on the tty to the su command properly?

Exporting a environment variable in Entrypoint file not work?

I have some problems with exporting an environment variable into docker Entrypoint file.
This is my docker file content:
FROM ubuntu:16.04
ADD entrypoint.sh .
RUN chmod 777 entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
CMD ["/bin/bash"]
In the Entrypoint file, I try to run command "export TOKEN=$client_token". Then, I create a container with that image file and I run "docker exec -it /bin/bash" command and into the container I continue run "set" command to show all environment variables. So,I can not find the $TOKEN variable that I exported before.
How can I export an environment variable into the entrypoint file?
You must inject your host environment variable (client_token) into the docker container using '-e' when running:
docker run -it --rm -e client_token=<whatever> <your image>
This works for example with this kind of entrypoint:
#!/bin/bash
export TOKEN=$client_token
echo "The TOKEN is: ${TOKEN}"
# do stuff ...
If you don't know the token value when the container was run, you should inject during attachment (docker exec) and perform required operations inside, but probably it is not valid for you if running container already needed that information.
docker exec -it -e TOKEN=<whatever> <your container>
BRs

How to get enviroment variables injected via -e key in entrypoint script?

I need to launch my app on the port, set via -e key in docker run command
I run my app in ENTRYPOINT script and try to get $PORT env variable, but there no any env variable, set via -e keys.
Serving the app in Dockerfile
ENTRYPOINT ["sh", "entrypoint.sh"]
entrypoint.sh script:
#!/bin/bash
func start --port $PORT
Docker run command:
docker run -d -p 20937:8081 --name queue_0_middleware -e WEBSITE_CORS_ALLOWED_ORIGINS=https://functions.azure.com,https://functions-staging.azure.com,https://functions-next.azure.com -e PORT=8081
If I run this command locally I add image name like this: sudo docker run -p 15615:8081 30c7bb13d4b4 --name queue_2_middleware -e PORT=8081
That won't do what you expect, the docker command line is order sensitive. Everything after the image name is used to replace the value of CMD inside your image. With the entrypoint defined, those are just args to your entrypoint script. In other words, the docker command looks like:
docker run ${args_to_run} ${image_name} ${cmd_override}
The fix is to reorder your command with the args to run placed before the image name:
sudo docker run -p 15615:8081 --name queue_2_middleware -e PORT=8081 30c7bb13d4b4

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

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

Resources