I created a package to make this easy: python-on-whales

Install with

pip install python-on-whales

Then you can do

from python_on_whales import docker
docker.compose.up()
docker.compose.stop()
# and all the other commands.

You can find the source for the package in my GitHub repository: https://gabrieldemarmiesse.github.io/python-on-whales/

Answer from Gabriel de Marmiesse on Stack Overflow
🌐
Docker
docker-py.readthedocs.io
Docker SDK for Python — Docker SDK for Python 7.1.0 documentation
It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc.
🌐
PyPI
pypi.org › project › docker-compose
docker-compose · PyPI
Note: Docker Compose requires Python 3.6 or later.
      » pip install docker-compose
    
Published   May 10, 2021
Version   1.29.2
🌐
Docker Docs
docs.docker.com › reference › docker engine api › sdk
SDK | Docker Docs
Docker provides an API for interacting with the Docker daemon (called the Docker Engine API), as well as SDKs for Go and Python. The SDKs allow you to efficiently build and scale Docker apps and solutions.
🌐
GitHub
github.com › docker › compose › issues › 4542
Tutorial/examples for using docker-compose code as a python module in a python script · Issue #4542 · docker/compose
February 28, 2017 - I need to write a good python script as helper to get start in my framework which is heavily based on docker containers. I am looking into docker-py that is quite great since it integrates also with the swarm mode. That said, the problem starts when using docker-compose yaml files inside the framework and allowing the developer to add and customize theirs.
Author   pdonorio
🌐
Medium
aws.plainenglish.io › how-to-use-docker-compose-and-python-to-automate-your-jenkins-environment-a-hands-on-guide-f1b094fb81d4
🐋 How to Use Docker Compose and Python to Automate Your Jenkins Environment 🐍 — A Hands-On Guide 👐 | by Jude Wakim | AWS in Plain English
June 2, 2025 - And this is what it looks like from the terminal after running docker-compose up -d : ... Next, I created a Dockerfile to “customize” the Jenkins image. This way, every build of the image had exactly what I needed baked in (which was literally barebones Jenkins). FROM jenkins/jenkins:lts USER root RUN apt-get update && apt-get install -y \ sudo \ curl \ git \ && rm -rf /var/lib/apt/lists/* USER jenkins · Finally, I wrote a Python script (automate.py) using the Docker SDK to:
Find elsewhere
🌐
CloudBees
cloudbees.com › blog › using-docker-compose-for-python-development
Using Docker Compose for Python Development
At a new command prompt, you can run docker-compose ps to view your running containers. You should see something like the following: Name Command State Ports ------------------------------------------------------------------------------------------------ pythondjangotodoapp_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp pythondjangotodoapp_web_1 gunicorn -b 0.0.0.0:8000 t ...
🌐
Medium
medium.com › @brent.gruber77 › python-with-docker-compose-ca1386924b9e
Python with Docker Compose. Developers hate it, how this one simple… | by Brent Gruber | Medium
August 27, 2021 - That’s it! What we’re going to do is setup a Python API running in docker-compose with hot reload enabled, meaning that any changes you make can be tested instantly once you save the file.
🌐
Gabrieldemarmiesse
gabrieldemarmiesse.github.io › python-on-whales › sub-commands › compose
docker compose - Python on whales
Return Docker Compose events for the specified services. This function streams events related to the specified services in real-time. If no services are specified, it streams events for all services in the current Compose project. ... from python_on_whales import docker # Stream events for a specific service for event in docker.compose.events(["my_service"]): print(event) # This will keep streaming events indefinitely.
🌐
Docker Docs
docs.docker.com › manuals › docker compose › compose sdk
Compose SDK | Docker Docs
To get started, create an SDK instance using the NewComposeService() function, which initializes a service with the necessary configuration to interact with the Docker daemon and manage Compose projects. This service instance provides methods for all core Compose operations including creating, starting, stopping, and removing containers, as well as loading and validating Compose files.
🌐
Medium
medium.com › geekculture › getting-started-with-python-and-docker-compose-9847467d7e10
Python P1: Getting started with Python and Docker-Compose | by Dino Cajic | Geek Culture | Medium
March 24, 2023 - This Dockerfile sets up a Python 3.11 environment, installs the requirements specified in requirements.txt, and runs app.py when the container starts. Create a new file called docker-compose.yml with the following contents:
🌐
Genui
genui.com › resources › python-api-development-in-docker
Python API Development in Docker - GenUI
Using a docker compose file to start and stop the docker container allows much more configurability for my environment and app. ... version: "3.8" services: api: env_file: ${DOT_ENV_FILE:-.env-none} # Ensure specific environment variables are ALWAYS available. environment: - APP_NAME=${APP_NAME:-"Python Flask Ariadne API Starter Test"} - FLASK_APP=${FLASK_APP:-app.py} - FLASK_DEBUG_MODE=${FLASK_DEBUG_MODE:-false} - FLASK_ENV=${FLASK_ENV:-development} - FLASK_RUN_PORT=${FLASK_RUN_PORT:-5000} - LOG_TYPE=${LOG_TYPE:-} - NO_AUTO_START=${NO_AUTO_START:-} - POSTGRES_DB=${POSTGRES_DB:-pfaas_dev} - PO
🌐
GitHub
github.com › apibara › python-sdk › blob › main › docker-compose.dev.yml
python-sdk/docker-compose.dev.yml at main · apibara/python-sdk
Apibara Python SDK. Contribute to apibara/python-sdk development by creating an account on GitHub.
Author   apibara
🌐
MetricFire
metricfire.com › blog › develop-and-deploy-a-python-api-with-kubernetes-and-docker
Comprehensive Guide to Developing and Deploying a Python API with Docker and Kubernetes (Part I) | MetricFire
May 14, 2025 - Setting up a development environment ... API for displaying weather data. Docker Compose, an open-source tool for defining and running multi-container Docker applications, is introduced for use in development environme...
🌐
Docker Docs
docs.docker.com › manuals › docker compose
Docker Compose | Docker Docs
Learn the key concepts of Docker Compose whilst building a simple Python web application.
🌐
Docker Docs
docs.docker.com › reference › samples › python samples
Python samples | Docker Docs
These samples offer a starting point for how to integrate different services using a Compose file.
🌐
Developers Heaven
developers-heaven.net › home › automating docker workflows with the python sdk
Automating Docker Workflows with the Python SDK - Developers Heaven
July 13, 2025 - Docker Compose simplifies the management of multi-container applications. While the Python Docker SDK doesn’t directly replace the docker-compose CLI, you can use it to programmatically interact with Compose configurations and manage your application stacks...
🌐
Docker
docs.docker.com › guides › python › develop your app
Use containers for Python development
USER appuser # Copy the source code into the container. COPY . . # Expose the port that the application listens on. EXPOSE 8001 # Run the application. CMD ["python3", "-m", "uvicorn", "app:app", "--host=0.0.0.0", "--port=8001"] Hide · Create a file named compose.yaml with the following contents.
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › quickstart-python
Python in a container
November 3, 2021 - Build, run, and verify the functionality of a Django, Flask, or General Python app. Debug the app running in a container. Install Docker on your machine and add it to the system path.