Setting the hostname of toolbox containers to their container name

When using several toolboxes it’s hard to distinguish between them. Currently toolbox does not support setting the container hostnames and there is an open issue on Github idea: include OS release in hostname · Issue #98 · containers/toolbox · GitHub. After following the thread i came up with a definitely not perfect solution but in the meantime it’s better than nothing.

toolbox() {
    if [[ $@ =~ (enter) && ! $@ =~ (-r|--release|-d|--distro) ]]; then

        CONTAINER=$(sed -E -e 's/enter|--container|-c|toolbox|\t|\n|\s//g' <<< $@)

        if [[ ! -z ${CONTAINER} && ${CONTAINER} =~ "^[a-zA-Z0-9-]{1,15}$" ]]; then
            command toolbox --container ${CONTAINER} run sudo hostname ${CONTAINER}
            command toolbox enter ${CONTAINER}
        else
            command toolbox enter
        fi
    else
        command toolbox $@
    fi
}

This function can be dropped into the bashrc or zshrc and it will just override the enter command. Suggestions to improve this snippets are welcome. :slight_smile:

3 Likes

There’s also some discussion about this in this issue that I posted: Low quality icons from applications running in Toolbox · Issue #586 · containers/toolbox · GitHub

The upshot of that is that for GUI integration it might be better to use the host hostname in the container, and use some other method of showing which container you are in in the prompt.