Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

Docker network bridge mapped with HOST OS network interfaces

Hi I am new in docker network . Basically I want to start a docker container that should be mapped with existing HOST OS network interface .

For e.g. List of HOST OS network interfaces

$>>ip a
1. ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 
4. ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000

Now I want to create a Docker Network Bridge (mapped with ens192/ens224) using

$ docker network create -d bridge my-bridge-network

And then run the container using the docker network

$ docker run -itd --network=my-bridge-network mydocker

But with mentioned steps I am not able to map network interfaces with docker networks.

question from:https://stackoverflow.com/questions/66046596/docker-network-bridge-mapped-with-host-os-network-interfaces

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...