Assigning /32 ip addr to containers with docker compose Ubuntu 20.04

782 Views Asked by At

as you might quickly notice, I'm not a network nor a docker guru. I appreciate all help big time!

I'm trying to run a two applications in two different docker container using docker-compose. I managed to run 1 application using the host network. Great success, at least for half of my goal. Every container needs to be mapped to a specific IP address. If you wonder why, it's because the services in the containers will be accessed by other applications on the www on a specific port. But every container uses the same port for this communication. To give you a more visual idea of the desired setup, here's a visual:

required network setup

For service A I linked the container to the host network. My the docker-compose.yml looks like this:

version: "3.8"
services:
  shell:
    image: "${MAIN_IMAGE}"
    container_name: mnm_shell
    network_mode: host
    pid: host
    volumes:
      - "mnms:/home/mnms"
    entrypoint: [ "bash" ]
    env_file:
      - "./.env"

After lots of research I found many articles describing how to make a macvlan with a private address subnet but I did not find any article describing how to link one (or more) /32 networks to specific containers.

Any help, guidance, examples are welcome. Thanks in advance!

1

There are 1 best solutions below

0
On

It's been 6 months since you posted, hopefully you will still find this useful.

You can do what your asking by creating a custom docker network which uses the macvlan driver. Relevant docker reference: https://docs.docker.com/network/network-tutorial-macvlan/#bridge-example

First create the network:

docker network create -d macvlan \
  --subnet=50.50.50.0/24 \
  --gateway=50.50.50.1 \
  -o parent=eth0 \
  my-macvlan-network

Once created you can reference it in your docker-compose files. I've adapted your original example here:

version: "3.8"

networks:
  my-macvlan-network:
    external: true

services:
  shell:
    image: "${MAIN_IMAGE}"
    container_name: mnm_shell
    networks:
      macvlan1:
        ipv4_address: 50.50.50.201
    volumes:
      - "mnms:/home/mnms"
    entrypoint: [ "bash" ]
    env_file:
      - "./.env"

Note: host networking has been removed.

A few things to note:

I'm using this technique on a linux host with PiHole and Gitea containers. Pihole wants port 53 (DNS) and gitea wants port 22 (SSH).