Funlamb
Funlamb

Reputation: 625

Problem with my Docker image not running Flask

I am trying to build a Flask docker image. I get the error:

zsh: command not found: flask

I followed this old tutorial to get things working. https://medium.com/@rokinmaharjan/running-a-flask-application-in-docker-80191791e143

In order to just learn how to start flask website with Docker I have made everything simple. My Docker image should just open a Hello world front page.

My example.py:

from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
   app.run()

My Dockerfile:

FROM ubuntu:16.04
RUN apt-get update -y
RUN apt-get install python -y
RUN apt-get install python-pip -y
RUN pip install flask
COPY example.py /home/example.py
ENTRYPOINT FLASK_APP=/home/example.py flask run --host=0.0.0.0

I run

sudo docker build . -t flask-app

to build the image.

When I run

docker run -p 8080:5000 flask-app

I get the error:

zsh: command not found: flask

What am I missing here?

Upvotes: 0

Views: 1831

Answers (1)

Daniel Campos Olivares
Daniel Campos Olivares

Reputation: 2594

Well, indeed you're following a really old tutorial.

I'm not going to enter into detail whether using Flask directly without a WSGI server is something you should do, so I'm just going to focus on your question.

Concise answer: you don't have the installed modules by pip in your PATH, so of course you cannot invoke them. Flask is one of this modules.

Extended answer: keep reading.


First of all, using that base image you're downloading an old version of both Python and pip, secondary: you don't need a fully fledged operating system(i.e. ubuntu) in order to run a Flask application.

There are already base images with Python like python:3.9.10-slim-buster with way less dependencies and possible vulnerabilities than an old image from Ubuntu 16.

FROM python:3.9.10-slim-buster

Second, you shouldn't rely on what do you have on the base image and you should use an environment (venv) for your application, where you can install Flask and any other dependency of the application which should be listed on the requirements.txt. Also you should choose in which working directory you would like to place your code (/usr/src/app is a common place normally).

Indicating which port are you exposing by default is also a good thing to do (even though everyone knows that Flask exposes port 5000).

FROM python:3.9.10-slim-buster

WORKDIR /usr/src/app

ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN python3 -m pip install flask
COPY example.py .
ENTRYPOINT FLASK_APP=example flask run --host=0.0.0.0

EXPOSE 5000

and as a result:

❯ docker run -p 8080:5000 flask-app
 * Serving Flask app 'example' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)

Upvotes: 1

Related Questions