Docker Community Forums

Share and learn in the Docker community.

Env variable cannot be passed to container

docker

(Liusl) #1

I tried to use --env to pass environment variable to container, the command is like:
docker run --env P4EDITOR=“vim” -d -P -h ${HOSTNAME} -v /home/p4:/home/p4 myimage
(My image will start sshd service)

However, when I login container with ssh, I found the env variable was not passed in.
env|grep P4EDITOR

Anybody know why?


docker version
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d/1.7.1
OS/Arch (server): linux/amd64


(Kesaich) #2

What happens when you docker exec into the container? The ENV VAR should be there. When you ssh into the container, you connect via pseudo terminal and a different environment.


(Liusl) #3

Thanks for the reply, the exec output:
docker exec f3edd100d4f0 echo ${P4EDITOR}
vim

Yes, it looks it’s due to ssh session, any recommendation to pass the env variable to ssh session?


(Kesaich) #4

Can you give me more context? Why are you ssh-ing into a container?


(Liusl) #5

The docker image is to provide a public console and some other people can ssh on it to do some work.


(Kesaich) #6

I’m not sure what best practice is for this situation, but you can add this to your Dockerfile: RUN echo ‘export P4EDITOR=“vim”’ >> /etc/profile.d/vim_env.sh

All files in /etc/profile.d should get sourced. Let me know how it works out for you. You can also copy the .sh file instead of running echo every time you build a container.


(Liusl) #7

Thanks for your suggestion, but besides P4EDITOR, I still have some account env variable such as P4USER, each user should be able to ssh to the container, then do some work with his own account, I cannot hardcode it in dockerfile.


(Kesaich) #8

Is everyone going to ssh as the same user, but have different environment, or is everyone going to have their own account, aka their own $HOME?


(Liusl) #9

everyone will have their own account, and own password.


(Liusl) #10

I found a way to workaround it:
Put account related env variables to /root/.bashrc

Then add -v /root:/root to docker run parameters.