Skip to content

Instantly share code, notes, and snippets.

@swamibluedata
Last active July 31, 2020 23:03
Show Gist options
  • Save swamibluedata/be0324bf5c201f6fb9b7d189ce39a49f to your computer and use it in GitHub Desktop.
Save swamibluedata/be0324bf5c201f6fb9b7d189ce39a49f to your computer and use it in GitHub Desktop.
Routable ips for docker
#! /bin/bash
# This document assumes following information is available for configuring routable ips
# for containers
# Primary interface to use
# Primary Subnet
# External Gateway ip
# Two static ipaddresses for the containers to use
# Ensure that primary nic is enabled with promiscuous mode.
# For ESX based vms, this has to be done from the vSwitch. For baremetal
# following commands can be used for the same
# ip link set ens32 promisc on
# ip a show ens32 | grep -i promisc
# STEP 1 - Create a docker network for use. This has to be done on both hosts
#
# Set these variables appropriately
NET_NAME="macvlan-net"
# Primary interfafce
IF_NAME="ens32"
# Primary subnet
NETWORK="16.143.20.0/22"
# External Gateway IP
GATEWAY="16.143.20.1"
# Let us go ahead and create a docker network that uses macvlan.
docker network create -d macvlan --subnet $NETWORK --gateway $GATEWAY -o parent=$IF_NAME $NET_NAME
# STEP 2 - Launch container on host 1
IF_NAME="ens32"
IP_ADDR1="16.143.22.246"
CONTAINER_NAME="container-1"
NET_NAME="macvlan-net"
docker run --rm -it --name $CONTAINER_NAME --net $NET_NAME --ip $IP_ADDR1 busybox sh
# STEP 3 - Launch container on host 2
IF_NAME="ens32"
IP_ADDR1="16.143.22.247"
CONTAINER_NAME="container-2"
NET_NAME="macvlan-net"
docker run --rm -it --name $CONTAINER_NAME --net $NET_NAME --ip $IP_ADDR1 busybox sh
# Ip addresses 16.143.212.246,16.143.22.247 should be accessible from outside
# Quick test will be ping the gateway from the container
# TEARDOWN
NET_NAME="macvlan-net"
docker network rm $NET_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment