构建 Python Flask 应用程序的 Docker 镜像

3

我正在尝试为Python Flask应用程序构建Docker镜像,但是遇到了构建问题-所有文件都位于名为web的文件夹中-这是项目结构:

web/
    __init__.py
    app.py
    Dockerfile
    models.py
    requirements.txt

目前,app.py 的代码如下:

from flask import Flask


app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello World!"


if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0')

我从https://www.smartfile.com/blog/dockerizing-a-python-flask-application/借用了Dockerfile:
FROM ubuntu:14.04

# Update OS
RUN sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y upgrade

# Install Python
RUN apt-get install -y python-dev python-pip

# Add requirements.txt
ADD requirements.txt /webapp

# Install wsgi Python web server
RUN pip install uwsgi
# Install app requirements
RUN pip install -r requirements.txt

# Create app directory
ADD . /webapp

# Set the default directory for our environment
ENV HOME /webapp
WORKDIR /webapp

# Expose port 8000 for uwsgi
EXPOSE 8000

ENTRYPOINT ["uwsgi", "--http", "0.0.0.0:8000", "--module", "app:app", "--processes", "1", "--threads", "8"]

我正在尝试使用docker build --no-cache --rm -t flask-app构建Docker镜像,但最终出现了错误信息:
Successfully installed uwsgi
Cleaning up...
 ---> 9bbc004212a3
Removing intermediate container 70ed8f07c408
Step 8/13 : RUN pip install -r requirements.txt
 ---> Running in f5e2eb59ffd1
Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
Storing debug log for failure in /root/.pip/pip.log
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1
3个回答

4
我认为您的Dockerfile只需要做出非常小的更改就可以解决这个问题:
FROM ubuntu:14.04

# Update OS
RUN sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y upgrade

# Install Python
RUN apt-get install -y python-dev python-pip

# Add requirements.txt
# Create app directory
ADD . /webapp

# Install wsgi Python web server
RUN pip install uwsgi
# Install app requirements
# Full path to requirements
RUN pip install -r /webapp/requirements.txt

# Set the default directory for our environment
ENV HOME /webapp
WORKDIR /webapp

# Expose port 8000 for uwsgi
EXPOSE 8000

ENTRYPOINT ["uwsgi", "--http", "0.0.0.0:8000", "--module", "app:app", "--processes", "1", "--threads", "8"]

我刚刚添加了requirements.txt的完整路径,这可以通过多种不同的方式实现,比如复制整个目录文件夹然后进行构建。


我遇到了类似的错误:成功安装uwsgi 清理中... ---> 42b208ea012c 正在删除中间容器308ffeea1f10 步骤8/13:运行pip install -r /webapp/requirements.txt ---> 正在运行dbe480989f42 无法打开要求文件:[Errno 20]不是目录:'/webapp/requirements.txt' 将失败的调试日志存储在/root/.pip/pip.log中 命令'/bin/sh -c pip install -r /webapp/requirements.txt'返回非零代码:1 - srm
我需要在本地的web文件夹中物理创建webapp子目录吗? - srm
我将 ADD . /webapp 移动到 pip install 语句上方了,你可以试一下吗? - Rafael Barros
好的,看起来似乎已经完成了,但是出现了另一个错误 - `Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip_build_root/scipy/setup.py", line 415, in <module>setup_package() File "/tmp/pip_build_root/scipy/setup.py", line 395, in setup_packagefrom numpy.distutils.core import setupImportError: No module named numpy.distutils.core----------------------------------------。这似乎是因为没有在要求中安装numpy。我认为scipy会安装包括numpy`在内的所有依赖项。 - srm

0

在添加要求之前,我替换了Dockerfile的初始行。

FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential

它成功地构建了镜像。

$ docker images
REPOSITORY          TAG                 IMAGE ID                CREATED             SIZE
flask-app           latest              32b81efb5719        2 minutes ago       644.9 MB
ubuntu              latest              104bec311bcd        5 weeks ago         129 MB
$
$ docker run -d flask-app
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
73ae8baa93c2        flask-app       "uwsgi --http 0.0.0.0"   6 seconds ago       Up 5 seconds        8000/tcp            lucid_heyrovsky
$
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' 73ae8baa93c2
172.17.0.2

因此,当我尝试指向URLhttp://172.17.0.2:8000/时,什么都没有显示。


首先,您可以检查该容器的 docker logs:确保您的 Flask 应用正在运行。接下来尝试使用 -p 选项启动容器,将容器端口映射到主机上,如下所示。docker run -p 8000:8000 -d flask-app - Cristian Baldi

0

添加

WORKDIR /webapp

之前

RUN pip install -r requirements.txt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接