This question probably needs some work to clarify your use case, but I'll answer as best as I can.

Generally, containers orchestrated in compose are all hosted on the same network, by default host networking. So, all should need to do is have a Dockerfile for each of your python projects, expose a port, connect them with docker compose, and perform your communication over these ports. These ports should map to ports on your host OS. e.g. a web app running on port 3000 in a docker-compose container will be available on your localhost at 3000.

Say you have a project structure like this:

.
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ project1
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ””โ”€โ”€ main.py
โ””โ”€โ”€ project2
    โ”œโ”€โ”€ Dockerfile
    โ””โ”€โ”€ main.py

Then you could have a project1/Dockerfile like this:

#whatever python you need
FROM godatadriven/python-onbuild 

# Coping source in current directory into the image
COPY . /app

#whatever your project needs to work 
RUN pip install --upgrade pip 
RUN pip install -r requirements.txt 

# Commands in a list
CMD ["python", "main.py"]

# Expose web port
EXPOSE 8002/tcp 

And something similar for project2/Dockerfile. Then, your docker-compose.yml would contain something like:

version: '3.9'  # version of compose format 

services:
  project1:
    build: ./project1  # path is relative to docker-compose.yml location
    volumes:
      - ./project1:/app # mount point
    #specify resources for container - you may not need to
    deploy:
      resources:
        limits:
          memory: 500M
        reservations:
          memory: 100M
    ports:
      - 8002:8002  # host:container
  project2:
    build: ./project2
    volumes:
      - ./project2:/app # mount point
    deploy:
      resources:
        limits:
          memory: 500M
        reservations:
          memory: 100M
    ports:
      - 8001:8001  # host:container

Then, you need to send all communication that these python projects are doing over these ports.

You would probably benefit from reading through Dockerfile best practices and Docker Compose docs. This will be an easy base image to start with, but you'll need to explore this and find out exactly what you need. In general, stay away from Alpine linux as you'll get crappy build times.

Answer from e god on Stack Overflow
๐ŸŒ
GitHub
github.com โ€บ bennzhang โ€บ docker-demo-with-simple-python-app
GitHub - bennzhang/docker-demo-with-simple-python-app ยท GitHub
Build and run a simple docker image with a python+flask+gunicorn web application.
Starred by 18 users
Forked by 199 users
Languages ย  Python
Top answer
1 of 1
1

This question probably needs some work to clarify your use case, but I'll answer as best as I can.

Generally, containers orchestrated in compose are all hosted on the same network, by default host networking. So, all should need to do is have a Dockerfile for each of your python projects, expose a port, connect them with docker compose, and perform your communication over these ports. These ports should map to ports on your host OS. e.g. a web app running on port 3000 in a docker-compose container will be available on your localhost at 3000.

Say you have a project structure like this:

.
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ project1
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ””โ”€โ”€ main.py
โ””โ”€โ”€ project2
    โ”œโ”€โ”€ Dockerfile
    โ””โ”€โ”€ main.py

Then you could have a project1/Dockerfile like this:

#whatever python you need
FROM godatadriven/python-onbuild 

# Coping source in current directory into the image
COPY . /app

#whatever your project needs to work 
RUN pip install --upgrade pip 
RUN pip install -r requirements.txt 

# Commands in a list
CMD ["python", "main.py"]

# Expose web port
EXPOSE 8002/tcp 

And something similar for project2/Dockerfile. Then, your docker-compose.yml would contain something like:

version: '3.9'  # version of compose format 

services:
  project1:
    build: ./project1  # path is relative to docker-compose.yml location
    volumes:
      - ./project1:/app # mount point
    #specify resources for container - you may not need to
    deploy:
      resources:
        limits:
          memory: 500M
        reservations:
          memory: 100M
    ports:
      - 8002:8002  # host:container
  project2:
    build: ./project2
    volumes:
      - ./project2:/app # mount point
    deploy:
      resources:
        limits:
          memory: 500M
        reservations:
          memory: 100M
    ports:
      - 8001:8001  # host:container

Then, you need to send all communication that these python projects are doing over these ports.

You would probably benefit from reading through Dockerfile best practices and Docker Compose docs. This will be an easy base image to start with, but you'll need to explore this and find out exactly what you need. In general, stay away from Alpine linux as you'll get crappy build times.

๐ŸŒ
Docker
docker.com โ€บ blog โ€บ how-to-dockerize-your-python-applications
How to โ€œDockerizeโ€ Your Python Applications | Docker
Form your new directory by creating a new root project folder in the sidebar, and naming it. Open a new workspace named main.py. Enter the cd [root folder name]command in the Terminal to tap into your new directory.
Published ย  November 6, 2024
๐ŸŒ
Real Python
realpython.com โ€บ tutorials โ€บ docker
Python Docker Tutorials โ€“ Real Python
Browse all resources below, or ... that runs the same everywhere. ... Create a Dockerfile that installs Python, copies your code, installs dependencies, and sets an entrypoint....
๐ŸŒ
Earthly
earthly.dev โ€บ blog โ€บ python-docker
Running Python on Docker - Earthly Blog
July 24, 2023 - Any dependencies installed wonโ€™t interfere with other Python projects. python3 -m venv <directory_name> source <directory_name>/bin/activate ยท Using the following code, create a new file called Dockerfile in the empty project directory:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ setting-up-docker-for-python-projects-a-step-by-step-guide
Setting Up Docker for Python Projects: A Step-by-Step Guide - GeeksforGeeks
July 23, 2025 - sudo apt update sudo apt install docker.io sudo systemctl start docker sudo systemctl enable docker ... First, create a simple Python project locally and add Python Files: Create a simple Python script inside your project directory.
๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ docker
Python Docker - using Docker for Python
July 8, 2023 - $ sudo docker build -t pymaria-simple . Sending build context to Docker daemon 4.608kB Step 1/9 : FROM mariadb ---> b95867b52886 Step 2/9 : RUN apt-get update && apt-get install -y python3.8 python3-pip ---&gt; Using cache ---> 9370769248ed Step 3/9 : RUN pip3 install pymysql ---> Using cache ...
Find elsewhere
๐ŸŒ
GitHub
github.com โ€บ deis โ€บ example-dockerfile-python
GitHub - deis/example-dockerfile-python: A simple Dockerfile / Python app for Deis, the open source PaaS ยท GitHub
A simple Dockerfile / Python app for Deis, the open source PaaS - deis/example-dockerfile-python
Starred by 4 users
Forked by 23 users
Languages ย  HTML
๐ŸŒ
GitHub
github.com โ€บ topics โ€บ python-docker
python-docker ยท GitHub Topics ยท GitHub
February 4, 2021 - This is a simple flask application designed to run in multiple ways: locally docker container from docker hub hosted by IBM cloud. python docker flask cloud docker-container standalone cloud-foundry hello-world flask-application bluemix helloworld ...
๐ŸŒ
Cisco DevNet
developer.cisco.com โ€บ docs โ€บ iox โ€บ tutorial-build-sample-docker-type-python-simple-app
Tutorial: Build Sample Docker Type Python Simple App - IOx - Cisco DevNet
Clone this repo and use branch master. Find the application under the following directory: 'simple-python-app' $ git clone https://github.com/CiscoIOx/docker-demo-apps.git -b master $ cd docker-demo-apps/simple-python-app
๐ŸŒ
Python Land
python.land โ€บ home โ€บ deployment โ€บ how to use docker to containerize your python project
How To Use Docker To Containerize Your Python Project โ€ข Python Land Tutorial
February 14, 2023 - Iโ€™ll start very simply, with the following Pipfile: ... For now, weโ€™ll create a Python file just to prove that things are working as expected. I called it app.py: ... Next, we need to create a Dockerfile that defines our container. Most projects these days offer official versions of their software as a Docker container.
๐ŸŒ
Medium
medium.com โ€บ @harichselvamc โ€บ getting-started-with-docker-building-and-running-your-first-python-application-2304deded192
Getting Started with Docker: Building and Running Your First Python Application | by harichselvamc | Medium
August 29, 2023 - Step 1: Install Docker Desktop and Set Up Docker Hub Account ยท 1.Download and install Docker Desktop from the official Docker website. 2.Once installed, create or log in to your Docker Hub account.
๐ŸŒ
DEV Community
dev.to โ€บ francescoxx โ€บ dockerize-a-python-application-1olg
Dockerize a Python application - DEV Community
April 14, 2023 - In this article, we will see how to dockerize a simple Python application.
๐ŸŒ
GitHub
github.com โ€บ buildkite โ€บ python-docker-example
GitHub - buildkite/python-docker-example: An example pipeline that runs Python tests inside a Docker container using uv for dependency management.
This repository is an example Buildkite pipeline that tests a Python project using Docker and uv, a fast Python package manager.
Starred by 33 users
Forked by 44 users
Languages ย  Dockerfile 50.0% | Python 50.0% | Dockerfile 50.0% | Python 50.0%
๐ŸŒ
KDnuggets
kdnuggets.com โ€บ containerize-python-apps-with-docker-in-5-easy-steps
Containerize Python Apps with Docker in 5 Easy Steps - KDnuggets
Here, we use Python 3.11 as the base image. We then set the working directory for all the following instructions with the WORKDIR command. We then use the COPY command to copy files from the project into the containerโ€™s file system. Because weโ€™re containerizing a command-line app, we specify the command to execute as โ€œ/bin/bashโ€. Which starts an interactive bash shell when we run the image and start a container. We have our todo.py file and Dockerfile ready.
๐ŸŒ
Docker Hub
hub.docker.com โ€บ _ โ€บ python
python - Official Image | Docker Hub
$ docker build -t my-python-app . $ docker run -it --rm --name my-running-app my-python-app Copy ยท For many simple, single file projects, you may find it inconvenient to write a complete Dockerfile.
๐ŸŒ
Medium
medium.com โ€บ @mark.southworth98 โ€บ building-a-simple-containerized-python-application-e3e2d62e1f2f
Building a Simple Containerized Python Application | by Mark Southworth | Medium
January 3, 2025 - Base Image: I used the Alpine Python image โ€” a lightweight option that keeps the container size small. The base Alpine image is only 5MB, and with Python, my application code, and dependencies, the total size is around 90MB. Thereโ€™s still room for optimization. (Explore Python images on Docker Hub.)
๐ŸŒ
Docker
docker.com โ€บ blog โ€บ containerized-python-development-part-1
Containerized Python Development - Part 1 | Docker
May 24, 2024 - A good way to do this is to create isolated development environments for each project. This can be easily done by using containers and Docker Compose to manage them. We cover this in a series of blog posts, each one with a specific focus. This first part covers how to containerize a Python service/tool and the best practices for it.