Retrieving hostname from within the container

Hello,

I’m very simply trying to grab the device hostname within my application. Unfortunately when I SSH into the device and run echo $RESIN_DEVICE_NAME_AT_INIT within the terminal no value is outputted except a new empty line.

If I run the following command to get the host config:
curl "$RESIN_SUPERVISOR_ADDRESS/v1/device/host-config?apikey=$RESIN_SUPERVISOR_API_KEY"
I’m getting:
curl: (3) <url> malformed

Also please note that I’m not looking for the containers hostname, I’m looking to retrieve the devices OR better yet if I can set the hostname of the container based on the parent, that would work as well.

Thanks in advance,

David

I got a response directly on chat support for this question. Posting here for other users benefit:

The $RESIN_DEVICE_NAME_AT_INIT is available inside the container and not inside the hostOS. So if you use the dashboard to access the console please make sure to access the application and not the hostOS when reading the variable.

2 Likes

As of RESIN_* variables migrated to BALENA_* there’s the read access from the Balena Supervisor API, and JSON data’s stored in /v1/device. I don’t get the curl: (3) <url> malformed anymore.
Enable label in docker-compose.yml

version: '2'
services:
  container-name:
    labels:
      io.balena.features.supervisor-api: 1

Export from Shell Script, run.sh :

export BALENA_SUPERVISOR_DEVICE="$BALENA_SUPERVISOR_ADDRESS/v1/device?apikey=$BALENA_SUPERVISOR_API_KEY"
printf "curl -X GET --header \"Content-Type:application/json\" %s/v1/device/host-config?apikey=%s" $BALENA_SUPERVISOR_ADDRESS $BALENA_SUPERVISOR_API_KEY
export BALENA_SUPERVISOR_DEVICE=$(curl -X GET --header "Content-Type:application/json" $BALENA_SUPERVISOR_DEVICE)

JSON’s easy to translate with python (3.6). Translate JSON data into python list, and attribute hostname, e.g. hostname-app.py :

def bln_device_fetch(attribute='network'):
    bln_device = os.getenv('BALENA_SUPERVISOR_DEVICE', None)
    if bln_device:
        data = json.loads(bln_device)
        host = str(data[attribute])
        print('Host:', host)
        return host
    else:
        return False

bln_device_fetch()['hostname']

I read the hostname from v1/device/host-config