原文地址:https://stackoverflow.com/questions/41750366/build-docker-image-of-a-python-flask-app
I'm trying to build a Docker image for a Python Flask app but having build problems - all files live in a folder called web
- this is the project structure:
web/
__init__.py
app.py
Dockerfile
models.py
requirements.txt
and app.py
at the moment looks like this:
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')
I've borrrowed the Dockerfile
from https://www.smartfile.com/blog/dockerizing-a-python-flask-application/:
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"]
I'm trying to build the Docker image using docker build --no-cache --rm -t flask-app
, but it ends with an error message:
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
I think a very small change on your Dockerfile would fix this:
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"]
I just added the full path to requirements.txt
, this could be accomplished in several different ways, like copying the entire directory folder and then building it.
微信公众号: 架构师日常笔记 欢迎关注!