【问题标题】:Build Docker image of a Python Flask app构建 Python Flask 应用程序的 Docker 映像
【发布时间】:2017-06-04 15:47:54
【问题描述】:

我正在尝试为 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

【问题讨论】:

    标签: python docker flask


    【解决方案1】:

    我认为对你的 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 的完整路径,这可以通过几种不同的方式完成,例如复制整个目录文件夹然后构建它。

    【讨论】:

    • 我收到类似的错误:Successfully installed uwsgi Cleaning up... ---> 42b208ea012c Removing intermediate container 308ffeea1f10 Step 8/13 : RUN pip install -r /webapp/requirements.txt ---> Running in dbe480989f42 Could not open requirements file: [Errno 20] Not a directory: '/webapp/requirements.txt' Storing debug log for failure in /root/.pip/pip.log The command '/bin/sh -c pip install -r /webapp/requirements.txt' returned a non-zero code: 1
    • 我是否必须在本地 web 文件夹中物理创建 webapp 子目录?
    • 我将ADD . /webapp 移到pip install 语句的上方,你可以试试吗?
    • 好的,它似乎已经完成但出现另一个错误 - 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_package from numpy.distutils.core import setup ImportError: No module named numpy.distutils.core ----------------------------------------。似乎这可能是因为numpy 不在要求中。我以为scipy 会安装任何依赖项,包括numpy
    【解决方案2】:

    在添加要求之前,我将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
    

    所以当我尝试指向 URL http://172.17.0.2:8000/ 时,我什么也得不到。

    【讨论】:

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

    添加

    WORKDIR /webapp
    

    之前

    RUN pip install -r requirements.txt
    

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 2018-03-09
      • 1970-01-01
      • 2020-06-09
      • 2022-09-28
      • 1970-01-01
      • 2022-01-13
      • 2021-07-11
      • 1970-01-01
      相关资源
      最近更新 更多