简体   繁体   English

在docker-compose文件中的docker容器中更改PYTHONPATH

[英]Change the PYTHONPATH in a docker container in docker-compose file

I have an application in a docker container whose entry point is defined as 我在Docker容器中有一个应用程序,其入口点定义为

ENTRYPOINT ["/usr/local/bin/gunicorn", "--pythonpath=`$PWD`/.."]

Then, I have three container processes which use that container and entry point to serve my files from the app. 然后,我有三个容器进程,它们使用该容器和入口点从应用程序中提供文件。 Everything fine there. 那里一切都很好。

I now am trying to start another container process that over rides the gunicorn command. 我现在正在尝试启动另一个gunicorn命令的容器过程。 I want it to run a python3 process with the command 我希望它使用以下命令运行python3进程

entrypoint: ["python3", "/crm/maintenance/maintenance.py"]

in the docker-compose.yml file. docker-compose.yml文件中。

The issue is when I run docker-compose up -d with the above entrypoint, all containers run fine except for the one running the python process. 问题是当我使用上述入口点运行docker-compose up -d时,除运行python进程的容器外,所有容器都运行良好。

The error I get is: 我得到的错误是:

Traceback (most recent call last):
File "/crm/maintenance/maintenance.py", line 6, in <module>
    from crm.sms_system.answer_send import AnswerSender
ImportError: No module named 'crm'

I attribute this error to the python path that remains incorrect. 我将此错误归因于仍然不正确的python路径。 For the Entrypoint defined in the docker file I have the "--pythonpath= $PWD /.." flag. 对于docker文件中定义的Entrypoint,我具有"--pythonpath= $ PWD /.."标志。 But this cannot transfer over to python3. 但这无法转移到python3。

Instead I have tried a number of things: 相反,我尝试了很多事情:

  1. In dockerfile ENV PYTHONPATH=$PWD/.. 在dockerfile中, ENV PYTHONPATH=$PWD/..
  2. In docker-compose.yml entrypoint: ["PYTHONPATH=/..","python3", "/crm/maintenance/maintenance.py"] 在docker-compose.yml entrypoint: ["PYTHONPATH=/..","python3", "/crm/maintenance/maintenance.py"]
  3. In docker-compose.yml entrypoint: ["PYTHONPATH=$PWD/..","python3", "/crm/maintenance/maintenance.py"] . 在docker-compose.yml entrypoint: ["PYTHONPATH=$PWD/..","python3", "/crm/maintenance/maintenance.py"] This does not work since the PWD is executed from where you run the docker-compose up command from not in the container. 这是行不通的,因为PWD是从不在容器中的位置运行docker-compose up命令的位置执行的。

How can I change the PYTHONPATH at run time in a container from the docker-compose file? 如何在运行时从docker-compose文件中的容器中更改PYTHONPATH?

You need to use $$ to escape the environment variable parsing for docker-compose. 您需要使用$$来转义docker-compose的环境变量解析。 Here is a sample file which worked for me 这是为我工作的示例文件

version: '2'
services:
  python:
    image: python:3.6
    working_dir: /usr/local
    command: bash -c "PYTHONPATH=$$PWD/.. python3 -c 'import sys; print(sys.path)'"

$ docker-compose up
Recreating test_python_1
Attaching to test_python_1
python_1  | ['', '/usr', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
test_python_1 exited with code 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM