HyperAIHyperAI

Inter-Container Communication

If you need to transfer data between two running containers, the most convenient method is through ssh / scp or rsync.

Get Target Container IP

First, log in to the target container (the container you want to connect to), open the Terminal and execute the following to view the container IP:

ip a

If the ip command is not found, you need to install it first:

apt install iproute2 -y
ip a

You will get results like this:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/ipip 0.0.0.0 brd 0.0.0.0
4: eth0@if96: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1440 qdisc noqueue state UP group default
    link/ether 2a:d7:3a:68:13:ee brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.38.6.87/32 scope global eth0
       valid_lft forever preferred_lft forever

In the output, find the IP address corresponding to eth0, which is 10.38.6.87 in this case.

Connect from Other Containers

Then in another container (the container initiating the connection), connect to the target container with the following command:

:::info Containers created by the same user already have keys prepared and do not require a password or the -i parameter for direct connection. :::

ssh 10.38.6.87

After a successful connection, you can transfer files between containers:

# Copy file from current container to target container
scp /path/to/local/file 10.38.6.87:/path/to/remote/

# Copy file from target container to current container
scp 10.38.6.87:/path/to/remote/file /path/to/local/

# Sync directory using rsync
rsync -avz /path/to/local/dir/ 10.38.6.87:/path/to/remote/dir/