django - 通过 shell 记录时未设置 Docker 环境变量

标签 django ubuntu docker

在我的设置文件中,我得到了这样的环境变量

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

我正在像这样在 docker 文件中设置

-e PG_DBNAME= "mapp"

现在

  1. 网络应用运行良好
  2. 如果我通过 docker exec ... bash 登录到 shell,那么 env 变量也会被设置

但是如果我通过 ssh 客户端的 ipaddress 和端口号登录,那么我可以登录但未设置 env 变量

最佳答案

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

那个env | grep _ >>/etc/environment 可能是与您的图像关联(通过 ENTRYPOINTCMD)的默认运行脚本的一部分。


Daniel A.A. Pelsmaeker建议jenkinsci/docker-ssh-agent issue 33对于选择和设置除特定拒绝名单之外的所有环境变量的方法:

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.

关于django - 通过 shell 记录时未设置 Docker 环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34630571/

相关文章:

python - 如何在 Django SessionWizardView 中重新初始化 session 变量

python - 新手 Django 选择菜单 'function' 对象不可迭代

python - python中的列表索引超出范围

linux - 在 Linux (mono) 上运行的 TeamCity NUnitLauncher 出现 "Corlib not in sync with this runtime"错误

docker - Keycloak 8 : User with username 'admin' already added

java - com.mongodb.MongoSocketOpenException : Exception opening socket 异常

python - 分页 Django 表单集

python - 如何编写 Python Django 查询并导入它们?

python-3.x - Python 卡在计算中

python - docker swarm 抛出错误 "swarm already part of swarm"