Home > Software design >  Set static ip for every docker container in my home LAN
Set static ip for every docker container in my home LAN

Time:12-30

I need my docker containers to have a static ip addreses in my lan, so i could access them by unique ip.

I've tried to do this with network, but this did not worked. This is example of docker-compose file i've tried:

version: "3"

services:
   postgres:
    image: postgres:14.1
    container_name: postgres
    restart: always
    ports:
      - "192.168.1.241:5432:5432"
    depends_on:
      - webmin
    environment:
      POSTGRES_USER: openfire
      POSTGRES_PASSWORD: password
      POSTGRES_HOST_AUTH_METHOD: trust
    networks:
      - docker-lab

  webmin:
    image: 'chsliu/docker-webmin'
    container_name: webmin
    restart: always
    ports:
      - "192.168.1.242:10000:10000"
      - "192.168.1.242:53:53/tcp"
      - "192.168.1.242:53:53/udp"
    environment:
      POSTGRES_USER: openfire
      POSTGRES_PASSWORD: password
      POSTGRES_HOST_AUTH_METHOD: trust
    networks:
      - docker-lab

networks:
  docker-lab:
    driver: bridge

When i'm running it with docker-compose -f lab.yml up -d - i'm getting this error:

Creating webmin ... 
Creating webmin ... error

ERROR: for webmin  Cannot start service webmin: driver failed programming external connectivity on endpoint webmin (02633ecc5adf4506bb7217472527cea7a4a3a7ef94d5181ebacc1ff6721c5d93): Error starting userland proxy: listen tcp4 192.168.1.242:10000: bind: cannot assign requested address

ERROR: for webmin  Cannot start service webmin: driver failed programming external connectivity on endpoint webmin (02633ecc5adf4506bb7217472527cea7a4a3a7ef94d5181ebacc1ff6721c5d93): Error starting userland proxy: listen tcp4 192.168.1.242:10000: bind: cannot assign requested address

Docker version:

Docker version 20.10.22, build 3a2c30b docker-compose version 1.29.2, build unknown

System:

NAME="Fedora Linux"
VERSION="37 (Workstation Edition)"
ID=fedora
VERSION_ID=37
VERSION_CODENAME=""
PLATFORM_ID="platform:f37"
PRETTY_NAME="Fedora Linux 37 (Workstation Edition)"

CodePudding user response:

You need to add two virtual IP with that specified IP

/etc/systemd/network/vip1.netdev

[NetDev]
Name=vip1
Kind=dummy

/etc/systemd/network/vip2.netdev

[NetDev]
Name=vip2
Kind=dummy

/etc/systemd/network/vip1.network

[Match]
Name=vip1

[Network]
Address=192.168.1.241
Mask=255.255.255.0
Broadcast=192.168.1.255

/etc/systemd/network/vip2.network

[Match]
Name=vip2

[Network]
Address=192.168.1.242
Mask=255.255.255.0
Broadcast=192.168.1.255

Then restart network service

sudo systemctl restart systemd-networkd.service

Finally run your compose again

docker compose up -d

CodePudding user response:

Solved this task

First i've created virtual interfaces:

ifconfig eth0:1 192.168.1.241 netmask 255.255.255.0 up

ifconfig eth0:1 192.168.1.242 netmask 255.255.255.0 up

Then i've just runned my docker-compose file without a network configuration but with ip's binded to containers. Example:

postgres:
    image: postgres:14.1
    container_name: postgres
    restart: always
    ports:
      #Binding port to IP (ip:port:port)
      - "192.168.1.241:5432:5432"
    depends_on:
      - webmin
    environment:
      POSTGRES_USER: openfire
      POSTGRES_PASSWORD: password
      POSTGRES_HOST_AUTH_METHOD: trust

This post on edureka helped me.

  • Related