Step 1: Create a Docker Network
To allow containers to communicate, we need to create a custom bridge network.
docker network create my-network
This command creates a bridge network named my-network where containers can communicate with each other.
Step 2: Connect the Containers to the Network
Next, we need to connect the running containers to the network. You can connect multiple containers to the network by using the docker network connect command.
docker network connect my-network container_name_or_id
docker network connect my-network container1
docker network connect my-network container2
This command adds container1 and container2 to the my-network network.
Step 3: Verify the Network Connection
To verify that the containers are successfully connected to the network, you can inspect the network configuration using the following command:
docker network inspect my-network
Expected output:
[
{
"Name": "my-network",
"Id": "xxxx",
"Created": "2024-12-26T15:21:36.764165956+09:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "xxx",
"Gateway": "xxx"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"xxxxxxx": {
"Name": "container1",
"EndpointID": "xxxxxx",
"MacAddress": "xxxx",
"IPv4Address": "xxx.xxx.xxx.xxx",
"IPv6Address": ""
},
"xxxx": {
"Name": "container2",
"EndpointID": "xxxxx",
"MacAddress": "xxxxx",
"IPv4Address": "xxx.xxx.xxx.xxx",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
You will see the containers listed under the Containers section with their corresponding IP addresses.
Step 4: Identify IP Address or Hostname for Communication
The output of the docker network inspect my-network command shows the IP addresses assigned to the containers. For example:
- container1: xxx.xxx.xxx.xxx
- container2: xxx.xxx.xxx.xxx
You will use these IP addresses to communicate between the containers.
Step 5: Prepare the Receiver Script in Container2
In container2, we will create a Python script to receive data over TCP. This script will listen on port 5000 for incoming connections and print the received data.
Create receive.py in Container2
import socket
# Set up a TCP server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("0.0.0.0", 5000)) # Listen on all network interfaces, port 5000
server_socket.listen(1)
print("Waiting for a connection...")
# Accept a connection from a client
client_socket, client_address = server_socket.accept()
print(f"Connection from {client_address} established.")
try:
while True:
# Receive data from the client
data = client_socket.recv(1024)
if not data:
break
number = int(data.decode())
print(f"Received number: {number}")
except Exception as e:
print(f"Error receiving data: {e}")
finally:
client_socket.close()
server_socket.close()
print("Connection closed.")
Run the receive.py Script in Container2
Once the script is ready, run it inside container2. Use the following command to start the receiver
docker exec -it container2 python /path/to/receive.py
This will start the server in container2, listening on port 5000.
Step 6: Prepare the Sender Script in Container1
In container1, create a Python script to send data (e.g., a number) to container2 over TCP.
Create send.py in Container1
import socket
import time
# Set up a TCP client socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the receiver (container2)
try:
client_socket.connect(("container2_ip", 5000)) # Use the IP of container2
print("Connected to receiver")
# Send a simple number (e.g., 42)
number = 42
client_socket.sendall(str(number).encode())
print(f"Sent number: {number}")
except Exception as e:
print(f"Error: {e}")
finally:
client_socket.close()
print("Connection closed.")
- Note: Replace "container2_ip" with the actual IP address of container2 as obtained from the docker network inspect command.
Run the send.py Script in Container1
Once the sender script is ready, run it inside container1 using the following command:
docker exec -it container1 python /path/to/send.py
This will send the number 42 from container1 to container2.
Step 7: Expected Output
- In container2 (Receiver):
- You should see the following output after running the receive.py script and receiving the data:
Waiting for a connection...
Connection from ('<sender_ip>', 5000) established.
Received number: 42
Connection closed.
- In container1 (Sender):
- After running the send.py script, you should see:
Connected to receiver
Sent number: 42
Connection closed.
Conclusion
In this post, we successfully set up communication between two Docker containers using a custom network. We created Python scripts for sending and receiving data over TCP, and verified the process by running the scripts in the containers. By following these steps, you can easily extend this setup to handle more complex data transfer scenarios between Docker containers.
'Backend Technologies' 카테고리의 다른 글
socket_003:Sending and Receiving Video over Sockets (0) | 2025.01.30 |
---|---|
socket_002: Real-Time Camera Streaming with Sockets (1) | 2025.01.30 |
socket_001: Introduction to Sockets in Python (0) | 2025.01.30 |
프로토콜 버퍼_01: 프로토콜 버퍼 기초 시작하기 (4) | 2024.12.22 |
Protocol Buffers_01: Getting Started with Protobuf Basics (English Version) (3) | 2024.12.22 |