Docker bash prompt does not display color output

2020-02-17 07:02发布

I use 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

4条回答
相关推荐>>
2楼-- · 2020-02-17 07:19

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 cli 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 utf8 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 repo from here vim-go-ide and finally does a few tweaks to a few config files to get color and utf8 working fine. 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 config 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

查看更多
淡お忘
3楼-- · 2020-02-17 07:26

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" "$@"
}
查看更多
Lonely孤独者°
4楼-- · 2020-02-17 07:28

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.

查看更多
Lonely孤独者°
5楼-- · 2020-02-17 07:30

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 single line

查看更多
登录 后发表回答