简体   繁体   中英

How can I run a Docker container with Python3.7 and Pipenv for a Flask app?

My Dockerfile is:


FROM ubuntu:18.04
RUN apt-get -y update
RUN apt-get install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update -y
RUN apt-get install -y python3.7 build-essential python3-pip
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
RUN pip3 install pipenv
COPY . /app
WORKDIR /app
RUN pipenv install
EXPOSE 5000
CMD ["pipenv", "run", "python3", "application.py"]

When I do docker build -t flask-sample:latest. , it builds fine (I think).

I run it with docker run -d -p 5000:5000 flask-sample and it looks okay

But when I go to http://localhost:5000 , nothing loads. What am I doing wrong?

Why do you need a virtual environment? Why do you use Ubuntu as base layer:

A simpler approach would be:

Dockerfile:

FROM python:3
  
WORKDIR /usr/src/

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY app.py .


ENTRYPOINT FLASK_APP=/usr/src/app.py flask run --host=0.0.0.0

You put in your requirements.txt the desired packages (eg flask). Build image: docker build -t dejdej/flasky:latest.

Start container: docker run -it -p 5000:5000 dejdej/flasky

If it is mandatory to use virtual environment, you can try it with venv:


FROM python:2.7

RUN virtualenv /YOURENV
RUN /YOURENV/bin/pip install flask

CMD ["/YOURENV/bin/python", "application.py"]

Short answer: Your container is running pipenv, not your application. You need to fix the last line. CMD ["pipenv", "run", "python3", "application.py"] should be only CMD ["python3", "application.py"]

Right answer: I completely agree that there isn´t any reason to use pipenv. Better solution is replace your Dockfile to use a python image and forget pipenv. You already in a container, no reason to use a enviroment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM