"unable to resolve host <service host name>" when network_mode: host

Hi,

Whenever I issue a “sudo” command in the shell of my docker service running on a raspberry PI 3 (balenaOS 2.29.2+rev1) I get the following message:

sudo: unable to resolve host f0bc010 .

Note that besides the above message the sudo seems to work fine.

Here below a complete example:

node-red@f0bc010:~$ sudo hcitool con
sudo: unable to resolve host f0bc010
Connections:
node-red@f0bc010:~$ 

So it seems that my service is not able to resolve its own hostname.
Note that my service nodered is configured in docker-compose.yml (see below) with network_mode : host.

So what should I do so that my docker service is able to resolve its own hostname when network_mode is set to host ?

FYI my build files are published in my github repository balena-node-red-eq3. I have copied the respective files from this repository here below.

my docker-compose.yml:

volumes:
  node-red-eq3-data: {}
services:
  nodered:
    build: node-red-eq3
    privileged: true
    restart: always
    network_mode: host
    ports:
       - "1880:1880"
    volumes:
      - 'node-red-eq3-data:/data'
    labels:
 #     io.balena.features.kernel-modules: '1'
 #     io.balena.features.firmware: '1'
 #     io.balena.features.dbus: '1'
 #     io.balena.features.supervisor-api: '1'
      io.balena.features.balena-api: '1'
 #     io.balena.update.strategy: download-then-kill
 #    io.balena.update.handover-timeout: ''
  mqtt:
    image: panuwitp/mosquitto-arm
    ports:
      - "1883:1883"
  nginx:
    build : nginx
    network_mode: host
    depends_on:
      - nodered
    ports:
       - "80:80"
       - "443:443"
    restart: always

and my nodered service dockerfile:


# installing an editor
USER root
RUN apt-get update && apt-get install nano bluetooth bluez libbluetooth-dev libudev-dev

# following command should assure that user node-red can use sudo without requiring to enter a password.
RUN echo "node-red ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

# following command is needed to run node js with root priviliges
# see https://github.com/noble/noble#running-without-rootsudo
RUN sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)

USER node-red

RUN npm install node-red-contrib-resinio
RUN npm install node-red-dashboard
RUN npm install node-red-contrib-credentials
RUN npm install btsimonh/node-red-contrib-eq3-bluetooth#btsimonh --save

# Take care that the following command is only effective the very first time the application is deployed
# on the device.  Next deployments won't overwrite  these files as /data is mounted volumne.
COPY settings.js flows.json flows_cred.json package.json /data/

I’ve encountered this error more than twice, on several different projects. The way to get around this error (I’m not sure if you would consider it a permanent fix or not) is to add this line to your /etc/hosts file:

127.0.0.1 $HOSTNAME

The $HOSTNAME variable will resolve to whatever the hostname is of your container (i.e. f0bc010).

In my modified balena-pihole image, I’m able to do this via the following in my docker-compose.yml:

version: '2.1'

volumes:
  pihole:
  dnsmasq:

services:
  pihole:
    image: pihole/pihole:4.2.2-1_armhf
    ports:
      - '80:80/tcp'
      - '80:80/udp'
    volumes:
      - 'pihole:/etc/pihole'
      - 'dnsmasq:/etc/dnsmasq.d'
    entrypoint: ['/bin/bash', '-c', 'echo "127.0.0.1 $HOSTNAME" >> /etc/hosts; echo "bind-interfaces" >> /etc/dnsmasq.conf; /s6-init']
    network_mode: host

EDIT:

Keep in mind, you cannot modify the /etc/hosts file in your Dockerfile; it must be changed either from you docker-compose.yml file, or within the already-running container.

Thanks @wwalker that is indeed a good work around. Thanks a lot for sharing this.

I hope balena team is coming up with a more permanent solution.
Jan.

1 Like

I do have the same issue but entrypoint workaround won’t work:/

Here is the relevant service from my docker-compose.yml file.

MagicMirror:
    build: ./MagicMirror
    privileged: true
    network_mode: host
    entrypoint: ["/bin/sh", "-c", 'echo "127.0.0.1 $HOSTNAME" >> /etc/hosts; cat /etc/hosts ; ./entrypoint.sh']
    depends_on:
      - PiHole

MagicMirror is based on FROM --platform=linux/arm karsten13/magicmirror:latest

cat /etc/hosts shows no change whatsoever.

Any advice?

Edit
I just fixed it by utilizing tee
entrypoint: ["/bin/sh", "-c", 'echo "127.0.0.1 $HOSTNAME" | sudo tee -a /etc/hosts; ./entrypoint.sh']