How to Set/Fix the TERM Environment Variable in Docker
In this mini post, I’ll show you how to manually set the TERM environment variable when using console programs with text-based UIs (top, clear, less, nano) inside docker containers.This will remove/fix the warning messages from those programs and they’ll work properly.
Here’s the problems I faced:
I create a container from a docker image and connected to it using the following commands:
To create a container from image, run:
$ docker run --name centos_container centos /bin/bash
To connect to the container, run:
$ docker exec -it centos_container /bin/bash
But, When I tried to use console programs with text-based UIs (top, clear, less, nano, more, vim), some of them simply didn’t work and other didn’t work properly. All you get is error messages complaining that the terminal does not work or the TERM environment variable is not set. They gave me the following warning messages:
- For clear and top commands:
TERM environment variable not set.
- For less command:
WARNING: terminal is not fully functional
- For nano command:
Error opening terminal: unknown.
Here’s the reasons:
The reason for these error messages is that programs like top, less or vim use information about the terminal you are using to fill the screen and recognize which keys you hit. These programs use the TERM variable to decide which strings to send for clearing the screen, for cursor movements or for displaying color. If this information is not correct, the screen may be messed up or keys may not be recognized. Simply, your container did not start with the
-t/--tty option when created.
Here’s the solutions:
We’ve two solutions for these problems a quick fix and a long-term solutions. so let’s solve these issues:
- A quick solution:
The quick solution for this problem is very easy – just set the TERM environment variable to xterm after you enter the container:
$ docker exec -it <a-docker-container> /bin/bash export TERM=xterm
Setting the TERM environment variable like this only fixes the problem for your current bash session. The next time you enter the container you’ll have to set the environment variable again.
- A long-term solution:
If you want to prevent setting the TERM environment variable every time you enter a docker container you should always EITHER use the
--tty flag when executing
docker run command OR RUN
echo "export TERM=xterm" >> ~/.bashrc just after connecting to your container and re-connect to your container. This will permanently set the TERM variable for you. Also, it’s better to use the latest version of Docker (≥1.9), Here’s the two options we’ve:
- Option 1: Start the container with the tty flag (
docker run -t image) as the following:
$ docker run -it --name centos_container centos /bin/bash
- Option 2: Add TERM variable to your container
.bashrcfile and re-connect to your container:
$ docker exec -it <a-docker-container> /bin/bash # echo "export TERM=xterm" >> ~/.bashrc
$ docker exec -it centos_container /bin/bash $ top
Here’s the output of the top command from my running container:
Hints: 1. You will still need the quick-fix from time to time, because not all containers are started with the
-t/--ttyoption works. 2. If you use version of Docker (<1.9), then you'll need a quick-fix even if the container started with
If You Appreciate What We Do Here On Mimastech, You Should Consider:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.