I don’t do this anymore, as I explain a little down.

I use Toolbox a lot, like every single day. And I have a very minimal bash prompt (just $ in green and > in red for PS2). But it is quite hard to tell at a glance if I am inside a Podman container that was created by Toolbox, mostly because the tools I normally use, are there, and it’s my home directory.

I usually check /etc/fedora-release but that’s a poor check.

Well, now my prompt inside a container is different:

# Colors
C_TEXT="\033[0m"
C_PS1="\033[38;5;46m"
C_PS2="\033[38;5;208m"
C_PS1_CONTAINER="\033[38;5;208m"

# Toolbox check
if grep -q container /proc/self/cgroup; then
	export PS1="${C_PS1_CONTAINER}\h \\$ ${C_TEXT}"
else
	export PS1="${C_PS1}\\$ ${C_TEXT}"
fi
export PS2="${C_PS2}> ${C_TEXT}"

That’s good enough for now.


What I do now:

Since I made this post, I realized there were some issues with the coloring in the prompt. Instead of debugging it, I changed a few things.

For starters, I keep my prompt the same everywhere and use the hostname as a way to identify where I am. So one line instead of the whole chunk before:

export PS1="\[\e[38;5;202m\]\h \[\e[38;5;131m\]\$ \[\033[0m\]"

It is not as minimal as before, but quite there.

I also use fzf keybindings which are usually not present inside a container. Instead of using the grep -q container /proc/self/cgroup approach, I just check if they are present or not:

# FZF: dnf install fzf -y
# Source only fzf bindings if the file exists. A common scenario is inside a container.
FZF_BINDINGS="/usr/share/fzf/shell/key-bindings.bash"
if [ -f $FZF_BINDINGS ]; then
	source $FZF_BINDINGS
fi

So I guess the conclusion here is… simplifying is always better?

In any case, the original idea of detecting if you are inside a container still works:

if grep -q container /proc/self/cgroup; then
	echo "Inside a container"
else
	echo "Outside a container"
fi

Now is good enough.