docker-pybuild: Embed Dockerfiles directly in your Python scripts

Hey r/Python! I wanted to share a small proof-of-concept I created that lets you build Docker images directly from Python scripts with embedded Dockerfiles.

What My Project Does

docker-pybuild is a Docker CLI plugin inspired by PEP-723 (which allows you to specify Python version and dependencies in script metadata). It extends this concept to include a complete Dockerfile in your Python script’s metadata.

Target Audience

It’s pretty much just a proof-of-concept at this point, but I thought someone might find it handy.

Comparison

I’m not really aware of any similar projects, but I’d be happy to hear if someone knows of any alternatives.

Example

# /// script # requires-python = « >=3.11 » # dependencies = [ # « requests<3 » # ] # [tool.docker] # Dockerfile = «  » » # FROM python:3.11 # RUN pip install pipx # WORKDIR /app # COPY application.py /app # ENTRYPOINT [« pipx », « run », « /app/application.py »] # «  » » # /// import requests # Your code here…

Then simply build and run:

docker pybuild your_script.py –tag your-image-name docker run your-image-name [arguments]

Why I made this

I prefer running Python applications in containers rather than installing tools like uv or pipx on my host system. This plugin lets you build a standalone script into a Docker image without requiring any Python package management tools on your host.

Installation

Make the script executable: chmod +x docker-pybuild.py Place it in your Docker CLI plugins directory: ln -s $(pwd)/docker-pybuild.py ~/.docker/cli-plugins/docker-pybuild

The code is available on GitHub.

submitted by /u/HCF to r/Python
[link] [comments]


Commentaires

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *