Skip to main content

Poetry application to Docker, automatically.

Project description

Poetry Dockerize Plugin

PyPI Downloads Py versions

Key features:

  • Automatically generate a docker image from your Poetry application.
  • Highly configurable. You can configure the image by adding a section in the pyproject.toml configuration file.

Installation

In order to install the plugin you need to have installed a poetry version >=1.2.0 and type:

poetry self add poetry-dockerize-plugin@latest

Quickstart

No configuration needed! Just type:

poetry dockerize
>Building image: poetry-sample-app:latest
>Successfully built image: poetry-sample-app:latest
docker run --rm -it poetry-sample-app:latest
>hello world!

Usage in GitHub Actions

You just need to run the quickstart command in your GitHub Actions workflow:

name: Build and publish latest

on:
  push:
    branches: main

jobs:
  login:
    runs-on: ubuntu-latest
    steps:
        - name: Install Poetry
          uses: snok/install-poetry@v1

        - name: Install poetry-dockerize-plugin
          run: poetry self add poetry-dockerize-plugin@latest

        - name: Build and package
          run: |
            poetry install
            poetry run pytest
            poetry dockerize

        - name: Login to Docker Hub
          uses: docker/login-action@v3
          with:
            username: ${{ secrets.DOCKERHUB_USERNAME }}
            password: ${{ secrets.DOCKERHUB_TOKEN }}

        - name: Push to Docker Hub
          run: docker push my-app:latest

Configuration

To customize some options, you can add a [tool.dockerize] section in your pyproject.toml file. For example to change the image name:

[tool.dockerize]
name = "myself/myproject-app"

Configuration API Reference

This examples shows a complete configuration of the docker image:

[tool.dockerize]
name = "alternative-image-name"
python = "3.12"
base-image = "python:3.12-slim"
tags = ["latest-dev"]
entrypoint = ["python", "-m", "whatever"]
ports = [5000]
env = {"MY_APP_ENV" = "dev"}
labels = {"MY_APP_LABEL" = "dev"}
apt-packages = ["curl"]
extra-run-instructions = ["RUN curl https://huggingface.co/transformers/"]

# Only for build docker layer
build-apt-packages = ["gcc"]
extra-build-instructions = ["RUN poetry config http-basic.foo <username> <password>"]
build-poetry-install-args = ["-E", "all", "--no-root"]
  • name customizes the docker image name.
  • python python version to use. If not specified, will try to be extracted from tool.poetry.dependencies.python. Default is 3.11
  • base-image customizes the base image. If not defined, the default base image is python:<python-version>-slim-bookworm.
  • tags declares a list of tags for the image.
  • entrypoint customizes the entrypoint of the image. If not provided, the default entrypoint is retrieved from the packages configuration.
  • ports exposes ports
  • env declares environment variables inside the docker image.
  • labels append labels to the docker image. Default labels are added following the opencontainers specification.
  • apt-packages installs apt packages inside the docker image.
  • extra-run-instructions adds extra instructions to the docker run (after poetry install). Any modification to the filesystem will be kept after the poetry install.

For the build step:

  • build-apt-packages installs apt packages inside the build docker container.
  • extra-build-instructions adds extra instructions to the docker build (before poetry install). Any modification to the filesystem will be lost after the poetry install. If you need to add files to the image, use the extra-run-instructions.
  • build-poetry-install-args adds additional arguments to the poetry install command in the build step.

You can also pass any option via environment variable by prefixing the key with DOCKERIZE_. For example, to set the entrypoint you can use the DOCKERIZE_ENTRYPOINT environment variable:

export DOCKERIZE_ENTRYPOINT="python -m myapp"
poetry dockerize

or use a .env file which will be loaded by the plugin:

echo "DOCKERIZE_ENTRYPOINT=python -m myapp" > .env
poetry dockerize

For dicts such as env and labels, you can set multiple values by adding multiple variables:

export DOCKERIZE_ENV_MY_VAR="my_value"
export DOCKERIZE_ENV_MY_OTHER_VAR="my_other_value"
export DOCKERIZE_LABELS_MY_LABEL="label1"
poetry dockerize

Command-Line options

All command line options provided by the poetry-dockerize-plugin may be accessed by typing:

poetry dockerize --help

Troubleshooting

To troubleshoot the plugin, you can use the --debug flag to get more information about the execution.

poetry dockerize --debug

Generate Dockerfile

To only generate the Dockerfile, you can use the --generate flag.

poetry dockerize --generate

Then you can store the Dockerfile on the repository and use it as a template and customize it as you need.

License

This project is licensed under the terms of the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

poetry_dockerize_plugin-1.2.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

poetry_dockerize_plugin-1.2.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file poetry_dockerize_plugin-1.2.0.tar.gz.

File metadata

File hashes

Hashes for poetry_dockerize_plugin-1.2.0.tar.gz
Algorithm Hash digest
SHA256 3843e227fbe4317e976010b415694fdc338ebc7c79520946e9fae072ff337823
MD5 8d42ee605f50d63d45e324c825dd635b
BLAKE2b-256 2da9c1073ad74fd4f31a292aabb6cc38eaab48b23d55a657b1471129fbeb01d2

See more details on using hashes here.

File details

Details for the file poetry_dockerize_plugin-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for poetry_dockerize_plugin-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5dbe5529c2a2fd37c91651b0f2378c0569bbc62fa00a02c164ce28d60c1f91b9
MD5 a269504f46e41eb6d50132fa19de13d7
BLAKE2b-256 87a83b077b938a7d9bf2354cd300dc5253b7227acfbc7bb9b1834b08c760128f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page