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.