I want to run two Docker containers side by side using docker-compose where the two contains will talk to each other using UDO. The caveat is that I don't want to run the two containers by treating them as part of the host, via network_mode: "host".
Previous docker-compose.yml implementation
version: '3.7'
services:
udpclient:
build: ./Client/
container_name: udp_client
# ports:
# - 13371:13371
# networks:
# - eb
network_mode: "host"
udpserver:
build: ./Server/
container_name: udp_server
# ports:
# - 13371:13371
# networks:
# - eb
network_mode: "host"
# networks:
# eb:
This works as expected between my Client & Server.
udp_server | (b'Hello UDP Server', ('127.0.0.1', 51645))
udp_server | (b'Hello UDP Server', ('127.0.0.1', 51645))
udp_server | (b'Hello UDP Server', ('127.0.0.1', 51645))
Now if I expose the two ports inside my Dockerfile and cleanup my docker-compose file, shown below:
docker-compose.yml
version: '3.7'
services:
udpclient:
build: ./Client/
container_name: udp_client
ports:
- 13371:13371
networks:
- eb
udpserver:
build: ./Server/
container_name: udp_server
ports:
- 13371:13371
networks:
- eb
networks:
eb:
Client/Dockerfile
FROM nvidia/cuda:11.7.0-devel-rockylinux8
WORKDIR /opt/testing
RUN yum install -y python3 python3-pip
EXPOSE 13371
ADD client.py .
ENTRYPOINT ["python3", "-u", "client.py"]
Server/Dockerfile
FROM nvidia/cuda:11.7.0-devel-rockylinux8
WORKDIR /opt/testing
RUN yum install -y python3 python3-pip
EXPOSE 13371
ADD server.py .
ENTRYPOINT ["python3", "-u", "server.py"]
I receive the following error:
$ docker-compose up
Removing udp_server
Recreating udp_client ...
Recreating 554441a04527_dockercommunication_
Recreating udp_client ... errorWARNING: Host is already in use by another container
ERROR: for udp_client Cannot start service udpclient: driver failed programming external connectivity on endpoint udp_client (228e9768d0f70e4203a46934c163fe8bed1f48894dfbc93fc4b1e8a67169cc8e): Bind for 0.0.0.0:13371 failed: port is already allocated
ERROR: for udpclient Cannot start service udpclient: driver failed programming external connectivity on endpoint udp_client (228e9768d0f70e4203a46934c163fe8bed1f48894dfbc93fc4b1e8a67169cc8e): Bind for 0.0.0.0:13371 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
The error hints that because I'm using the first reserved 13371 for the server or client, whatever comes first.
How can I clean this up where the two connections can happen without being on the host network.
CodePudding user response:
Docker Compose automatically creates a network for your containers. Since you don't need to rely on the host network, you can remove the ports property altogether.
Containers on the network will be reached by their service names, for example:
udpclient:13371
udpserver:13371
version: '3.7'
services:
udpclient:
build: ./Client
container_name: udp_client
udpserver:
build: ./Server
container_name: udp_server