Even if the documentation refers to the "path within the build context to the Dockerfile", it works for a Dockerfile outside the build context if an absolute path is specified.

Using my project tree:

client.images.build(
    path = '/opt/project/src/',
    dockerfile = '/opt/project/dockerfile/Dockerfile.name',
    tag = 'image:version'
)
Answer from Marco Miduri on Stack Overflow
🌐
Docker
docker-py.readthedocs.io › en › stable › images.html
Images — Docker SDK for Python 7.1.0 documentation
cache_from (list) – A list of images used for build cache resolution · target (str) – Name of the build-stage to build in a multi-stage Dockerfile
🌐
Videosdk
docs.videosdk.live › build docker image
Build Docker Image - Python Worker
Here’s an example Dockerfile for your Python worker: FROM python:3.11-slim RUN apt-get update && apt-get install -y python3-dev WORKDIR /app COPY requirements.txt . RUN python -m pip install -r requirements.txt COPY .
🌐
Europython
ep2019.europython.eu › media › conference › slides › HVM9f5G-docker-meets-python-a-look-on-the-docker-sdk-for-python.pdf pdf
A look on the Docker SDK for Python
Two days of trainings. Multiple 3 hour long sessions with experts. Training tickets are sold separately · Over 120 sessions in 6 parallel tracks. Talks, poster sessions, open spaces, helpdesks, recruitment sessions, sponsor exhibits and more. All included in the main conference ticket
🌐
Reddit
reddit.com › r/docker › is it possible to build a docker image from inside a python script
r/docker on Reddit: Is it possible to build a docker image from inside a python script
September 25, 2020 -

Currently I'm using Dockerfiles to build docker images, but I want to parameterize the building process so I'm trying to write a python script that takes as input a folder and dockerizes. I checked the docker-py library but it only supports managing docker images and containers, not building new ones. Is there another library or another way to dockerize directories by automatically creating the required Dockerfile?

🌐
Fedorkobak
fedorkobak.github.io › python › packages › docker_sdk.html
Docker SDK — Python
As an example, we will use an image based on the dockerfile defined below: ... It’s an image that will print hello_new_image when it starts. ... Using docker_client.images.build we can build the image. client.images.build(path="/tmp/docker_sdk_files", tag="modified_hello_world")
Find elsewhere
🌐
Docker
docker-py.readthedocs.io
Docker SDK for Python — Docker SDK for Python 7.1.0 documentation
>>> client.images.pull('nginx') <Image 'nginx'> >>> client.images.list() [<Image 'ubuntu'>, <Image 'nginx'>, ...] That’s just a taste of what you can do with the Docker SDK for Python. For more, take a look at the reference. ©2024 Docker Inc.
🌐
GitHub
github.com › docker › docker-py
GitHub - docker/docker-py: A Python library for the Docker Engine API · GitHub
>>> client.images.pull('nginx') <Image 'nginx'> >>> client.images.list() [<Image 'ubuntu'>, <Image 'nginx'>, ...]
Starred by 7.2K users
Forked by 1.7K users
Languages   Python
🌐
Apache
beam.apache.org › documentation › sdks › python-sdk-image-build
Building Beam Python SDK Image Guide - Apache Beam®
You can build a docker image if your local environment has Java, Python, Golang and Docker installation. Try ./gradlew :sdks:python:container:py<PYTHON_VERSION>:docker. For example, :sdks:python:container:py310:docker builds apache/beam_python3.10_sdk locally if successful.
🌐
YouTube
youtube.com › codetube
python docker sdk build image example - YouTube
Download this code from https://codegive.com Title: Building Docker Images with Python Docker SDK: A Step-by-Step TutorialIntroduction:Docker has become a st...
Published   January 21, 2024
Views   22
🌐
YouTube
youtube.com › watch
BUILD YOUR OWN DOCKER USING DOCKER SDK AND PYTHON PART 2 - YouTube
Welcome to my YouTube channel! In this video, we explore the powerful combination of Docker and Python by diving into the Docker SDK for Python. Join me as I...
Published   May 24, 2023
🌐
OneUptime
oneuptime.com › home › blog › how to use python docker sdk (docker-py) for automation
How to Use Python Docker SDK (docker-py) for Automation
February 8, 2026 - # build_image.py # Builds a Docker image and streams build output import docker import json client = docker.from_env() # Build an image from a Dockerfile in the current directory print("Building image...") image, build_logs = client.images.build( path=".", tag="myapp:latest", dockerfile="Dockerfile", buildargs={"APP_VERSION": "1.2.3"}, labels={"build-date": "2026-02-08"}, rm=True, # Remove intermediate containers nocache=False, ) # Stream and display build logs for log_entry in build_logs: if "stream" in log_entry: print(log_entry["stream"].strip()) elif "error" in log_entry: print(f"ERROR: {log_entry['error']}") print(f"\nBuilt image: {image.tags}") print(f"Image ID: {image.short_id}") print(f"Size: {image.attrs['Size'] / 1024 / 1024:.1f} MB")
🌐
GitHub
github.com › docker › docker-py › blob › main › docker › models › images.py
docker-py/docker/models/images.py at main · docker/docker-py
Build an image and return it. Similar to the ``docker build`` command. Either ``path`` or ``fileobj`` must be set. · If you already have a tar file for the Docker build context (including · a Dockerfile), pass a readable file-like object to ``fileobj`` and also pass ``custom_context=True``. If the stream is also ·
Author   docker
🌐
Docker Docs
docs.docker.com › reference › docker engine api › sdk › examples
Examples using the Docker Engine SDKs and Docker API
It's possible to override these credentials, but that's out of scope for this example guide. After using docker login, the Python SDK uses these credentials automatically. import docker client = docker.from_env() image = client.images.pull("alpine") ...
🌐
Docker
docker-py.readthedocs.io › en › stable › api.html
Low-level API — Docker SDK for Python 7.1.0 documentation
... chunk_size (int) – The number of bytes returned by each iteration of the generator. If None, data will be streamed as it is received. Default: 2 MB ... A stream of raw archive data. ... >>> image = client.api.get_image("busybox:latest") >>> f = open('/tmp/busybox-latest.tar', 'wb') >>> ...
🌐
Docker
docker.com › blog › how-to-dockerize-your-python-applications
How to “Dockerize” Your Python Applications | Docker
Choosing a slim image build is smart, since they’re miniscule. Similarly, python:<version>-alpine achieves similar results. This image may cater to sensitive workloads due to its minimal attack surface. You’ll need to add your source file into your container’s base folder, for any Python project. That respective command below draws upon our earlier example:
Published   November 6, 2024
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › quickstart-python
Python in a container
November 3, 2021 - The Containers: Add Docker Files to Workspace... command automatically creates a Docker launch configuration to build and run your container in debug mode. To debug your Python app container: Navigate to the file that contains your app's startup code, and set a breakpoint. Navigate to Run and Debug and select Containers: Python - General, Containers: Python - Django, or Containers: Python - Flask, as appropriate. Start debugging using the F5 key. The container image builds.