The goal
Assuming that you want your Docker container to display to the network as an ordinary device, you can "map" a host interface using a macvlan
bridge. What you will get:
- Docker clients receive an IP from the network the host is in
- Other devices can access the container as if it was in their physical network
- The container can access the same hosts as its host via the given interface
If that is not what you meant, please leave a comment and further explain your scenario.
How it's done
Check weather macvlan
module is installed using
lsmod | grep macvlan
If it is not listed, install it by issuing
modprobe macvlan
Using Docker CLI
Create a network with the interface you want to share and specify subnet and gateway (gateway is optional if you want to use the internet via this network):
docker network create -d macvlan --subnet=1.2.3.4/24 --gateway=1.2.3.1 -o parent=eth0 nice_name
(parent must be changed from eth0
to your interface)
Using Docker-Compose
You can also create those networks on-demand and include them in your docker-compose.yml
files like in the following example:
version: '3.3'
services:
nginx1:
restart: unless-stopped
image: nginx:latest
networks:
- nice_name
networks:
private:
nice_name:
driver: macvlan
driver_opts:
parent: eth0 # change this
ipam:
config:
- subnet: "1.2.3.0/24" # change this
gateway: "1.2.3.1" # change this (optional)
# Be aware that there is no "-" before "gateway" as it belongs to the subnet!
If you have already created the network outside of Docker-Compose, you can still connect clients to it that use Docker-Compose by using the external
parameter:
version: '3.3'
services:
nginx1:
restart: unless-stopped
image: nginx:latest
networks:
- nice_name
networks:
nice_name:
external: true
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…