Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Bash prompt does not display color output

I use the command docker run --rm -it govim bash -l to run Docker images, but it does not display color output.

If I source ~/.bash_profile or run bash -l again, output will then correctly be output with color.

Bash Prompt Image

My bash_profile and bash_prompt files.

like image 971
SolomonT Avatar asked Nov 03 '15 07:11

SolomonT


People also ask

How do I view the output of a docker container?

docker logs <container id> will show you all the output of the container run. If you're running it on ECS, you'll probably need to set DOCKER_HOST=tcp://ip:port for the host that ran the container. My container is already stopped. Using the cmd line doing, docker run -d image, it returns me the container id.

Does Docker run override CMD?

An essential feature of a CMD command is its ability to be overridden. This allows users to execute commands through the CLI to override CMD instructions within a Dockerfile. A Docker CMD instruction can be written in both Shell and Exec forms as: Exec form: CMD [“executable”, “parameter1”, “parameter2”]

What is TTY mode Docker?

The -t (or --tty) flag tells Docker to allocate a virtual terminal session within the container. This is commonly used with the -i (or --interactive) option, which keeps STDIN open even if running in detached mode (more about that later).

How do I run a docker container in CMD?

How to Use the docker run Command. The basic syntax for the command is: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] You can run containers from locally stored Docker images.


2 Answers

The OP SolomonT reports that docker run with env do work:

docker run --rm -it -e "TERM=xterm-256color" govim bash -l 

And Fernando Correia adds in the comments:

To get both color support and make tmux work, I combined both examples:

docker exec -it my-container env TERM=xterm-256color script -q -c "/bin/bash" /dev/null 

As chepner commented (earlier answer), .bash_profile is sourced (itis an interactive shell), since bash_prompt is called by .bash_profile.

But docker issue 9299 illustrates that TERM doesn't seem to be set right away, forcing the users to open another bash with:

docker exec -ti test env TERM=xterm-256color bash -l 

You have similar color issues with issue 8755.

To illustrate/reproduce the problem:

docker exec -ti $CONTAINER_NAME tty not a tty 

The current workaround is :

docker exec -ti `your_container_id` script -q -c "/bin/bash" /dev/null 

Both are supposing you have a running container first, which might not be convenient here.

like image 93
VonC Avatar answered Sep 18 '22 12:09

VonC


Based on VonC's answer I adding the following to my Dockerfile (which allows me to run the container without typing the environment variables on the command line every time):

ENV TERM xterm-256color #... more stuff CMD ["bash", "-l"] 

And sure enough it works with:

docker run -it my-image:tag 

For tmux to work with color, in my ~/.tmux.conf I need:

set -g default-terminal "screen-256color" 

and for UTF-8 support in tmux, in my ~/.bashrc:

alias tmux='tmux -u' 

My Dockerfile:

FROM fedora:26 ENV TERM xterm-256color RUN dnf upgrade -y && \     dnf install golang tmux git vim -y && \     mkdir -p /app/go/{bin,pkg,src} && \     echo 'export GOPATH=/app/go' >> $HOME/.bashrc && \     echo 'export PATH=$PATH:$GOPATH/bin' >> $HOME/.bashrc && \     mkdir -p ~/.vim/autoload ~/.vim/bundle && \     curl -LSso ~/.vim/autoload/pathogen.vim \         https://tpo.pe/pathogen.vim && \     git clone https://github.com/farazdagi/vim-go-ide.git \         ~/.vim_go_runtime && \     bash ~/.vim_go_runtime/bin/install && \     echo "alias govim='vim -u ~/.vimrc.go'" >> ~/.bashrc && \     echo "alias tmux='tmux -u'" >> ~/.bashrc && \     echo 'set -g default-terminal "screen-256color"' >> ~/.tmux.conf  CMD ["bash", "-l"] 

The Dockerfile builds an image based off Fedora 26, updates it, installs a few packages (Git, Vim, golang and tmux), installs the pathogen plugin for Vim, then it installs a Git repository from here vim-go-ide and finally does a few tweaks to a few configuration files to get color and UTF-8 working fine. You just need to add persistent storage, probably mounted under /app/go.

If you have an image with all the development tools already installed, just make a Dockerfile with ENV statement and add the commands to modify the configuration files in a RUN statement without the installation commands and use your base image in the FROM statement. I prefer this solution because I'm lazy and (besides the initial setup) it saves typing when you want to run the image.

Using Vim and plugins within tmux

like image 30
Edge-Case Avatar answered Sep 18 '22 12:09

Edge-Case