3

My container changes its ip address extremely often, almost every boot. I tried giving it a static ip address so my script knows how to ssh into it but nothing I did seemed to work. So I tried looking into the host side. No luck either. After doing the below (and rebooting my system). I tried booting with -b -D path -n. When I do I get 0 connectivity. systemctl list-units --type=service show systemd-networkd.service as loaded active running. I'm not sure what could be wrong. The container is alpine and the host is arch

cp /usr/lib/systemd/network/80-container-host0.network /etc/systemd/network/ 
2
  • Did you ever solve this? I have exactly the same problem. Commented Jan 26, 2023 at 17:38
  • @ThomasBrowne, This is pretty late, but I have added an answer that should work. I'm pinging you in case you still have this problem. Commented Mar 5 at 15:27

2 Answers 2

0

For individual containers —

For a container named c with a virtual interface ve-c,

  1. Create a systemD network configuration file in your host machine ...
; /etc/systemd/network/N-ve-c.network [Match] Name=ve-c Driver=veth [Network] ; Assign an address and subnet to this interface; the IP used will be the gateway for containers Address=169.254.0.1/24 ; Turn this on to make IP packets originating in the container be "masked" with the IP of the host; this allows the container to reach the public internet through the host's other interfaces ; IPMasquerade=yes 

... assigning it an IP and a subnet, and instructing it to operate a DHCPServer on that interface

  1. Create a systemD network configuration file on the container ...
; /etc/systemd/network/1-host0.network [Match] Virtualization=container Name=host0 [Network] ; Address this container will have, must be in the subnet assigned to this container's interface on the host Address=169.254.0.2/24 ; Address assigned to this container's interface on the host Gateway=169.254.0.1 ; The DNS server to use; only needed if IPMasquerade is on ; DNS=1.1.1.1 

... assigning it an IP and the subnet assigned on the host-side, and informing it of what the IP of its gateway is.

  1. Ensure that systemd-networkd is the only network manager running on the host and the container: systemctl start --enable systemd-networkd.service

  2. Restart systemd-networkd on your host machine: systemctl restart systemd-networkd

  3. Start the container: machinectl start c

  4. The container should have had a single static IP address assignd to it: machinectl

MACHINE CLASS SERVICE OS VERSION ADDRESSES c container systemd-nspawn arch - 169.254.0.2… 1 machine listed. 

To network between containers —

If you want your containers to be able to talk to each other as well, bridge their interfaces

For containers named c1, c2, c3, ... with virtual interfaces ve-c1, ve-c2, ve-c3, ...

  1. Create a bridge device on your host machine:
; /etc/systemd/network/N-br-container-group.netdev [NetDev] Name=br-container-group Kind=bridge 
  1. Instruct systemd-nspawn to bridge the virtual interfaces created for each machine to the specified network bridge:
; /etc/systemd/nspawn/c{1,2,3,...}.nspawn [Network] Bridge=br-container-group 
  1. Continue as before, assigning an IP and subnet to the interface:
; /etc/systemd/network/N-br-container-group.network [Match] Name=br-container-group ; We are no longer using a veth driver, so ask systemd-networkd to match a bridge device instead ; Driver=veth Kind=bridge ... 

Note: you do not need to create a systemD network configuration file for the virtual interfaces of the containers like before; the required configuration will have been performed by systemd-nspawn on encountering the Bridge=br-container-group instruction.

  1. And instructing each container on what IP and gateway to use, as before; the containers are not aware that they are bridged, so the container-side configuration is identical to the previous case.

Note: Each container shares the same gateway

Note: Each container has a different static IP address, each of which must nevertheless come from the same subnet assigned to the bridge interface

  1. Ensure systemd-networkd is running, and start the container as before

  2. The containers can now communicate with each other, you can confirm this by running a lightweight HTTP server on each container and sending requests between them

0

The following worked for me:

Assign static IP address to systemd-nspawn container (private networking mode).

Note: I just modified the DHCPServer=no option because DHCP is no needed in the static IP assignment and to prevent the conflict with the dnsmasq DHCP server service running on the host in my use case.

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Aug 14 at 20:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.