I've ran my Docker container using this command:
docker run --name test1 -d -e FLAG='***' rastasheep/ubuntu-sshd
Now, when I connect to it via SSH, I can't get my env there via printenv FLAG
.
How can I fix this? When running with -it
and sh
, I can my get env via printenv FLAG
.
I can't get env var in the Docker container
6.5k Views Asked by Dmitry Sharshakov At
3
There are 3 best solutions below
0

As suggested by docs you might need to create your own Dockerfile with following changes
Project
|--Dockerfile
|--entrypoint.sh
Dockerfile
FROM rastasheep/ubuntu-sshd
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["/usr/sbin/sshd", "-D"]
File: entrypoint.sh
#!/bin/bash
echo "export FLAG=$FLAG" >> /etc/profile
exec "$@"
Command:
docker build -t your-ubuntu-sshd .
docker run --name test1 -d -e FLAG='abc' -p 2222:22 your-ubuntu-sshd
You are doing two different things:
docker run -it -e FLAG='***' rastasheep/ubuntu-sshd sh
will run a container in interactive mode with a shell, and this shell session will have the environment variable you passed on the command line. Withdocker run -d -e FLAG='***' rastasheep/ubuntu-sshd
, a SSH daemon process will start with defined env vars.This can be observed when running a container, connecting to it using ssh and showing all processes and their environment variable:
We are now connected into the container, we can see the SSH daemon process (PID 1) and our SSH session process (PID 7):
Lets check it out, print our current process env var, and the env var of the SSH daemon process:
As pointed out by @Dmitrii, you can read Dockerize an SSH service for more details.