Building a Python package, and a container image with poetry

# init the new project in the current directory
% poetry init --python "^3.7" --dependency "Flask:*"
# create the virtual environment (and the poetry.lock file)
% poetry install
# hello_world/__init__.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

if __name__ == "__main__":
app.run()
% export FLASK_APP=hello_world
% export FLASK_ENV=development
% poetry run flask run

Packaging the application

% poetry build --format wheel
% poetry add "Flask:>=1.1,<1.2"

To pin or not to pin

# install versions from poetry.lock
% poetry install
# update versions in the poetry.lock
% poetry update --lock
% poetry export | tee requirements.txt

Building the container image

FROM ubuntu:focal

SHELL ["/bin/bash", "-xe", "-c"]

ARG DEBIAN_FRONTEND=noninteractive

COPY . /app

RUN apt-get update -q \
&& apt-get install -y -q --no-install-recommends \
python3-wheel \
python3-pip \
gunicorn \
&& if [ -e requirements.txt ]; then \
python3 -m pip install --no-cache-dir \
--disable-pip-version-check \
-r requirements.txt; \
fi \
&& python3 -m pip install \
--no-cache-dir --disable-pip-version-check \
/app/ \
&& apt-get remove -y python3-pip python3-wheel \
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/* \
&& useradd _gunicorn --no-create-home --user-group

USER _gunicorn
WORKDIR /app

CMD ["gunicorn", \
"--bind", "0.0.0.0:8000", \
"hello_world:app"]

Going further

--

--

--

Senior developer

Love podcasts or audiobooks? Learn on the go with our new app.

10+ tips on how to effectively describe tasks for your dev team

Estonia, CodeWeek and VIVITA

VSCode Server on Raspberry PI 4

Accuracy of Self-Made R Functions

Look through looking glass on camera mechanics plus R logo

Why Some People Never Become Web Developers? (Even if They Want To)

How a Distributed Data Mesh can be both Data Centric and Event Driven

Projects Update: February 3rd, 2021

How to contact VirtualBox support phone number?

contact virtualbox support phone number

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yoan Blanc

Yoan Blanc

Senior developer

More from Medium

Cleaner Queries in Python with Row Factories

AVL Tree Data structure and it’s implementation in Python.

SQLite in Pythonn!!

Python queries to work with multiple instances of Oracle and SQL Server.