How do I get the IP address of an LXC container?

Seeing as running things in containers doesn't appear to be supported in Ubuntu, my next best suggestion is to look at the IP address leases that dnsmasq is handing out. That's really simple:

$ cat /var/lib/misc/dnsmasq.leases
1363699477 00:16:3e:4a:ce:a4 10.0.3.83 containername *

There are only two parts there that are of any use, so we can format that up a lot nicer:

$ awk '{ print $4,$3 }' /var/lib/misc/dnsmasq.leases | column -t
containername  10.0.3.83

The easiest way to do this now is:

lxc-info -n container-name -iH

This returns the IP address with no other text.

The -i option specifies that the IP address should be returned and the -H option disables human readable output i.e. labels. For more info see the lxc-info man page.

EDIT for newer version of LXC:

lxc info container-name

Then you get detailed info. Look at the "Ips:" block, which should look like the one below. You might one to grab the first IPv4 address (10.121.48.241) in this case:

Ips:
  eth0: inet    10.121.48.241   vethSBP4RR
  eth0: inet6   fda5:b9a9:f3b9:ba32:216:3eff:fe4a:4d7d  vethSBP4RR
  eth0: inet6   fe80::216:3eff:fe4a:4d7d    vethSBP4RR
  lo:   inet    127.0.0.1
  lo:   inet6   ::1

Technically speaking you should be able to use lxc-attach to connect and fire in a command (and process the output), like so:

sudo lxc-attach --name containername -- ifconfig

This requires the container to be running.

Note: I couldn't get this to work. I installed LXC and tried this but just saw a mush of namespace errors, missing files and other nonsense. But my only experience with LXC is the 10 minutes I've spent on this question. It might work. It might not. Good luck!

Tags:

Lxc

12.04