How to set Alpine container IP using config file inside container, like Ubuntu Netplan

Trying to get a static IP working for an Alpine container running on an Ubuntu Focal machine.

With Ubuntu containers, this is trivial by creating an /etc/netplan config file.

Someone pass along to me the equivalent config file setup for Alpine.

Current networking shows…

net13 # lxc list net13-template-alpine-315
+---------------------------+---------+-----------------------+----------------------------------------------+-----------+-----------+
|           NAME            |  STATE  |         IPV4          |                     IPV6                     |   TYPE    | SNAPSHOTS |
+---------------------------+---------+-----------------------+----------------------------------------------+-----------+-----------+
| net13-template-alpine-315 | RUNNING | 10.130.226.112 (eth0) | fd42:f1b:cbe0:23be:216:3eff:fee6:a01e (eth1) | CONTAINER | 0         |
|                           |         |                       | fd42:f1b:cbe0:23be:216:3eff:fe4f:1767 (eth0) |           |           |
+---------------------------+---------+-----------------------+----------------------------------------------+-----------+-----------+

Thanks!

Assuming the Alpine instance is using DHCP, you can create a static DHCP allocation using:

lxc config device override <instance> <interface> ipv4.address=n.n.n.n

Never have gotten any lxd config device syntax to work with any container, which is why I use /etc/netplan in Ubuntu containers.

Here’s error…

net13 # lxc config device override net13-template-alpine-315 eth0 ipv4.address=192.99.135.210
Error: The device already exists

Be great if someone can tell me how to do this inside the container, maybe through /etc/network/interfaces syntax.

Use set rather than override, as you can only override a device coming from a profile, but in this case the device must already be in the instance directly:

 lxc config device set net13-template-alpine-315 eth0 ipv4.address=192.99.135.210
net13 #  lxc config device set net13-template-alpine-315 eth0 ipv4.address=192.99.135.210
Error: Invalid devices: Device validation failed for "eth0": Device IP address "192.99.135.210" not within network "lxdbr0" subnet

Yep, thats probably true :slight_smile:
You can’t instruct LXD to create a static DHCP allocation for an IP outside of the parent network’s subnet.

What does lxc network show lxdbr0 show?

config:
  ipv4.address: 10.130.226.1/24
  ipv4.nat: "true"
  ipv6.address: fd42:f1b:cbe0:23be::1/64
  ipv6.nat: "true"
description: ""
name: lxdbr0
type: bridge
used_by:
- /1.0/instances/net13-david-favor
- /1.0/instances/net13-david-favor
- /1.0/instances/net13-david-favor-fixdeliver
- /1.0/instances/net13-david-favor-fixdeliver
- /1.0/instances/net13-dns-ns12-focal
- /1.0/instances/net13-dns-ns12-focal
- /1.0/instances/net13-fixdeliver-mta
- /1.0/instances/net13-fixdeliver-mta
- /1.0/instances/net13-hivelocity
- /1.0/instances/net13-hivelocity
- /1.0/instances/net13-jasites-author-projects
- /1.0/instances/net13-jasites-author-projects
- /1.0/instances/net13-jasites-health-projects
- /1.0/instances/net13-jasites-health-projects
- /1.0/instances/net13-joymta-v1
- /1.0/instances/net13-joymta-v1
- /1.0/instances/net13-joymta-v2
- /1.0/instances/net13-joymta-v2
- /1.0/instances/net13-template-alpine-315
- /1.0/instances/net13-template-alpine-315
- /1.0/instances/net13-template-focal
- /1.0/instances/net13-template-focal
- /1.0/instances/net13-website-tester
- /1.0/instances/net13-website-tester
- /1.0/profiles/default
managed: true
status: Created
locations:
- none

Also here’s the routing table…

net13 # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         54.39.133.254   0.0.0.0         UG    100    0        0 eno1
10.130.226.0    0.0.0.0         255.255.255.0   U     0      0        0 lxdbr0
54.39.133.0     0.0.0.0         255.255.255.0   U     0      0        0 eno1
54.39.133.254   0.0.0.0         255.255.255.255 UH    100    0        0 eno1
142.44.168.80   0.0.0.0         255.255.255.240 U     0      0        0 lxdbr0
192.99.135.208  0.0.0.0         255.255.255.240 U     0      0        0 lxdbr0

192.99.135.210 is not in the lxdbr0 subnet 10.130.226.0/24

Correct.

What’s required is 192.99.135.210 being set as the container’s static IP.

This is trivial with Ubuntu, just create an /etc/netplan file.

What I’m asking about is a similar trivial config for Alpine containers, which run old V1 networking, so /etc/network/interfaces is likely file to modify.

The following /etc/network/interfaces seems to work.

Someone let me know if this is the correct approach or there’s a better way.

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
hostname $(hostname)

auto eth1
iface eth1 inet static
address 192.99.135.210
netmask 255.255.255.0
gateway 10.130.226.1

Thanks!