LXC: Is there a way to setup nameserver on container config?

8k views Asked by At

Is there a way to setup nameserver on LXC container config?

It seems like it's not ideal to modify /etc/network/interfaces inside a (Ubuntu) container when you want to setup a static ip. So I naturally guess that there is probably a way to setup nameserver on a container config, but I don't find the parameter, and I don't know if such thing exists.

Right now, what I do for a Ubuntu container is to setup nameserver on rootfs/etc/resolvconf/resolv.conf.d/base.

2

There are 2 answers

0
frater_sourcecode On

I saw no documented lxc.net option to add nameserver to container's interfaces when using static IP . First of all , consider whether you need custom resolver config for each container, using one caching dns resolver from host is much easier .

Brute-force approach:

if you have only one interface, you can use hook in container's config: lxc.hook.pre-start=/var/lib/lxc/nameserver_set.sh

where nameserver_set.sh has something like

#! /bin/bash
NAMESERVER=`cat /var/lib/lxc/${LXC_NAME}/nameserver`
echo "dns-nameserver $NAMESERVER" >> ${LXC_ROOTFS_PATH}/etc/network/interfaces

roundabout-dns-way: you can sort-of work around by assigning dns-nameserver and IP via dhcp: (look at https://askubuntu.com/a/571095 ) ,

by setting LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf in /etc/default/lxc-net ,
dhcp-hostsfile=/etc/lxc/dnsmasq-hosts.conf in /etc/lxc/dnsmasq.conf ,

and finally raw.dnsmasq: dhcp-option=option:dns-server,8.8.8.8 in /var/lib/lxc/{container}/config

note: It may or might not need much more config fiddling , depending on your host

why is it this way?

under lxc1 (not lxd) , it seems that resolv.conf and /etc/network/interfaces generation is left to each template's discretion (e.g ubuntu uses resolvconf, or determines via debootstrap, fedora/centos copies hosts resolv.conf ,... ) ,same for interfaces, see /usr/share/lxc/templates/lxc-ubuntu .

But, this also means you can make copy of the template file and customize it according to taste . (just remember to compare it after upgrades - it will break if there are major changes)

Under lxd (lxc v2) : , things seem to be moving towards using cloud-init , so check https://github.com/lxc/lxd/blob/master/doc/cloud-init.md , cloud-init's yaml seems like a nice way to configure things, but I didn't get to use it much.

0
Harry Lee On

I use LXD 3

For example, if your nameserver is 8.8.8.8 and 8.8.4.4, just run this command and reboot your container.

lxc network set lxdbr0 raw.dnsmasq dhcp-option=6,8.8.8.8,8.8.4.4 (6 means the DNS configuration)

Then in the container, try cat /etc/resolv.conf, you will see like this:

domain lxd
search lxd
nameserver 8.8.8.8
nameserver 8.8.4.4