How can I change the IP of a container or start it with a specified IP?

Hello,
I’m trying to launch a container with a specific IP, or alter it after creation. Is there a way to do this?

For example:

  • The network settings for default lxdbr0 are ipv4.addres: 10.15.78.1/24
  • If I create a container with lxc launch ubuntu:16.04 my-container it may get the IP 10.15.78.119.
  • Now I want to change it to 10.15.78.100

I tried these commands, but they didn’t seem to do the trick:

lxc config device add my-container eth0 nic name=eht0 nictype=bridged parent=lxdbr0
lxc config device set my-container eth0 ipv4.address 10.15.78.100

I’ve looked at link and there they discuss changing the network settings themselves and not the containers IP settings.

Which os is your container ?
Depending of Linux version, you can set ip address on configuration files :
/etc/network/interfaces for ubuntu
/etc/sysconfig/network-scripts/if… for centos/redhat

Hi, thanks for the reply @zoum, I’ll look into it. Was actually hoping there was a lxc command that one could run to set the IP or an argument I could add when calling the launch command.

See

on how to set statically the IP address of a container on a managed network using lxc commands.

To do so, you need to

  1. stop the container
  2. attach the lxdbr0 interface to the container
  3. set the IP address
  4. start the container again.

I got it working :slight_smile:

First off, the name was incorrect in my device add command… eht0 instead of eth0.
And secondly, I had to restart the container.

Thanks for the response @simos. Your message came through just as I figured out what I did wrong, but I bet it would’ve helped me figure it out. :smiley:

Hello Ruan,

Although its not an answer to your question it might be a good idea to put your containers behind a proxy over which you have more control.

I feel it is a little easier to add more ip addresses to the proxy and serve your container apps via them.

Additionally you can take a backup of the container create a new one at some other ip and restore the data.

Hope this helps.

Hi @brickcap, thanks for the suggestion. I will certainly keep it in mind.