You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何从一个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:

1. Configure Container2 as the Router

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 line net.ipv4.ip_forward=1, then run sysctl -p to 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>
2. Set Up SSH Access for Remote Commands

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, change PermitRootLogin prohibit-password to PermitRootLogin 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"
3. Deploy the Persistent Server and Collect Statistics

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 ss or tcpdump:
    • 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.
4. Modify tc netem Parameters and Rerun Tests

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%"
  • 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 &"
  • Finally, initiate new connections from your client and collect updated statistics to compare with previous test results.

内容的提问来源于stack exchange,提问作者AmithRc

火山引擎 最新活动