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. Answer from bufandatl on reddit.com
🌐
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 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
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 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
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
🌐
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 ...
🌐
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.
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.
🌐
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
🌐
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.
🌐
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.
🌐
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 - 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.
🌐
Reddit
reddit.com › r/docker › how does one run a python script on docker-compose startup?
r/docker on Reddit: How does one run a python script on docker-compose startup?
October 1, 2021 -

I have a RabbitMQ server and a script for creating my exchanges & queues. Here is what my docker-compose.yml looks like:

version: "3.9"
services:
  rabbitmq:
    image: rabbitmq:3.8-management-alpine
    ports:
        - 5672:5672
        - 15672:15672
    restart: always
    volumes:
        - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
        - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq

Assuming I have a file called migration.py on my project's root folder, how do I run this script after the rabbitmq service finishes starting up?

Top answer
1 of 4
8
I'd recommend building your own Docker container using a Dockerfile: https://docs.docker.com/engine/reference/builder/ You can likely use rabbitmq:3.8-management-alpine as your baseline image, possibly like the following: FROM rabbitmq:3.8-management-alpine COPY migration.py /migration.py ENTRYPOINT ["python", "/migration.py"] This would copy in your custom script from your host to your container, and start it on the container start. Once you get a Dockerfile configured to call this script inside the container, you can plug this Dockerfile into your docker-compose.yml file with something like this: version: "3.9" services: rabbitmq: build: context: path/to/custom/dockerfile dockerfile: name/of/custom/dockerfile ports: - 5672:5672 - 15672:15672 restart: always volumes: - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/ - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
2 of 4
4
Something like this: version: "3.9" services: rabbitmq: image: rabbitmq:3.8-management-alpine ports: - 5672:5672 - 15672:15672 restart: always volumes: - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/ - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq init: image: python:latest volumes: - ./migration.py:migration.py entrypoint: - "python migration.py" depends_on: - rabbitmq The service named init is following the sidekick container pattern - one that is short-lived and helps to initialised a long-running service. The key part of this is the depends_on clause of the init, which means that rabbitmq will start first, followed by init. However: as is outlined in this documentation , docker-compose is quite limited in how start-up order is controlled. It could be that the YAML definition above is insufficient and that the Python script would run before RabbitMQ itself is ready to receive connections, since compose will only wait for the container itself to be started. The suggested solution is to wait for the database (i.e. RabbitMQ in this case) to be ready before attempting to interact with it. In this case, that would mean putting some logic at the start of migration.py to poll RabbitMQ and only begin interacting with it once a connection has been successfully made. Or you could wrap migration.py inside another script which does the waiting if you need to.
🌐
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.
🌐
Better Programming
betterprogramming.pub › using-pyyaml-to-generate-a-docker-compose-file-f9393a231038
Generate a Docker Compose File Using PyYAML | by Marin Aglić | Better Programming
February 8, 2023 - To pass in the argument (the number of workers) to the script, we can simply call it with: python compose_generator.py -w 3 — if we want three workers. In my case, I provide some template YAML files for constructing the final docker-compose file. This makes it simpler to get the output I want, as I don’t need to provide all the values through Python.