DEV Community

Cover image for Add Docker container name to shell prompt
Max Ivanov
Max Ivanov

Posted on • Edited on • Originally published at maxivanov.io

Add Docker container name to shell prompt

If you run multiple Docker container with interactive shells (sh, bash, zsh) in them, it always take a moment to distinguish one terminal window from the other.

It may help if you could see the docker container name right in the shell prompt.
Below, cognito-jwt-verifier and azure-functions-apim-aad-auth are the names of 2 containers I'm running at the moment:

Show Docker container name in shell prompt

Here's how to do it for popular shells, via instructions in Dockerfile or alternatively by passing arguments to the docker run command.

Prerequisites

You will only need Docker installed on your host machine to follow along.

I will be using the tiny alpine Linux as the base image in dockerfiles below.

Formatting prompt: PS1 environment variable

To show the container name in the prompt, it is added to the PS1 environment variable which is recognized by the shell to format the prompt.

Let's check an example format I'll be using below:

PS1="🐳 \e[0;34m$DOCKER_CONTAINER_NAME\e[0m \w # "

Format deconstructed:

🐳 - add a bit of touch with the whale emoji
\e[0;34m - start coloring. 34 is the color code for blue
$DOCKER_CONTAINER_NAME - container name
\e[0m - reset color
\w # - add current path (\w) and # as a separator

Passing Docker container name

There's no way to obtain the container's name from within the container (at least I couldn't find any) so it has to be passed from the host machine when the container is started.

You can pass the entire PS1 variable as an argument to the docker run ... but that may look ugly.
Another way is to set the PS1 variable in the Dockerfile and only pass the container name as an environment variable when the container is started.

Pass PS1 with docker run

In its simplest form, starting a container with prompt modified looks like this:

docker run -it --rm -e PS1="🐳 \e[0;34mMY_CONTAINER_NAME\e[0m \w # " --name MY_CONTAINER_NAME alpine sh
Enter fullscreen mode Exit fullscreen mode

Pass PS1 environment variable with docker run

Command breakdown:

docker run - start a new container from existing image
-it - run container in interactive mode so that we can use the shell
--rm - remove the container after exit
-e PS1="🐳 \e[0;34mMY_CONTAINER_NAME\e[0m \w # " - pass the PS1 environment variable
--name MY_CONTAINER_NAME - container name which you can see with docker ps
alpine - name of the Docker image to start the container from
sh - command to run in the container

Note: zsh uses slightly different syntax, which we will see below.

Configure PS1 in Dockerfile

Instructions will be a bit different per shell used.

sh

FROM alpine:latest

RUN echo 'export PS1="🐳 \e[0;34m$DOCKER_CONTAINER_NAME\e[0m \w # "' > ~/.profile

CMD [ "sh", "-l" ]
Enter fullscreen mode Exit fullscreen mode

Usage:

docker build -t docker-name-in-sh - < Dockerfile-sh
docker run -it --rm -e DOCKER_CONTAINER_NAME=MY_CONTAINER_NAME --name MY_CONTAINER_NAME docker-name-in-sh
Enter fullscreen mode Exit fullscreen mode

sh shell with docker

bash

FROM alpine:latest

RUN apk add --no-cache bash
RUN echo 'export PS1="🐳 \e[0;34m$DOCKER_CONTAINER_NAME\e[0m \w # "' > ~/.profile

CMD [ "bash", "-l"]
Enter fullscreen mode Exit fullscreen mode

Usage:

docker build -t docker-name-in-bash - < Dockerfile-bash
docker run -it --rm -e DOCKER_CONTAINER_NAME=MY_CONTAINER_NAME --name MY_CONTAINER_NAME docker-name-in-bash
Enter fullscreen mode Exit fullscreen mode

bash shell with docker

zsh

FROM alpine:latest

RUN apk add --no-cache zsh
RUN echo 'export PS1="🐳 %F{blue}$DOCKER_CONTAINER_NAME%f %~ # "' > ~/.zshrc

CMD [ "zsh"]
Enter fullscreen mode Exit fullscreen mode

Usage:

docker build -t docker-name-in-zsh - < Dockerfile-zsh
docker run -it --rm -e DOCKER_CONTAINER_NAME=MY_CONTAINER_NAME --name MY_CONTAINER_NAME docker-name-in-zsh
Enter fullscreen mode Exit fullscreen mode

zsh shell with docker

If you want to level up your zsh shell experience, consider installing oh-my-zsh and try one of the many available themes.

Personally I like the powerlevel10k theme. It's that theme that is used on the very first screenshot above.

...

I think little enhancements to the process like this can make our day-to-day work more enjoyable.

One of the great things about Docker is that containers are disposable so it's very easy to experiment.

I encourage you to challenge your setup from time to time and find ways to improve it. Then please share it with the community!

Top comments (0)