How to show in the shell prompt the name of the container in use?

Hi all :wave:

I was checking FSB35 yesterday, I’m fairly impressed how smooth the container integration is with the host system, especially when running graphical applications :exploding_head: Kudos to the Silverblue team!

Something I didn’t find how to do tho - and admittedly haven’t deep dive into the Toolbox documentation yet - is how to get the name of the container in use when you enter a container through toolbox enter $CONTAINER.

Sometimes during the time I tested this cool feature, I’ve found myself switching from container to container and at some point losing track of which container I was standing in.

It would be incredibly handy if somehow we could get the name of that container to get it shown in the prompt like one can usually do by setting the PS1 variable or using tools like the wonderful Starship.rs

Thanks!

1 Like

I’m not pretty sure, but it seems to work.
Once inside the container, issue the hostname command:
sudo hostname mytoolbox
Exit and enter, now the container has its name.

4 Likes

Hello @bluejat,
Welcome to ask :fedora: !
When I enter a toolbx container my prompt changes to reflect that …

Screenshot from 2021-12-21 10-21-21

As you can see, it is designated as toolbox, and if I installed Fish shell in it I would get my Fish shell there too. Maybe I’m missing what you want to see at the prompt.

2 Likes

Hi guys,

Thanks for your responses so far.

@alciregi Hmm that would involve manual configuration and I was looking for something like @jakfrost mentions

@jakfrost This is exactly what I was looking for. Did you set up that integration by yourself, or somehow that works out of the box for you?
I’m unable to reproduce the behavior you show OOTB in SB35 (installed on a KVM VM since I’m getting acquainted with the OS before switching to it as my main OS):

In the above example t is a container containing CLI (terminal) tools, and brave is a container in which I installed Brave browser.

I also checked if Fish ships this integration natively, but it’s the same thing.

Well, but if you have more than one container, each one figure as “toolbox” as their hostname (when you are inside them). This is the point.

1 Like

Yeah, that was what I was wondering. I remember someone else discussing this somewhere on discussion.fp.o I think. It is something useful, in my case Guake usually seems to display the command issued and the pwd it was issued from, but the shell reflecting what container name would need to likely invoke a script at startup of the container to change the $PS1 and possibly $PS2. For instance when I have Fish installed it is like


Toolbox is using my configuration files from the host and my prompt reflects that as does terminal startup

1 Like

Huh, I see.

I was expecting that Toolbox somehow would show the name of the container in the shell prompt, to give a proper context of the new shell execution context.

However, I understand this might not be as easy to implement as intended, since toolbox enter (which I guess is basically an alias for podman attach) has no way to drag any environment variable into a running container, at least not that I know with either podman or Docker.

It seems that in the end, the solution you proposed by changing the hostname of the container may work.
Generally speaking, I’m not fond of setting the hostname for containers, as I treat them as ephemeral execution contexts. But since we are talking here about mutable containers that are intended as long-term execution contexts, giving them a proper hostname does make sense.

I wonder if there could be a way to add this functionality to Toolbox to set the hostname of a container at creation time, maybe with podman exec or even by leveraging nsenter?

I def need to take a look at Toolbox code.

Thanks!

1 Like

You should look at toolbox.conf the configuration file help. When you create a toolbox container, you can specify many options such as different distro, custom image, perhaps the option you desire is one. As well as giving them specific names which also highlights difference. They are intended to be “pet” containers you keep around for awhile instead of being as ephemeral and disposable as a typical container. FWIW, it (toolbx/toolbox) is written in Go and C AFAIK, and is not just a wrapper for podman. It used to be a script originally.

Back to your problem, it is quite easy:

nano .bash_profile

# User specific environment and startup programs
source ~/.color
export PS1="${cyan}\u${grey}@${blue}\h ${FGC} ${grey}\w ${cyan}\$ $reset"

If you want to use color variables just create a .color file in your home root and add:

## The BASH color codes:
export red="\033[1;31m"
export green="\033[1;32m"
export yellow="\033[1;33m"
export blue="\033[0;34m"
export purple="\033[1;35m"
export cyan="\033[1;36m"
export grey="\033[0;37m"
export reset="\033[m"

If you don’t use them, just remove alias do not add:

source ~/.color

In your .bash_profile.

In you toolbox just type printenv and substitute in PS1 what ever variable you would like to add. I used after the hostname (/h) ${FGC}

My shell prompt looks now like:

ilikelinux@toolbox f35 ~ $

I can be wrong. But if I have more than one toolbox, I always get the same hostname:

$ toolbox enter container1
me@toolbox f35 ~ $ 
$ toolbox enter container2
me@toolbox f35 ~ $

I think that this is what we are talking about:

3 Likes

Just if they are all f35, but you are right a specific container-name is not available.
I just took a variable who is available.

Hi everyone :wave:

Super excited here to see all your responses!

To quickly address some of your comments guys:
@jakfrost You’re correct sir, the part of the code I saw yesterday in the repo is Go, which is beautiful.
@ilikelinux I’ll take a look at it and see what I can salvage for my configs (right now I’m using Zsh + OMZ + Starship.rs, all awesome developments. I want to fall in love with Nu shell tho, time will tell).
@alciregi Once again, you were correct about setting the hostname to differentiate containers. Also, nice finding with that issue!

In the meantime, I came up with this quick hack:

C=mycontainerizedapp && toolbox create --container $C && toolbox run --container $C sudo hostname $C

This provides me with containers with their hostnames that match the container name itself and give me complete visibility over what environment I’m standing in at all times.

By running this:

C=(ansible aws gcp python)

for i in "${C[@]}";
    do
        toolbox create -c $i \
        && toolbox run -c $i sudo hostname $i \
        && toolbox run -c $i hostname \
        && echo -e "\n\n";
    done

You should end up with something like this:

image

3 Likes

On the pull-request you posted was the proposal to change the hostname while using the /run/.containerenv file.

If you add this to ~/.bash_profile as:
source /run/.containerenv

you can use the $name variable to change the hostname. No need to change PS1.

I guess in rawhide they will include this already.