Docker Bash prompt does not display color output

Solution 1:

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.

Solution 2:

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

Solution 3:

Adding -t is working for me:

docker exec -t vendor/bin/phpunit