Sometimes* when you are entering a docker container with docker exec -ti <container-name> bash the basic bash commands like top, less, more, clear or vim don’t work.
All you get is error messages complaining that the terminal does not work or the TERM environment variable is not set:
clear, top
TERM environment variable not set.
nano
Error opening terminal: unknown.
Or the programs muchstart with a warning, but don’t work properly (e.g. less):
less
WARNING: terminal is not fully functional
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. If this information is not correct, the screen may be messed up or keys may not be recognized.
A quick fix
The quick solution for this problem is very easy – just set the TERM environment variable to xterm after you enter the container:
docker exec -ti <a-docker-container> 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 use the -t or --tty flag when executing docker run. This will permanently set the TERM variable for you.
- Start the container with the tty flag (
docker run -t image) - Use the latest version of Docker (≥1.9)
If you follow these two rules you won’t have to set the TERM environment varibale yourself.
But you will still need the quick-fix from time to time, because not all containers are started with the tty option. Especially noIt seemed to me t if you used a tool like docker-compose to start the containers.
An explanation
Some background on the TERM environment variable and why this problem does not always ocurr.