Docker bash提示符不显示颜色输出

时间:2015-11-03 07:19:33

标签: linux bash shell docker

我使用命令:docker run --rm -it govim bash -l来运行docker图像但是 它不显示颜色输出。如果我source ~/.bash_profile或再次运行bash -l,则输出将正确输出颜色。

Bash Prompt Image

My bash_profile and bash_prompt

5 个答案:

答案 0 :(得分:27)

OP SolomonT报告docker runenv有效:

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

Fernando Correia添加了in the comments

  

为了获得颜色支持并使tmux工作,我结合了两个例子:

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

正如chepner评论(之前的回答),.bash_profile来源(它是一个交互式shell),因为bash_promptcalled by .bash_profile

但是docker issue 9299说明TERM似乎没有立即设置,迫使用户打开另一个bash:

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

您与issue 8755有类似的颜色问题。

illustrate/reproduce the problem

docker exec -ti $CONTAINER_NAME tty
not a tty

current workaround是:

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

两者都假设您首先有一个正在运行的容器,这可能不方便。

答案 1 :(得分:3)

添加到VonC' answer,我做了这个bash功能:

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" "$@"
}

答案 2 :(得分:3)

我认为这是您必须手动实施的。我的容器有python,所以这里是我用一行打印的颜色:

示例泊坞文件:

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')"

标准终端:

print console colors using terminal command in single line

答案 3 :(得分:2)

根据@ VonC的回答,我将以下内容添加到我的Dockerfile中(允许我在每次都没有在cli上键入环境变量的情况下运行容器):

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

确定它适用于:

docker run -it my-image:tag

tmux使用颜色,我需要~/.tmux.conf

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

以及utf8中的tmux支持,~/.bashrc

alias tmux='tmux -u'

我的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"]

Dockerfile根据Fedora 26构建一个图像,更新它,安装一些软件包(git,vim,golang和tmux),为vim安装病毒源插件,然后从这里安装一个git repo vim-go-ide最后对一些配置文件进行一些调整以获得颜色和utf8正常工作。只需添加持久存储,可能安装在/ app / go下。

如果您的图像已经安装了所有开发工具,只需使用Dockerfile语句生成ENV并添加命令以在RUN语句中修改配置文件而不使用安装命令并在FROM语句中使用基本映像。我更喜欢这个解决方案,因为我很懒,而且(除了初始设置)它还可以在您想要运行图像时保存输入。

Using vim and plugins within tmux

答案 4 :(得分:0)

添加-t对我有用:

SELECT DISTINCT TCID
               ,START_TIME
               ,RESULT
               ,cast(START_TIME as date) as m_date
               ,max(cast(START_TIME as time)) OVER(PARTITION BY TCID) as max_time
FROM jenkins_result.JENKINS_RESULT
WHERE TCID='A330506'
ORDER BY TCID
        ,m_date;