Flask + Docker: quick tips

Gabriel Guerra
Level Up Coding
Published in
3 min readApr 6, 2020

--

Flask is a well known Python Micro Framework. One can say Python is an easy language to learn and I would say maybe. It depends on what you want to do.

Photo by Louis Reed on Unsplash

I wouldn't say Python is easy to debug for a beginner. Neither easy to setup if you are not familiar with some basic concepts, most of all from *nix realm.

I'll bring here quick tips to aid the beginner in the process of Dockerising a Flask Application.

Why Flask?

Flask is a Micro Framework written in Python, extremely flexible, with low footprint and lightweight.

BEFORE WE BEGIN — A BIG disclaimer here:
There's a million ways to bring a Flask App into Docker. This is just ONE of them. Feel free to come over with your solution and to share it with us. That said, let's roll!

1. Choose a base Image

If you're a beginer my advice would be: stick with Ubuntu. It's easier to find resources and how to's. After being successful with Ubuntu you can try other flavors like Alpine, for example.

FROM ubuntu:18.04

2. Update your box

Before you begin, it's a good idea run an apt update && apt install. Pay attention to keep everything in the same RUN statement. It will keep you free from bugs and weird scenarios due to outdated libraries.

RUN apt-get update -y && \
apt-get install -y python3-pip python3-dev python3-venv

3. Install the requirements.txt

Send you requirements.txt to the container and then run PIP

COPY ./requirements.txt /app/requirements.txtRUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

4. Copy the App files

COPY ./app /app/

5. Permission Denied on a .sh bootstrap file

Running “ls -la” in the terminal will tell you if the file has the right permissions. If not, remember to set it when building the image like this:

RUN chmod +x ./boot.sh

I would strongly advise to use correct permissions here instead of running as root user, but it’s completely acceptable in development stage, I guess. Sometimes can happen a conflict of permissions and you'll have to apply the same permissions to the file in your machine.

6. Expose ports

EXPOSE 5000

7. Define the entrypoint

ENTRYPOINT [ "./boot.sh" ]

8. The App is running but browser shows nothing

It happens because you can't see the server yet. Remember to bind the App to 0.0.0.0 address in order to make it accessible from outside the container.

This can be done easily like below, when starting the App:

flask run --host 0.0.0.0

9. Flask Auto Reload

It's annoying to stop the container then start it again each time you change a file. Well, you really don't need to do that!

Before booting the app, export your ENV as development:

export FLASK_ENV=development

10. Issues with characters and encoding

Set your App default encoding as UTF-8:

export LC_ALL=C.UTF-8
export LANG=C.UTF-8

The Entrypoint file

In that case the whole boot.sh will be:

#!/bin/bash
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
export FLASK_APP=app.py
export FLASK_ENV=development
flask run --host 0.0.0.0

Curious about the complete Dockerfile? Here it is:

FROM ubuntu:18.04

RUN apt-get update -y && \
apt-get install -y python3-pip python3-dev python3-venv

COPY ./requirements.txt /app/requirements.txt

RUN pip3 install --upgrade pip && \
pip3 install -r requirements.txt

COPY ./app /app/
RUN chmod +x ./boot.sh
EXPOSE 5000
ENTRYPOINT [ "./boot.sh" ]

That's all folks, I hope it helps you!
Happy coding with Flask.

Appreciate your support!

--

--