Post

Archive - Connecting the Docker container to the external network via VLAN

Create Docker network

Get network id:

1
`ip addr show`

Create the Docker network with MACVLAN driver. The part ens192 of parent adapts to the environment.

1
2
3
4
5
6
7
8
9
 docker network create -d macvlan \
    --subnet=172.6.2.0/24 \
    --gateway=172.6.2.1 \
    -o parent=enp2s0.20 vlan20

 docker network create -d macvlan \
    --subnet=172.6.4.0/24 \
    --gateway=172.6.4.1 \
    -o parent=enp2s0.40 vlan40

Create Docker container

Create a container by specifying the created network and IP address. You can use Ping & Traceroute, so use Alpine.

1
2
3
4
5
docker run -it -d --rm \
   --net=vlan20 \
    --ip=172.6.2.201 \
    --name container-vlan20 \
    alpine /bin/sh

Check working

Confirm communication from each container.

1
2
 docker exec -it container-vlan20 /bin/sh
 ping 8.8.8.8

Docker compose

cat ./docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
version: '2.1'

services:
  vlan20:
    image: alpine
    container_name: container-vlan20
    command: ['tail', '-f', '/dev/null']
    networks:
      vlan20:
        ipv4_address: 172.6.2.200
  vlan40:
    image: alpine
    container_name: container-vlan40
    command: ['tail', '-f', '/dev/null']
    networks:
      vlan30:
        ipv4_address: 172.6.4.200


networks:
  vlan20:
    name: vlan20
    driver: macvlan
    driver_opts:
      parent: enp2s0.20
    ipam:
      config:
        - subnet: 172.6.2.0/24
          gateway: 172.6.2.1
  vlan30:
    name: vlan40
    driver: macvlan
    driver_opts:
      parent: enp2s0.40
    ipam:
      config:
        - subnet: 172.6.4.0/24
          gateway: 172.6.4.1

Fix usb mount

1
nano /etc/udev/rules.d/99-usb-serial.rules

Insert the following line:

1
SUBSYSTEM=="usb", ATTRS{idVendor}=="051d", ATTRS{idProduct}=="0002",ATTRS{serial}=="9B2053A15027",GROUP="tty", SYMLINK+="ttyUSB-UPS"
This post is licensed under CC BY 4.0 by the author.