Container network name

I found some strange behavior of the nixos-containers.

When I assign a localAddress and a hostAddress, the container name is nicely added to /etc/hosts, and I can reach is with container-name.containers.

If I don’t do it, the container gets an network, but it isn’t added to /etc/hosts

In general, I try to care as little as possible about the numerical addresses, and defining them could lead to collisions. Is it possible to create containers with /etc/hosts entries without defining your own adresses?

Usually there is a nss-mymachines module that should resolve hostname to an ip for each container registered with machined.
However in the nixos-container case, scripts does take care of the network so machined does not its ip. Maybe there is a way around that. I was playing in the past with the machined dbus interface, maybe the ip address could be added this way.

Mhm, appearently it knows the address of my container:

$ machinectl status database
           Since: Fri 2018-09-07 23:47:33 BST; 3min 43s ago
          Leader: 29324 (systemd)
         Service: systemd-nspawn; class container
            Root: /var/lib/containers/database
           Iface: ve-database
            Unit: container@database.service
                  │ ├─29324 systemd
                  │ ├─29558 /nix/store/rx76y57vl69ynnig325pid8jb0466298-systemd-239/lib/systemd/systemd-journald
                  │ ├─29585 nscd
                  │ ├─29587 /nix/store/mbvaa8kr2xmdin4gwls388ga98rgjgbz-dbus-1.12.10/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
                  │ ├─29609 /nix/store/rx76y57vl69ynnig325pid8jb0466298-systemd-239/lib/systemd/systemd-logind
                  │ ├─29688 agetty --login-program /nix/store/vykxgkzyajk45y0nl7cal99wymv1snxg-shadow-4.6/bin/login --noclear --keep-baud console 115200,38400,9600 vt220
                  │ ├─29689 postgres
                  │ ├─29697 postgres: checkpointer process
                  │ ├─29698 postgres: writer process
                  │ ├─29699 postgres: wal writer process
                  │ ├─29700 postgres: autovacuum launcher process
                  │ └─29701 postgres: stats collector process

My fault. It actually works:

$ getent ahosts database  STREAM database  DGRAM  RAW
fe80::3419:33ff:feb5:5548 STREAM
fe80::3419:33ff:feb5:5548 DGRAM
fe80::3419:33ff:feb5:5548 RAW

I was trying the host command, which uses dns instead of gethostbyname from libc.

Normal commands should therefor work:

 $ ping database
PING database ( 56(84) bytes of data.
64 bytes from database.containers ( icmp_seq=1 ttl=64 time=0.096 ms

Strange: without declaring an IP-adress the hostname is “container-name” and after declaring “container-name”.containers is added to /etc/hosts…