docker-dsl

Write Dockerfiles the way you write Python — one recipe, many variants.

PyPI Python Docs License: MIT

AI / Agents

Skills
llms.txt
llms-full.txt

Developers

Yasyf Mohamedali

Maintainer

Community

Full license MIT

Meta

Requires: Python >=3.13
Provides-Extra: dev

An imperative, context-manager-based Python DSL for authoring multi-stage Dockerfiles. Write builds with with blocks, conditionals, comprehensions, and reusable helpers, then render one recipe into many Dockerfile variants.

Install

Run it through uvx, no install needed:

uvx docker-dsl --help

To add it to a project:

uv add docker-dsl

Quickstart

Write a recipe module, minimal.py:

from docker_dsl import Stage
from docker_dsl import context as ctx

ctx.register("tag", str)

with Stage("ubuntu:24.04") as s:
    s.arg("APP_TAG", ctx.tag or "latest", env=True)
    s.workdir("/app")
    with s.run() as r:
        r.echo("hello from docker-dsl") > "/app/greeting.txt"
    s.cmd("cat", "/app/greeting.txt")

Render it:

uvx docker-dsl minimal --tag=v1.0.0 --out Dockerfile

Dockerfile now holds:

## syntax=docker/dockerfile:1
FROM ubuntu:24.04 AS base
ARG APP_TAG=v1.0.0
ENV APP_TAG=${APP_TAG}
WORKDIR /app
RUN echo "hello from docker-dsl" > /app/greeting.txt
CMD ["cat", "/app/greeting.txt"]

Change --tag and render again for a different Dockerfile from the same recipe.

Why

  • RUN chains glued with && are fragile. The run builder accumulates commands in Python and emits one correct RUN, with cd scoping restored for you.
  • Variants drift apart. One recipe takes --gpu=true|false and renders both a GPU and a CPU image from the same code path, instead of two Dockerfiles that diverge over time.
  • BuildKit mounts are easy to get wrong. cache, secret, and bind are scoped context managers, so every RUN inside the block picks them up and nothing leaks out.

Docs

The documentation has a tutorial, task-focused guides (multi-stage builds, mounts, smart apt, the run builder, reusable helpers, and the CLI), and the full API reference.

License

This project is licensed under the MIT License; see LICENSE.