docker is an example of a method to start a container with multiple network interfaces

  • 2020-12-10 01:02:40
  • OfStack

Add a network interface to the container

1 runs 1 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 on the host machine, which by default has created a network interface, eth0.

Get PID for the container


# docker inspect -f '{{.State.Pid}}' tst_add_inf

The PID obtained above for the container is PID for the container no. 1 process in the host machine namespace.

Add the network interface eth1 to the container

(1) Create 1 pair of veth peer devices


# ip link add veth0 type veth peer name veth1 

Once created, you can see the two devices just created via "ip link list".

(2) Add the veth1 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 change its IP address.


# ip netns exec $pid ip link set dev veth1 name eth1
# ip netns exec $pid lp link set eth1 up

Once the container is started, you can operate with "docker network connect," but this means that the process is already running and may miss the new one.

The question is about searching for docks and multiple network interfaces. Although not the required version of 1 some information before I leave here:

Using Docker 1.12, you can add multiple network interfaces to the docker container, but you need to create the container first, then append the 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

In addition, you can use the network = host parameter running on docker to start the container of the Dockerhost network interface:


$docker run --net=host containerimage:latest

Translated from: http: / / stackoverflow com/questions / 34110416 / start - container - with - multiple - network - interfaces


Related articles: