大网数据广告活动
服务器运维问答

How to set environment variable within the docker file?

· 52CE 我爱测

The CMD of my dockerfile is this: ["python", "myproject/start_slide_server.py"] However for this to work, I need to set the PYTHONPATH to /app , which is the parent directory of myproject If I start the docker process and overrid…

问题原文

原帖链接 · Anthony Kong · CC BY-SA 3.0

The CMD of my dockerfile is this: ["python", "myproject/start_slide_server.py"]

However for this to work, I need to set the PYTHONPATH to /app, which is the parent directory of myproject

If I start the docker process and override CMD with bash, I can run the following

root@42e8998a8ff7:/app# export PYTHONPATH=.
root@42e8998a8ff7:/app# python myproject/start_slide_server.py
 * Running on http://0.0.0.0:8090/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 236-035-556

And it works as expected

Now I add the line

RUN export PYTHONPATH=/app

before

CMD ["python" , "myproject/start_slide_server.py"]

it just failed

Traceback (most recent call last):
  File "/app/myproject/start_slide_server.py", line 23, in <module>
    from myproject import env
ImportError: No module named myproject

It seems like the RUN line does not have any impact at all

I really do not want to define ENV at the dockercommand level because this PYTHONPATH will not change from one image to next.

How can I achieve this?


I can use the ENV directive

ENV PYTHONPATH /app

社区高分解答

原帖链接 · tashuhka · CC BY-SA 4.0

You should not over-write the PYTHONPATH with your path but append it; otherwise the system will not find the installed Python packages.

  • Option 1: RUN export PYTHONPATH="$PYTHONPATH:/app"
  • Option 2: ENV PYTHONPATH="$PYTHONPATH:/app"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。