# Docker Network

# Setting up docker network for Vlans

## Setting Up a VLAN and Docker IPvlan Network

This guide demonstrates how to create a VLAN on a host interface and then use it to run a Docker container on a dedicated subnet using the IPvlan driver.

### 1. Create a VLAN Interface

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-sudo-ip-link-add-lin"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`sudo ip <span class="hljs-built_in">link</span> add <span class="hljs-built_in">link</span> eth0 name eth0.107 <span class="hljs-built_in">type</span> vlan <span class="hljs-built_in">id</span> 107sudo ip <span class="hljs-built_in">link</span> <span class="hljs-built_in">set</span> eth0.107 up`</div></div>**Explanation:**

- `ip link add link eth0 name eth0.107 type vlan id 107`  
    This creates a new VLAN interface (`eth0.107`) attached to the physical interface `eth0` with VLAN ID `107`.
- `ip link set eth0.107 up`  
    Activates the VLAN interface so it can start sending and receiving traffic.

---

### 2. Create a Docker IPvlan Network

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-docker-network-creat"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`docker network create -d ipvlan \  --subnet 10.14.107.0/24 \  --gateway 10.14.107.1 \  -o parent=eth0.107 \  test2 `</div></div>**Explanation:**

- `docker network create -d ipvlan`  
    Creates a Docker network using the **IPvlan** driver, which allows containers to appear as if they are directly connected to the physical network.
- `--subnet 10.14.107.0/24`  
    Defines the subnet for the Docker network.
- `--gateway 10.14.107.1`  
    Sets the gateway for the subnet.
- `-o parent=eth0.107`  
    Binds the IPvlan network to the host VLAN interface (`eth0.107`). Replace`eth0.107` with your host interface if different.
- `test2`  
    Name of the Docker network.

---

### 3. Run a Docker Container on the IPvlan Network

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-sudo-docker-run--itd"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`sudo docker run -itd --<span class="hljs-built_in">rm</span> --network test2 --ip 10.14.107.50 --name test2 ubuntu`</div></div>**Explanation:**

- `--network test2`  
    Connects the container to the previously created IPvlan network.
- `--ip 10.14.107.50`  
    Assigns a static IP address to the container within the subnet.
- `--name test2`  
    Gives the container a recognizable name.
- `-itd --rm ubuntu`  
    Runs an Ubuntu container in detached mode, interactive terminal enabled, and removes it automatically when stopped.

---

**Result:**  
You now have a Docker container (`test2`) running on its own VLAN (`eth0.107`) with a dedicated subnet. This setup is useful for network isolation, lab testing, or managing multiple services with their own IP addresses.

---