Package your python app with poetry & docker
The combination of "package", "python", "poetry" and "docker" are words you may not hear very often these days as there is more than one way to skin a cat. Today, I choose to go off the beaten track of pip et al just for the fun of it all and if that gives you the heebie-jeebies you may try a chill-pill or please leave now.
三 THREE
二 TWO
一 ONE
〇 YES!
You are still here! Firstly congratulations and secondly, here is your TLDR copy and paste example:
As per usual, should you require some unpacking, carry on reading.
This is called a multi-stage build and it offers a "significant reduction in complexity" by saving named interim stages that are used as a build cache and can be referenced internally. Let's check it out stage by stage:
Build Stage 1: os-base
- Named stage.
- PYTHONUNBUFFERED forces the stdout and stderr streams to go straight to terminal.
- PYTHONDONTWRITEBYTECODE prevents Python from writing .pyc files to disk. This is useful inside a docker container as a marginal performance gain if your process runs only once and needs not be saved to byte code for subsequent invocations.
- "... used to resynchronise the package index files from their sources", therefore does not hurt to run.
- We need curl for installing poetry in the next step.
Build Stage 2: poetry-base
- Named stage.
- Installing poetry as per the install instructions.
- Docker variables declared with ENV can be used across all docker stages. Here we're modifying our PATH environment variable so that we can execute poetry here and in subsequent stages.
- Since we are inside a docker container there is no immediate reason to use a virtual environment. As with most things, if you put a lot of software engineers in a room there will potentially be a few opinions on that. However, since I have come across scenarios where a virtual environment is required to prevent conflicts with the OS, I appreciate all experiences and leave it to you. If you choose to use a virtual environment, make use of poetry's fabulous ability to switch between versions/environments.
- Since we have no use for curl anymore, it doesn't hurt to remove it. In case you're wondering what the "-y" flag does, it's answering "yes" if the uninstaller asks you a question such as "are you a happy developer?". Not that it will but in a parallel universe it could. If you want to have some fun ask yourself "am I a happy developer?" and type "yes" in your linux terminal.
Build Stage 3: app-base
Here's where you'll copy your app specific files and directories. I left it quite open so you can replace it with your own.
By now you should be able to decipher what the steps do. I will slightly freestyle without numbering then, shall I?
WORKDIR does what it says on the tin. It's also magically created if it doesn't exist.
Notice that we are only copying pyproject.toml and NOT the poetry.lock. Why don't you find out why that might or might not be a good idea?
And next comes, the mighty dependency installation minus the development dependencies. You have hopefully run your pytest tests during your build pipeline if you haven't and you are interested in the Google Cloud Platform, join me next time to see how to automate unit and integration testing for your python project with Cloud Build.
Epilogue - Build Stage 4: main
This is where you launch your app. We're not launching an app in this tutorial but using the space to show you a little trick instead.
Your container will now remain running. You can either ssh to it to e.g check manually the dependencies have been installed, or what the world might look like from inside a container.
じゃまたね or じゃまた or またね