Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoloboschi committed Jan 18, 2024
1 parent 0aa833e commit 9f0a542
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 19 deletions.
77 changes: 68 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
# poetry-dockerize-plugin
Package your Poetry application to a Docker image without configuration. It just works.
# Poetry Dockerize Plugin

```
![PyPI](https://img.shields.io/pypi/v/poetry-dockerize-plugin?color=green&label=pypi%20package)
![PyPI](https://img.shields.io/pypi/pyversions/poetry-dockerize-plugin?color=gree)

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:

```bash
poetry self add poetry-dockerize-plugin
```

## Quickstart

No configuration needed! Just type:
```bash
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!
```

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

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

## Configuration API Reference

This examples shows a complete configuration of the docker image:

```toml
[tool.docker]
name = "alternative-image-name"
tags = ["latest-dev"]
entrypoint = ["python", "-m", "whatever"]
python = "3.8"
ports = [5000]
env = {"MY_APP_ENV" = "dev"}
labels = {"MY_APP_LABEL" = "dev"}
```

* `name` customizes the docker image name.
* `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.
* `python` python version to use. Default is `3.11`
* `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.


## Command-Line options

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

```bash
poetry dockerize --help
```

cd your-application
poetry dockerize
$ ...
$ Image built!
## License

```
This project is licensed under the terms of the MIT license.
15 changes: 9 additions & 6 deletions poetry_dockerize_plugin/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from poetry.toml import TOMLFile


class AutoDockerConfiguration:
class DockerizeConfiguration:
name: str = ""
tags: List[str] = []
entrypoint_cmd: List[str] = []
Expand All @@ -27,8 +27,8 @@ class ProjectConfiguration:
labels: dict[str, str]


def parse_auto_docker_toml(dict: dict) -> AutoDockerConfiguration:
config = AutoDockerConfiguration()
def parse_auto_docker_toml(dict: dict) -> DockerizeConfiguration:
config = DockerizeConfiguration()
config.name = dict.get("name")
tags = dict.get("tags")
if tags:
Expand All @@ -41,6 +41,7 @@ def parse_auto_docker_toml(dict: dict) -> AutoDockerConfiguration:
config.python = dict.get("python")
config.ports = dict.get("ports")
config.envs = dict.get("env")
config.labels = dict.get("labels")
return config


Expand All @@ -50,9 +51,10 @@ def parse_pyproject_toml(pyproject_path) -> ProjectConfiguration:
doc = file.read()

config = ProjectConfiguration()
tool_poetry = doc.get('tool', dict()).get('poetry', dict())
tool = doc.get('tool', dict())
tool_poetry = tool.get('poetry', dict())

auto_docker = parse_auto_docker_toml(doc.get('dockerize', dict()))
auto_docker = parse_auto_docker_toml(tool.get('dockerize', dict()))

config.image_name = auto_docker.name or tool_poetry['name']
config.image_tags = auto_docker.tags or [tool_poetry["version"], "latest"]
Expand Down Expand Up @@ -149,10 +151,11 @@ def build(
real_context_path = os.path.realpath(root_path)
for tag in config.image_tags:
full_image_name = f"{config.image_name}:{tag}"
print("Building image: " + full_image_name)
print(f"Building image: {full_image_name}")
docker_client = docker.from_env()
docker_client.images.build(
path=real_context_path,
dockerfile=dockerfile,
tag=full_image_name
)
print(f"Successfully built image: {full_image_name}")
29 changes: 26 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
[tool.poetry]
name = "poetry-dockerize-plugin"
version = "0.1.0"
description = "Package your Poetry application to a Docker image automatically."
description = "Poetry application to Docker, automatically."
authors = ["Nicolò Boschi <[email protected]>"]
license = "MIT"
readme = "README.md"
keywords = ["poetry", "packaging", "docker"]
repository = "https://github.com/nicoloboschi/poetry-dockerize-plugin"
documentation = "https://github.com/nicoloboschi/poetry-dockerize-plugin"
classifiers = [
"Intended Audience :: Developers",
"Intended Audience :: Information Technology",
"Intended Audience :: System Administrators",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Software Development :: Libraries :: Application Frameworks",
"Topic :: Software Development :: Version Control :: Git",
"Topic :: System :: Archiving :: Packaging",
"Topic :: System :: Installation/Setup",
"Topic :: System :: Software Distribution",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"License :: OSI Approved :: MIT License"
]
packages = [
{include = "poetry_dockerize_plugin", from = "."}
]


[tool.poetry.plugins."poetry.application.plugin"]
dockerize = "poetry_dockerize_plugin.plugin:DockerApplicationPlugin"

Expand All @@ -18,7 +42,6 @@ python = "^3.9"
poetry = "^1.7.1"
poetry-core = "^1.8.1"
docker = "^6.1.3"
pytest = "^7.4.4"


[tool.poetry.group.test.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ flask = "*"
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[dockerize]
[tool.dockerize]
tags = ["latest", "latest-dev", "0.1.0"]
env = {PORT = "5001"}
ports = ["5001"]

0 comments on commit 9f0a542

Please sign in to comment.