With Docker 1.12+ it’s possible to add more than one network interface to a docker container with about five lines of shell commands.
First: you must create the Docker networks network1
and network2
via docker network create
shell command:
docker network create --driver=bridge network1 --subnet=172.19.0.0/24
docker network create --driver=bridge network2 --subnet=172.19.1.0/24
notes:
- in this example there are two networks
- for N networks, you would have N lines ensuring each network name and subnet is unique
Second: create the container and attach the Docker networks (prior to starting the container) via shell commands:
docker create --network=network1 --name container_name image_name:tag_name
docker network connect network2 container_name
notes:
- the
docker create
has the first network on purpose because otherwise a default network interface will be created and your container will have 3 network interfaces instead of the expected 2 network interfaces
Third: start your container
docker start container_name
tested with:
- docker version 20.x, rockylinux:8
- when i get into the container command line shell
ifconfig
shows two network interfaceseth0
,eth1
(not countinglo
)
p.s. Also, you can start the container attaching the dockerhost network interfaces by using the –network=host argument in docker run:
docker run --net=host image_name:tag_name
p.p.s. If you want the container to be interactive change the create
command and start
command to:
docker create -it --name container_name image_name:tag_name bash --login
docker start -ai container_name