66

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.

1
  • What happens if you source .bash_prompt from .bashrc (where you used to set PS1 manually, instead of from .bash_profile? (That's technically more correct; you might want to set PS1 for an interactive shell that isn't also a login shell.) Commented Nov 3, 2015 at 12:42

7 Answers 7

90

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I read all link you list and find out when run docker with env TERM="xterm-256color", my bash prompt display color again. Thank you so much.
@SolomonT Great! I have included your comment in the answer for more visibility. Can you review it?
I missed the -t flag, that one is important for this to work.
To get both color support and make tmux work, I combined both examples: docker exec -it my-container env TERM=xterm script -q -c "/bin/bash" /dev/null
@FernandoCorreia Thank you. I have included your comment in the answer for more visibility.
|
25

Adding -t is working for me:

docker exec -t vendor/bin/phpunit

5 Comments

Also worked with docker run, no -e needed.
I needed -t along with --env "TERM=xterm-256color"
This actually worked for me. Kinda surprised TBH.
simple and efficient
-t is enough to get access to the basic 16-color palette (probably good enough for most). But you also need the -e "TERM=xterm-256color" if you want to use the rest of the extended 256-color ANSI palette.
14

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

Comments

3

Adding to VonC's answer, I made this Bash function:

drun() { # start container with the specified entrypoint and colour terminal
    if [[ $# -lt 2 ]]; then
        echo "drun needs 2+ arguments: image entrypoint" >&2
        return
    fi
    docker run -ti -e "TERM=xterm-256color" "$@"
}

Comments

2

I think this is something that you'd have to implement manually. My container has Python, so here's how I print in color using a single line:

Example Docker file:

FROM django:python3
RUN python -c "print('\033[90m   HELLO_WORLD   \033[0m')"
RUN python -c "print('\033[91m   HELLO_WORLD   \033[0m')"
RUN python -c "print('\033[92m   HELLO_WORLD   \033[0m')"
RUN python -c "print('\033[93m   HELLO_WORLD   \033[0m')"
RUN python -c "print('\033[94m   HELLO_WORLD   \033[0m')"
RUN python -c "print('\033[95m   HELLO_WORLD   \033[0m')"
RUN python -c "print('\033[96m   HELLO_WORLD   \033[0m')"
RUN python -c "print('\033[97m   HELLO_WORLD   \033[0m')"
RUN python -c "print('\033[98m   HELLO_WORLD   \033[0m')"

Standard terminal:

Print console colors using terminal command in a single line

Comments

2

You need to add the following line to your Dockerfile:

RUN echo PS1="'"'\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\ \033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '"'" >> /app/.bashrc

Change the /app/.bashrc to where your .bashrc file is in the docker.

If you want ls command to have colors too add this line:

RUN echo alias ls="'"'ls --color=auto'"'" >> /app/.bashrc

Comments

2

Just adding -e "color_prompt=yes" worked for me.

docker run -it -e "color_prompt=yes" my-image:tag

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.