如何从一个Docker容器SSH到另一个?附多容器场景操作需求
Alright, let's walk through setting up and managing this multi-container test setup step by step—here's how to make everything work smoothly:
First, you need to enable IP forwarding on Container2, which is critical for routing traffic between Container1 and Container3:
- Access Container2's shell:
docker exec -it Container2 bash - Enable IP forwarding temporarily:
echo 1 > /proc/sys/net/ipv4/ip_forward - To make this setting persistent across container restarts, edit
/etc/sysctl.conf, uncomment the linenet.ipv4.ip_forward=1, then runsysctl -pto apply changes.
Next, set the default gateway of Container1 and Container3 to point to Container2's IP:
- For Container1:
docker exec -it Container1 ip route add default via <Container2_IP> - For Container3:
docker exec -it Container3 ip route add default via <Container2_IP>
To run commands on Container2 and Container3 via SSH, install and configure the SSH server in both containers:
- Access the target container (take Container3 as an example):
docker exec -it Container3 bash - Install openssh-server:
apt update && apt install -y openssh-server - Allow root login (this is convenient for testing, not recommended for production environments): Edit
/etc/ssh/sshd_config, changePermitRootLogin prohibit-passwordtoPermitRootLogin yes - Set a root password:
passwd root - Restart the SSH service:
service ssh restart
Now you can connect from your client (or the host machine) via SSH:
- Connect to Container2:
ssh root@<Container2_IP> - Connect to Container3:
ssh root@<Container3_IP>
You can also run single commands directly without entering the shell, e.g.:ssh root@<Container3_IP> "tc qdisc show"
Assuming you have a custom server program, here's how to run it and gather connection stats:
- Start the server in the background on Container3:
nohup ./your-server-program > server-output.log 2>&1 & - To collect connection statistics, use tools like
ssortcpdump:- View real-time connections:
ssh root@<Container3_IP> "ss -tulnp | grep <your-server-port>" - Capture traffic to a file for later analysis:
ssh root@<Container3_IP> "tcpdump -i any port <your-server-port> -w capture.pcap" - Alternatively, build statistics logic directly into your server program and output data to a log file for easy comparison.
- View real-time connections:
tc netem is used to simulate network conditions like latency or packet loss. Here's how to update parameters and restart testing:
- First, clear any existing qdisc rules on Container3 (if present):
ssh root@<Container3_IP> "tc qdisc del dev eth0 root" - Add new network emulation rules, for example:
- Add 100ms latency:
ssh root@<Container3_IP> "tc qdisc add dev eth0 root netem delay 100ms" - Add 5% packet loss:
ssh root@<Container3_IP> "tc qdisc add dev eth0 root netem loss 5%"
- Add 100ms latency:
- Restart your server program to apply changes (or just initiate new test connections):
- Find the server's PID:
ssh root@<Container3_IP> "ps aux | grep your-server-program" - Kill the existing process:
ssh root@<Container3_IP> "kill <PID>" - Restart the server:
ssh root@<Container3_IP> "nohup ./your-server-program > new-test-output.log 2>&1 &"
- Find the server's PID:
- Finally, initiate new connections from your client and collect updated statistics to compare with previous test results.
内容的提问来源于stack exchange,提问作者AmithRc




