Home Hosting a Docker container with an external IP using Docker Compose and IPvLAN
Post
Cancel

Hosting a Docker container with an external IP using Docker Compose and IPvLAN

Introduction

A docker container should be accessable from an IPv4 address that is not the original IP of the Docker host. This is possible with the IPvLAN driver. This article will show how to configure a Docker container with an external IP using Docker Compose and IPvLAN.

Prerequisites

  • install Docker
  • install Docker Compose

Docker Compose

  • create a docker-compose.yml file with the following content:
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
---
version: '3.8'

networks:
  lan:
    driver: ipvlan
    driver_opts:
      parent: enp0s31f6
    ipam:
      driver: default
      config:
        - subnet: 10.0.0.0/24
          gateway: 10.0.0.1
          ip_range: 10.0.0.3/32

services:
  web:
    image: nginx:latest
    container_name: web
    restart: always
    networks:
      lan:
        ipv4_address: 10.0.0.3
    ports:
      - 80
  • enp0s31f6 is the name of the network interface on the Docker host that is connected to the network where the container should be reachable from
  • 10.0.0.0/24 is the network where the container should be reachable from
  • 10.0.0.1 is the gateway of the network
  • 10.0.0.3/32 is the IP address range for Docker containers
  • 10.0.0.3 is the IP address of the container we’d like to deploy

  • start the container with docker-compose up -d
This post is licensed under CC BY 4.0 by the author.