我正在Linux Docker容器中的Windows上工作。我正在尝试通过Gunicorn在Docker容器中运行Flask应用程序(使用docker-compose)。 Web应用程序会从import_app函数继续以状态8退出。
我尝试以管理员身份运行命令行,尝试更改docker-compose文件中的run命令,还尝试了不使用它运行项目的其余部分。这些都不起作用。
更新:
我尝试过更改端口,更改文件名(index.py
),重新安装Python的Docker映像,修剪我的旧Docker容器,在docker-compose中添加Python路径,以及
docker-compose.yml:
version: '3'
services:
web:
build: .
command: gunicorn "index:app" -b 0.0.0.0:8080
ports:
- "8888:8080"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
然后进行控制台回溯:
web_1 | [2018-12-19 20:36:50 +0000] [8] [ERROR] Exception in worker process
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
web_1 | worker.init_process()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
web_1 | self.load_wsgi()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
web_1 | self.wsgi = self.app.wsgi()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
web_1 | self.callable = self.load()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
web_1 | return self.load_wsgiapp()
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
web_1 | return util.import_app(self.app_uri)
web_1 | File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
web_1 | __import__(module)
web_1 | ModuleNotFoundError: No module named 'index'
web_1 | [2018-12-19 20:36:50 +0000] [8] [INFO] Worker exiting (pid: 8)
web_1 | [2018-12-19 20:36:50 +0000] [1] [INFO] Shutting down: Master
web_1 | [2018-12-19 20:36:50 +0000] [1] [INFO] Reason: Worker failed to boot.
flaskpython_web_1 exited with code 3
Dockerfile:
FROM python:3
# set environment variables
ENV PYTHONUNBUFFERED 1
ENV FLASK_APP=index.py
# copy current directory into working directory
COPY . /code
WORKDIR /code
# install python packages
RUN pip install -r requirements.txt
expose 8080
CMD ["gunicorn", "index:app", "-b", "0.0.0.0:8080"]
最后是项目的结构:
|docker-compose.yml
|Dockerfile
|index.py
|requirements.txt
预期结果是Web服务器不会在所有工作进程退出时崩溃或退出。我希望它能够认识到索引是我进入项目其余部分的入口。
答案 0 :(得分:1)
这似乎特定于Windows的docker。在here中进行讨论。简而言之,默认情况下,docker-machine挂载C:\users\[USERNAME]
,因此无法挂载位于其他地方的位置-绑定挂载为空。
对于该解决方案,您有几种选择:
从docker-compose文件中删除音量选项。这应该没问题,因为您已经将所需的所有内容添加到Dockerfile
中的图像中。
将项目放在主目录C:\users\[USERNAME]
中,然后从那里运行所有内容。
转到docker设置,然后将共享驱动器设置为所需的驱动器,可能是 C 。更多详细信息,here。
答案 1 :(得分:0)
Gunicorn将在容器内的index.py
上寻找PYTHONPATH
。尝试在PYTHONPATH
中将docker-compose.yml
设置为环境变量:
version: '3'
services:
web:
build: .
command: gunicorn "index:app" -b 0.0.0.0:8080
environment:
PYTHONPATH: '/code'
...