1

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)"
Hid-K
  • 26
  • 3
  • How do you assign IP addresses to other individual processes on your host? Have you assigned the mentioned addresses on 192.168.1.0/24 to a host interface already? – David Maze Dec 29 '22 at 11:17

2 Answers2

1

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
Xirehat
  • 1,155
  • 1
  • 8
  • 20
  • This worked but ip is only available on machine which running this container. Also SSH does not available anymore – Hid-K Dec 29 '22 at 08:41
  • I do not understand what you said `but ip is only available on machine which running this container`! you cannot assign your home lan IP to container directly, you must do this way and expose your port on virtual ip. Do you want to ssh 192.168.1.241-242 ? – Xirehat Dec 29 '22 at 08:46
  • No. I don't want SSH on container. I need it only on my server which running this containers. – Hid-K Dec 29 '22 at 08:50
  • I need to have an access to this containers in my LAN by their own ip's, not by ip of server. – Hid-K Dec 29 '22 at 08:51
0

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.

Hid-K
  • 26
  • 3