Home Explore Blog CI



docker

1st chunk of `content/manuals/engine/network/tutorials/macvlan.md`
9dc23a3663e7dfb20c00dcfbcb59a68913cbb1f5cbf9d0270000000100000eb3
---
title: Networking using a macvlan network
description: Tutorials for networking using a macvlan bridge network and 802.1Q trunk
  bridge network
keywords: networking, macvlan, 802.1Q, standalone
aliases:
  - /network/network-tutorial-macvlan/
---

This series of tutorials deals with networking standalone containers which
connect to `macvlan` networks. In this type of network, the Docker host accepts
requests for multiple MAC addresses at its IP address, and routes those requests
to the appropriate container. For other networking topics, see the
[overview](/manuals/engine/network/_index.md).

## Goal

The goal of these tutorials is to set up a bridged `macvlan` network and attach
a container to it, then set up an 802.1Q trunked `macvlan` network and attach a
container to it.

## Prerequisites

- Most cloud providers block `macvlan` networking. You may need physical access
  to your networking equipment.

- The `macvlan` networking driver only works on Linux hosts, and is not supported
  on Docker Desktop or Docker Engine on Windows.

- You need at least version 3.9 of the Linux kernel, and version 4.0 or higher
  is recommended.

- The examples assume your ethernet interface is `eth0`. If your device has a
  different name, use that instead.

- The `macvlan` driver is not supported in rootless mode.

## Bridge example

In the simple bridge example, your traffic flows through `eth0` and Docker
routes traffic to your container using its MAC address. To network devices
on your network, your container appears to be physically attached to the network.

1.  Create a `macvlan` network called `my-macvlan-net`. Modify the `subnet`, `gateway`,
    and `parent` values to values that make sense in your environment.

    ```console
    $ docker network create -d macvlan \
      --subnet=172.16.86.0/24 \
      --gateway=172.16.86.1 \
      -o parent=eth0 \
      my-macvlan-net
    ```

    You can use `docker network ls` and `docker network inspect my-macvlan-net`
    commands to verify that the network exists and is a `macvlan` network.

2.  Start an `alpine` container and attach it to the `my-macvlan-net` network. The
    `-dit` flags start the container in the background but allow you to attach
    to it. The `--rm` flag means the container is removed when it is stopped.

    ```console
    $ docker run --rm -dit \
      --network my-macvlan-net \
      --name my-macvlan-alpine \
      alpine:latest \
      ash
    ```

3.  Inspect the `my-macvlan-alpine` container and notice the `MacAddress` key
    within the `Networks` key:

    ```console
    $ docker container inspect my-macvlan-alpine

    ...truncated...
    "Networks": {
      "my-macvlan-net": {
          "IPAMConfig": null,
          "Links": null,
          "Aliases": [
              "bec64291cd4c"
          ],
          "NetworkID": "5e3ec79625d388dbcc03dcf4a6dc4548644eb99d58864cf8eee2252dcfc0cc9f",
          "EndpointID": "8caf93c862b22f379b60515975acf96f7b54b7cf0ba0fb4a33cf18ae9e5c1d89",
          "Gateway": "172.16.86.1",
          "IPAddress": "172.16.86.2",
          "IPPrefixLen": 24,
          "IPv6Gateway": "",
          "GlobalIPv6Address": "",
          "GlobalIPv6PrefixLen": 0,
          "MacAddress": "02:42:ac:10:56:02",
          "DriverOpts": null
      }
    }
    ...truncated
    ```

4.  Check out how the container sees its own network interfaces by running a
    couple of `docker exec` commands.

    ```console
    $ docker exec my-macvlan-alpine ip addr show eth0

    9: eth0@tunl0: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:10:56:02 brd ff:ff:ff:ff:ff:ff
    inet 172.16.86.2/24 brd 172.16.86.255 scope global eth0
       valid_lft forever preferred_lft forever
    ```

Title: Networking with Macvlan Networks
Summary
This document provides a tutorial on how to network standalone containers using `macvlan` networks, including setting up a bridged `macvlan` network and an 802.1Q trunked `macvlan` network. It outlines prerequisites, such as Linux host requirements and kernel versions, and provides a step-by-step example of creating a bridged `macvlan` network and attaching an Alpine container to it.