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
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.
🌐
Medium
simone-rigoni01.medium.com › lets-use-docker-compose-9072f320ba3f
Let’s use Docker Compose. An easy example using Python | by Simone Rigoni | Medium
May 28, 2025 - FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY client.py . CMD ["python", "client.py"] ... In this way we can run client.py also locally and it will pick up this value SERVER_HOST and when we build with docker-compose we can inject another value.
Discussions

how to run docker compose using docker python sdk - Stack Overflow
I would like to run docker-compose via python docker sdk. However I couldn't find any reference on how to achieve this using these reference Python SDK? I could also use subprocess but I have some... More on stackoverflow.com
🌐 stackoverflow.com
Running docker-compose from python - Stack Overflow
I am looking for a way to run a docker-compose file from python script. I looked in Docker SDK for python, but i didn't found anything about docker-compose. So, is there a way to run a docker-compose More on stackoverflow.com
🌐 stackoverflow.com
How To Run Any Python App in Docker with Docker Compose
Honestly I think compose is the wrong tool for beginners. They should start with bare docker commands so they understand what compose is doing in the background and learn this way also to troubleshoot. With all automated from the start makes it hard sometimes to help someone who doesn’t even know the basics. More on reddit.com
🌐 r/docker
5
8
March 25, 2024
Tutorial/examples for using docker-compose code as a python module in a python script
Hi there, I've been looking quite a lot for this and can't find any answer neither in the documentation nor anywhere else, so I'm opening an issue to see ask for directions. I need to w... More on github.com
🌐 github.com
14
February 28, 2017
🌐
PyPI
pypi.org › project › docker-compose
docker-compose · PyPI
Docker Compose is a tool for running multi-container applications on Docker defined using the Compose file format. A Compose file is used to define how the one or more containers that make up your application are configured.
      » pip install docker-compose
    
Published   May 10, 2021
Version   1.29.2
🌐
Docker Docs
docs.docker.com › manuals › docker compose › quickstart
Docker Compose Quickstart
# Copies `requirements.txt` RUN pip install -r requirements.txt # Installs the Python dependencies COPY . . # Copies the current directory `.` in the project to the workdir `.` in the image EXPOSE 5000 CMD ["flask", "run", "--debug"] # Sets the default command for the container to `flask run --debug` ... Make sure the file is named Dockerfile with no extension. Some editors add .txt automatically, which causes the build to fail. For more information on how to write Dockerfiles, see the Dockerfile reference. ... Compose automatically reads .env and makes these values available for interpolation in your compose.yaml.
🌐
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 ...
🌐
PyPI
pypi.org › project › docker-composer
docker-composer · PyPI
A library to interact with docker-compose V2 from a python Program.
      » pip install docker-composer
    
Published   May 19, 2025
Version   2.79.7
Find elsewhere
🌐
Docker
docs.docker.com › guides › python › containerize your app
Containerize your app | Docker Docs
USER appuser # Copy the source code into the container. COPY . . # Expose the port that the application listens on. EXPOSE 8000 # Run the application. CMD ["python3", "-m", "uvicorn", "app:app", "--host=0.0.0.0", "--port=8000"] Hide · Create a file named compose.yaml with the following contents.
🌐
GeeksforGeeks
geeksforgeeks.org › devops › docker-compose-for-python-applications
Docker Compose for Python Applications: A Comprehensive Guide - GeeksforGeeks
July 23, 2025 - Docker Compose improves on the orchestration of interconnected services, giving designers a clear method for defining, running, and managing complex application models, for Python developers, Docker Compose offers a strong tool compartment for containerizing their applications, ensuring portability, versatility, and simplicity of deployment.
🌐
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. Docker Samples: A collection of over 30 repositories that offer sample containerized demo applications, tutorials, and labs.
🌐
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:
🌐
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
🌐
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.
Top answer
1 of 4
2

In the Bourne shell, in general, you can run two commands in sequence by putting && between them. It sounds like you're already aware of this.

# without Docker, at a normal shell prompt
python test.py && python main.py

The Dockerfile CMD has two syntactic forms. The JSON-array form does not run a shell, and so it is slightly more efficient and has slightly more consistent escaping rules. If it's not a JSON array then Docker automatically runs it via a shell. So for your use you can use the shell form:

CMD python test.py && python main.py

In comments to other answers you ask about providing this as an override in the docker-compose.yml file. Compose will not normally run a shell for you, so you need to explicitly specify it as part of the command: override.

command: /bin/sh -c 'python test.py && python main.py'

Your Dockerfile should generally specify a CMD and the docker-compose.yml often will not include a command:. This makes it easier to run the image in other contexts (via docker run without Compose; in Kubernetes) since you won't have to retype the command every different way you want to run the container. The entrypoint wrapper pattern highlighted in @sytech's answer is very useful in general and it's easy to add to a container that uses a CMD without an ENTRYPOINT; but it requires the Dockerfile to use CMD as a normal well-formed shell command.

2 of 4
0

In general, it is a good rule of thumb that a container should only a single process and that essential process should be pid 1

Using an entrypoint can help you do multiple things at runtime and optionally run user-defined commands using exec, as according to the best practices guide.

For example, if you always want the tests to run whenever the container starts, then execute the defined command in CMD.

First, create an entrypoint script (be sure to make it executable with chmod +x):

#!/usr/bin/env bash

# always run tests first
python /src/tests.py

# then run user-defined command
exec "$@"

Then configure the dockerfile to copy the script and set it as the entrypoint:

#...
COPY entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["python", "main.py"]

Then when you build an image from this dockerfile and run it, the entrypoint will first execute the tests then run the command to run main.py

The command can also still be overridden by the user when running the image like docker run ... myimage <new command> which will still result in the entrypoint tests being executed, but the user can change the command being run.

🌐
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.
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › quickstart-python
Python in a container
November 3, 2021 - Compose is typically used when running multiple containers at once. With all this information, the Container Tools extension creates the following files: A Dockerfile.
🌐
GitHub
github.com › docker › compose
GitHub - docker/compose: Define and run multi-container applications with Docker · GitHub
Want to help develop Docker Compose? Check out our contributing documentation. If you find an issue, please report it on the issue tracker. The Python version of Compose is available under the v1 branch.
Starred by 37.2K users
Forked by 5.8K users
Languages   Go 97.2% | Dockerfile 2.2%
🌐
GitHub
github.com › okteto › python-docker-compose
GitHub - okteto/python-docker-compose
This is the source code of the application built during the first community call to demonstrate how to deploy docker-compose applications on Okteto.
Starred by 3 users
Forked by 6 users
Languages   Python 92.4% | Dockerfile 7.6% | Python 92.4% | Dockerfile 7.6%