1. Introduction
Configuring a Docker container to display colored Bash prompts improves the terminal’s aesthetics, no doubt. But more than that, colored output can be functional, as it makes it easier to tell the prompt apart from commands.
In this tutorial, we’ll explore how to display colored output in Bash inside a Docker container using the PS1 variable.
2. Displaying Colored Bash Prompt Inside a Docker Container
By default, the shell prompt in Docker containers have white/colorless fonts. But then, the commands also have fonts in the same color. So, when outputs are lengthy, the prompts and the outputs typically blend in so much that we may have to strain a bit to tell them apart. However, with ANSI color codes, we can customize the colors of shell prompts, making them distinct from outputs.
When trying to colorize a container’s shell prompt, one of the first things to consider is the terminal’s compatibility with ANSI escape codes. Luckily, most modern terminals support ANSI color codes. So, choosing a terminal when configuring a container with colored Bash prompt is rarely an issue. Some of the terminals we use when configuring with ANSI code include xterm, kitty, alacritty, amongst others.
After choosing a suitable terminal, we’ll write a PS1 code to customize the prompt to our desired colors. PS1 is a variable that stands for Prompt String 1. It defines the appearance of command prompts, including the color and text of the prompt.
So, how do PS1, ANSI code, and the terminal type help us make our command prompt colored? Well, we specify the color we want for our prompt using ANSI color codes passed to the PS1 variable. Then, we need a terminal that supports ANSI escape codes to translate the PS1 value accordingly.
2.1. Using the Command Line
When configuring a Docker container to have a colored command prompt, we can specify the terminal in the command line using the -e option:
$ docker run -it -e "TERM=xterm-256color" ubuntu:latest
The command above creates and runs a container with an interactive terminal from the ubuntu:latest image. Then, using the environment variable option -e, it defines the terminal environment variable, TERM as xterm-256color.
We could have used xterm as our terminal instead of xterm-256color. But as the name may indicate, xterm-256color supports 256 colors while xterm typically supports only 16 colors.
Once in the container’s terminal, we can customize the command prompt by typing our PS1 variable and hitting Enter:
$ PS1='\e[92m\u\e[0m@\e[94m\h\e[0m:\e[35m\w\e[0m# '
When we pass the PS1 variable above, we create a Bash prompt with a bright green username, white @ symbol, bright blue hostname, and white : symbol. Then, the current working directory takes on a magenta color while the # symbol is white:
We can dissect the value passed to the PS1 variable into these components:
- \e[92m is the ANSI code for bright green
- \u is a Bash prompt escape sequence for username
- \e[0m resets ANSI formatting
- \e94m is the ANSI color code bright blue
- \h is an escape sequence for hostname
- \e[35m is ANSI for magenta
- \w is a Bash prompt escape character for current working directory
- @, :, and # remain unformatted in the value because /e[0m comes before them
When we pass the PS1 variable directly from the command line, the settings only last for the current session. Once we close the session, PS1 goes back to default. However, we can make the PS1 value permanent by adding the code to the container’s .bashrc file.
2.2. Using a Dockerfile
Instead of having to run the commands in the previous section each time we create a container, we can create a Dockerfile. The Dockerfile creates an image whose command prompt is already colored as desired:
FROM ubuntu:latest
ENV TERM=xterm-256color
RUN echo "PS1='\e[92m\u\e[0m@\e[94m\h\e[0m:\e[35m\w\e[0m# '" >> /root/.bashrc
So, every time we create a container from the ensuing image, the Bash prompt would come colored.
3. Displaying Colored Command Outputs Inside a Docker Container
We can display colored command outputs in a Docker container using ANSI codes as we did with the Bash prompt:
$ echo -e "\e[92mHello \e[94mBaeldung"
The command above will echo “Hello Baeldung” with Hello in bright green and Baeldung in bright blue:
Colored command outputs can come in handy when we need a clear distinction between error messages and success messages. In this script, we create an if-else condition that echoes outputs in different colors, depending on the outcome of the condition:
$ cat script.sh
#!/bin/bash
if [ $1 == 'success' ]
then
echo -e "\e[92msuccess!"
else
echo -e "\e[31mfailed!"
fi
We get a bright green output when the condition is true:
But when it’s false, the output is red:
4. Conclusion
In this article, we learned how to make Bash prompts colored in a Docker container using the command line and using a Dockerfile. We also highlighted the relevance of the PS1 variable and ANSI sequences in configuring colored command prompts. Then, we went over colored command outputs.