gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:56 25 4
gpt4 key购买 nike

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

最佳答案

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

关于python - 构建 Python Flask 应用程序的 Docker 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41750366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com