Tuesday, October 26, 2021

[SOLVED] Docker env variables not set while log via shell

Issue

In my settings file i am getting env variables like this

'NAME': os.environ['PG_DBNAME'],  # Database

I am setting in docker file like this

-e PG_DBNAME= "mapp"

Now

  1. The web app work fine
  2. If i log into shell via docker exec ... bash then env variables are also set

But if i log in via ipaddress and port number from ssh client then i am able to login but env variables are not set


Solution

As commented in issue 2569:

This is expected. SSH wipes out the environment as part of the login process.

One way to work around it is to dump the environment variables in /etc/environment (e.g. env | grep _ >> /etc/environment) before starting Supervisor.
Further "login processes" should source this file, and tada! There is your environment.

That env | grep _ >> /etc/environment could be part of a default run script associated (through ENTRYPOINT or CMD) to your image.


Daniel A.A. Pelsmaeker suggests jenkinsci/docker-ssh-agent issue 33 for an approach that selects and sets all environment variables excluding a specific denylist:

For my own uses I changed that line to the following:

env | egrep -v "^(HOME=|USER=|MAIL=|LC_ALL=|LS_COLORS=|LANG=|HOSTNAME=|PWD=|TERM=|SHLVL=|LANGUAGE=|_=)" >> /etc/environment

This takes all environment variables, except those listed, and appends then to /etc/environment, overriding any previously defined there.



Answered By - VonC