Static IP for CentOS LXC container

LXC containers get dynamic IP from DHCP. When you stop and start a container, its IP gets changed. If you hosting some web application on this container, you need to point the application to new IP. To avoid this, you can configure static IP on the container.

LXC containers get IP in the range 10.0.3.2-255. To make CentOS container IP static, edit file

vi /etc/sysconfig/network-scripts/ifcfg-eth0 

Find

BOOTPROTO=dhcp

Replace with

BOOTPROTO=STATIC

Add below

IPADDR=10.0.3.2
GATEWAY=10.0.3.1
DNS1=1.1.1.1
DNS1=8.8.8.8

10.0.3.2 = replace with any unused IP in the range your LXC container assign using DHCP.

Create a static route file

vi /etc/sysconfig/network-scripts/route-eth0

Add

10.0.3.1 dev eth0
default via 10.0.3.1 dev eth0

After restarting the LXC container, you will have a fixed IP.

reboot

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *