When you set up LXD with the default private bridge, you are asking from LXD to provide a DNS server (dnsmasq
) to resolve the names of the containers. If the container is called mycontainer
, then this DNS server will have an entry for mycontainer
and will answer to DHCP requests from the specify container about the mycontainer
name. Then, it’s up to your container image to honor the DHCP reply and use it.
However, you can also manually change the network settings in a container, and use a different name, like mycontainer2
. This will make it a bit messy because both mycontainer
and mycontainer2
will work for this container.
Therefore, how do we solve this impending mess when we really want to change the name of a container? We solve it my making sure to update both LXD and the container with the new name!
Let’s create mycontainer
.
$ lxc launch ubuntu:18.04 mycontainer
Creating mycontainer
Starting mycontainer
What’s the name in /etc/hostname
?
$ lxc exec mycontainer -- cat /etc/hostname
mycontainer
What does systemd
know about this?
$ lxc exec mycontainer -- hostnamectl status
Static hostname: mycontainer
Icon name: computer-container
Chassis: container
Machine ID: d28fb7af31513827378d9dd878022d40
Boot ID: df6a83db2c6baaef9e399282939e3856
Virtualization: lxc
Operating System: Ubuntu 18.04.1 LTS
Kernel: Linux 4.15.0-33-generic
Architecture: x86-64
Let’s change first the container name to mycontainer2
.
$ lxc rename mycontainer mycontainer2
Error: Renaming of running container not allowed
Exit 1
Oh, busted!
$ lxc stop mycontainer
$ lxc rename mycontainer mycontainer2
$ lxc start mycontainer
Error: not found
Exit 1
$ lxc start mycontainer2
What does the container know about its name now?
$ lxc exec mycontainer2 -- cat /etc/hostname
mycontainer
$ lxc exec mycontainer2 -- hostnamectl status
Static hostname: mycontainer
Icon name: computer-container
Chassis: container
Machine ID: d28fb7af31513827378d9dd878022d40
Boot ID: df6a83db2c6baaef9e399282939e3856
Virtualization: lxc
Operating System: Ubuntu 18.04.1 LTS
Kernel: Linux 4.15.0-33-generic
Architecture: x86-64
Oh, oh! The container still remembers the first name that was given during the first boot. It’s a static hostname because it is written in the file /etc/hostname
.
How do we fix this so that the container is really called mycontainer2
?
$ lxc exec mycontainer2 -- hostnamectl set-hostname mycontainer2
That was it? Did this really work? Let’s see.
$ lxc exec mycontainer2 -- cat /etc/hostname
mycontainer2
$ lxc exec mycontainer2 -- hostnamectl status
Static hostname: mycontainer2
Icon name: computer-container
Chassis: container
Machine ID: d28fb7af31513827378d9dd878022d40
Boot ID: df6a83db2c6baaef9e399282939e3856
Virtualization: lxc
Operating System: Ubuntu 18.04.1 LTS
Kernel: Linux 4.15.0-33-generic
Architecture: x86-64