Container hostname

Hello,

We’re using Netdata on our Balena devices.
When Netdata cloud sends us notifications, the nodename is a random number (containerid of netdata container).
It’s hard to track notifications like this.

In the Netdata config, we set the hostname. This is used on the cloud dashboard.
But for notification, Netdata seems to use the output from the hostname command.

Is tried to set the hostname from the supervisor API.
But this only sets the device hostname. Not the container hostname.

Is there a way to set the container hostname?

Hello there,
As of docker-compose v2 one can use network aliases to set alternate hostnames that a service container is reachable at (See alias docs and examples here Compose file version 2 reference | Docker Documentation)
Let us know if this helps.

In addition, you can also set the hostname directly like:

services:
    myservice:
        hostname: myname
        // ...

Let us know if this worked

Hello @somombo,

I think the aliases only have impact on the routing not on the hostname command inside the container.

I looked into setting the hostname from docker-compose.
But I didn’t find a way to set it per device.
All devices in the fleet will have the same hostname (for that container).

Is there a way to set the hostname for a container per device?
Maybe using device env vars?

Hello thanks for clarifying. I have a better sense for what you mean now. I’ve reached out to our team and we will get back to you.

Hello folks,
Would using the hostname block help: balenaHub: an easier way to find and publish fleets, projects, and blocks for edge devices

Check this out ^

Nice approach.
But it uses the supervisor api internally: hostname/start.sh at master · balenablocks/hostname · GitHub

I already tried the supervisor api.
It sets the hostname on the device but not inside the containers.

Hello,
Thanks for the context you provided so far. From what I understand, you are looking for a solution for setting hostnames per container, per device in the fleet. Our recommended solution would be to override the entry point of your Netdata image using docker-compose and then set the hostname from there.

In order to accomplish this, I would recommend using device variables to specify hostname for a particular service for a particular device: Variables - Balena Documentation

This variable will be set in your service container at runtime. In your docker-compose, override the entry point of your netdata image that you are using something like this:

version: "2"

services:
  sut:
    image: netdata/netdata
    entrypoint:
      - "sh"
      - "-c"
      - "hostname $${SET_HOSTNAME} ; /usr/sbin/run.sh"

This should set the hostname before starting the netdata service for your device and use the SET_HOSTNAME device variable that you have set in the balenaCloud dashboard to define the hostname for your service. If SET_HOSTNAME is unset it will just print the current hostname, so no issues there.

We have a custom launch script.
I’ve added the hostname command but get an error:

hostname: sethostname: Operation not permitted

When logged into the running container as root, I get another error:

root@f9e69c0:~# balena exec -ti --user root 63aae3dcd165 /bin/bash
root@63aae3dcd165:/# whoami
root
root@63aae3dcd165:/# hostname
63aae3dcd165
root@63aae3dcd165:/# hostname test123
hostname: you must be root to change the host name

Hi,

Someone dropped me a message about this but I only just noticed it, sorry for the late reply.

Did you manage to find a solution?

I’m not familiar with Netdata, but have you tried the ideas from their docs (assuming this is the same Netdata?):

If you don’t want to destroy and recreate your container, you can edit the Agent’s netdata.conf file directly. See the above section on configuring Agent containers to find the appropriate method based on how you created the container.

What does netdata.conf look like? Sounds from these docs like that might be a way to manually override the nodename without having to manipulate the hostnames? It may be easier to create a custom entry point script to alter the config file.

As an alternative to the Docker hostname thing mentioned by Zack, you can manually override the hostname that Netdata reports in the netdata.conf file by adding a hostname key under the [global] section. This obviously doesn’t scale well if you have a lot of nodes, but if you’ve only got one or two it can be a much easier solution than trying to get Docker to give all the containers sane hostnames.

Something else that springs to mind would be to change your network mode to host instead of bridge, that way the hostname inside the container will match the hostname of the device and the other solutions you looked at could become more viable. Of course that has some networking implications, but without knowing your setup it’s hard to say what they would be.

Hello @maartenc were you able to fix this problem following the comments from my colleague above?

Check this project to explore if this can help you on what you are trying to do with netdada → GitHub - odyslam/starknet-node: 1-click deployment of a Starknet node to a Raspberry Pi, powered by balena.

Let us know if we can help you more!

Hello @maggie0002 and @mpous,

The problem was with the netdata-claim.sh script inside netdata.
The netdata run.sh docker script doesn’t supply the hostname to that script.

If no hostname is supplied, it will get it from the hostname command.
It won’t use the hostname configured in netdata.conf

The hostname is also cached.
So, just reclaiming with the correct hostname didn’t work.
I had to clear the /var/lib/netdata and /var/cache/netdata folders.

The notifications are now showing the correct nodename.

Thanks for your help!

2 Likes

Thanks for sharing @maartenc :slight_smile: