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