Add a network interface to the container
1 Run a container in the default network mode
# docker run --name tst_add_inf -it tst_img /bin/bash
In this way, we create a container named tst_add_inf through the mirror tst_img on the host machine. This container has created a network interface eth0 by default.
2 Get the PID of the container
# docker inspect -f '{{.}}' tst_add_inf
The PID of the container obtained above is the process PID of the container process No. 1 in the host machine namespace.
3 Add network interface eth1 to the container
(1) Create a pair of veth peer devices
# ip link add veth0 type veth peer name veth1
After creation, you can see the two devices you just created through "ip link list".
(2) Add veth end to the bridge
# brctl addif docker0 veth0 # ip link set veth0 up
(3) Associate the other end of veth with the container
# ln -s /proc/$pid/ns/net /var/run/netns/$container_id # ip link set veth1 netns $pid
(4) Configure the newly added network interface of the container
Rename the new interface to eth1 and modify its IP address.
# ip netns exec $pid ip link set dev veth1 name eth1 # ip netns exec $pid lp link set eth1 up
After the container is started, you can use "docker network connect" to operate, but this means that the process is already running and may miss new ones.
This question is about searching for docks and multiple network interfaces. Although not the required version in my leave here some information:
With Docker 1.12, multiple network interfaces can be added to the docker container, but first you need to create the container and then attach a second (and subsequent) network NIC before starting the container:
$docker create --network=network1 --name container_name containerimage:latest $docker network connect network2 container_name $docker start container_name
You need to create a network first:
$docker network create --driver=bridge network1 --subnet=172.19.0.0/24 $docker network create --driver=bridge network2 --subnet=172.19.1.0/24
Additionally, you can start the container of the Dockerhost network interface using the --network=host parameter in running docker:
$docker run --net=host containerimage:latest
Translated from:/questions/34110416/start-container-with-multiple-network-interfaces
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.