shell 为什么运行Docker容器时不执行'~/.bashrc'?

wixjitnu  于 2022-11-16  发布在  Shell
关注(0)|答案(4)|浏览(268)

我有一个docker文件,如下所示。launch.sh是这个docker映像中的入口点。

FROM ubuntu:16.04
USER root

RUN apt-get update && apt-get install -y \
        curl \
        vim \
        net-tools \
        git \
        iputils-ping \
        wget

RUN apt-get install -y python
RUN apt-get update && apt-get install -y gcc g++ make libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev

RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

ENV NVM_DIR /root/.nvm
RUN . $NVM_DIR/nvm.sh && \
  nvm install 7.9.0 && npm install -g npm@5.6.0

ADD ./Docker/launch.sh /workspace/

CMD ["/bin/sh", "/workspace/launch.sh"]

launch.sh的含量为:

#!/bin/bash

cd /workspace/demo
npm install
node index.js

当我运行Docker容器时:docker run IMAGE_NAME,我得到这个错误:

npm: not found
node: not found

这个镜像中的node是由已经安装的nvm管理的,它的脚本已经设置在/root/.bashrc文件上。但是我不知道为什么它找不到nodejs命令。但是如果我用docker run -it IMAGE_NAME bash运行容器,然后手动运行workspace/launch.sh命令,一切正常。运行映像时似乎没有执行~/.bashrc。我如何让容器源代码.bashrc?
/root/.bashrc的含量为:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
p4tfgftt

p4tfgftt1#

每个命令都运行一个单独的子shell,因此环境变量不被保留,并且.bashrc不作为源(参见此答案)。
您必须在运行命令的同一进程**中手动获取脚本,**以便它可以:

CMD source /root/.bashrc && /workspace/launch.sh

只要您的launch.sh是一个可执行文件。
根据文档,您使用的exec表单不调用命令shell,因此它不能与您的.bashrc一起工作。

编辑:

BASH不是您的默认shell,因此

CMD /bin/bash -c "source /root/.bashrc && /workspace/launch.sh"

运行脚本时需要。如果要将shell默认设置为BASH,可以使用文档中描述的SHELL指令,例如:

SHELL ["/bin/bash", "-c"]
carvr3hs

carvr3hs2#

带CMD和 shell 形式

CMD /bin/bash -i "/workspace/launch.sh"

Edit还应使用ENTRYPOINT和,并使用exec形式,使用

ENTRYPOINT ["bash","-i","/workspace/entrypoint.sh"]

我认为-i标志按预期方式工作,.bashrc文件按预期方式使用,其他解决方案对我不起作用,.bashrc文件从未使用
一个解决方案可能不适合所有人,使用-i标志,程序可能会提示用户交互
ps:我使用了docker create和docker start -i“容器名称”

bfhwhh0e

bfhwhh0e3#

您可以在launch.sh中添加source /path/to/bashrc,并将CMD更改为以下内容,而不是通过CMD本身更改为bash:

CMD ["/workspace/launch.sh"]

或者,您可以在您的Dockerfile中执行以下操作,而不是依赖于bashrc

ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 7.9.0
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules #Ensure that this is the actual path
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
RUN . $NVM_DIR/nvm.sh && \
  nvm install $NODE_VERSION && npm install -g npm@5.6.0
i86rm4rw

i86rm4rw4#

现有答案均未准确回答标题问题:* 为什么运行Docker容器时不执行~/.bashrc?*
有两件事需要注意:

使用登录shell

bash man page
bash作为交互式登录shell调用,或作为带有**--login选项的非交互式shell调用时,它首先从文件 /etc/profile(如果该文件存在)读取并执行命令。阅读该文件后,它依次查找 ~/.bash_profile~/.bash_login~/.profile,并从第一个存在且可读的命令中读取和执行命令。在启动shell时可以使用--noprofile**选项来禁止此行为。
因此,为了在调用bash时自动读取.profile/.bashrc,必须使用--login-l选项调用bash
您可以通过以下几种方式来实现此目的:

**1.将shell设置为包含-l选项。**例如,

SHELL ["/bin/bash", "-l", "-c"]

2.使用RUN的exec形式调用-l以执行特定命令

CMD ["/bin/bash", "-l", "-c", "/workspace/launch.sh"]

注意.bashrc的顶部

从上面的手册页中,我们知道了概要文件的搜索和加载顺序。如果您查看/root/.profile,您可能会看到如下内容:

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n 2> /dev/null || true

这就是~/.bashrc获取bash shell的源代码的方式。因此,我们可以预期,当使用bash shell时,~/.bashrc将被获取。
但是,请仔细查看.bashrc文件顶部附近的内容:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

这意味着.bashrc的其余内容实际上被忽略,除了 interactive shell。
一个答案是使用bash-i选项来调用交互式shell。这确实有效,因为环境变量PS1是为交互式shell设置的,因此.bashrc会继续。
但是,也许您不需要交互式环境。在这种情况下,有几个选项:

**1.注解掉返回行。**您可以在Dockerfile中使用类似于以下内容的代码:

RUN sed -e '/[ -z "$PS1" ] && return/s/^/#/g' -i /root/.bashrc

.bashrc的这种修改将防止其从非交互式调用中提前退出。

**2.将nvm设置移动到.profile。**将.bashrc文件的最后三行移动到.profile,以便无条件执行它们。
**3.手动获取.bashrc的源代码。**正如其他答案已经提到的,您当然可以根据需要手动获取.bashrc的源代码,如下所示:

RUN source /root/.bashrc && /workspace/launch.sh

注意到.bashrc的大部分内容对于交互式shell来说最有意义,否则通常是不必要的,这可能使上面的选项2最有吸引力。

相关问题