Poetry¶
Poetry is a virtual environment and dependency manager, builder and publisher for python. We are using it as a tool to update our python dependencies.
The configuration file, pyproject.toml
in the project root, contains the list of dependencies. From there, we can export a requirements.txt
file for use with pip, which we use in our Dockerfile
.
How to add poetry to the running container¶
Adding poetry to the running container lets us manage project dependencies without installing it on the host machine. Not adding it to the docker image keeps the image smaller.
-
Make sure the container is running
docker-compose up -d # (1)!
- This runs the container in background (daemon) mode
-
Install poetry inside the running container
docker-compose exec mkdocs pip install poetry # (1)!
- Runs
pip install poetry
in the mkdocs service container
Now we can call poetry that's in the image
- Runs
-
Use poetry for dependency management or other purposes.
-
Take down the container when done
docker-compose down # (1)!
- This stops and deletes the container. The poetry install is gone as well. If we don't want to delete the container, use
docker-compose stop
.
- This stops and deletes the container. The poetry install is gone as well. If we don't want to delete the container, use
How to add poetry to the image¶
Adding poetry to the image lets us manage the project dependencies without installing it on the host machine.
-
Add poetry to Dockerfile
Dockerfile... # install dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir poetry==1.5.1 ...
-
Build the image
docker-compose build
Now we can call poetry that's in the image
How to add a package¶
We use pyproject-fmt
as an example. pyproject-fmt
is an auto-formatter for the pyproject.toml configuration file.
- Get a shell inside the container
docker-compose run mkdocs sh
- Install and run the package
pip install pyproject-fmt
pyproject-fmt pyproject.toml
How to add package to a group¶
Organizing packages into groups allows better organization of dependencies. For example, dev dependencies and docs dependencies as opposed to the ones for the main application.
poetry add pytest --group dev