【问题标题】:Load shell configuration file when starting container启动容器时加载shell配置文件
【发布时间】:2020-05-05 08:48:48
【问题描述】:

问题

鉴于.zshrc 文件已经在容器中,我想在使用docker run -it container_name 进入docker 容器时加载自定义.zshrc。

说明

我有一个具有以下结构的 Dockerfile:

FROM archlinux:latest

# Install things...
# Install zsh & oh-my-zsh
# Retrieve custom .zshrc from a repository and place it at ~/.zshrc
# Clone extensions for oh-my-zsh

# Run zsh on container start
CMD [ "zsh" ]

这一切都有效。如果我进入容器,我可以看到我的自定义 .zshrc 文件在它应该在的位置,如果我运行 source ~/.zshrc,它会被加载并且所有扩展都可以工作。

尝试

我尝试在CMD 步骤中直接获取配置文件,但找不到指定的文件。更新后的行如下所示: CMD [ "zsh && source ~/.zshrc" ]

我知道这可能不是使用 docker 容器的预期方式,但它更像是一种学习体验,我想看看是否可以做到。

【问题讨论】:

  • 您可以映射您的目录。 docs.docker.com/storage/volumes
  • 我真的不明白它是如何工作的。这是否意味着我必须在我的主机上本地拥有该文件?我想知道是否可以在 docker 容器中使用已经克隆的文件。
  • @alexHexan : 我对docker的了解不够,但是你能不能在docker配置中设置一个环境变量,然后在zsh启动之前建立?如果您设置变量ZDOTDIR,它必须命名一个目录,然后.zshrc 来自该目录而不是$HOME。

标签: docker dockerfile zsh oh-my-zsh zshrc


【解决方案1】:

码头文件

您需要添加命令 chsh -s /path/to/shell 以便将 ZSH shell 添加为容器中用户的默认值:

FROM archlinux:latest

# Install things...
# Install zsh & oh-my-zsh
# Retrieve custom .zshrc from a repository and place it at ~/.zshrc
# Clone extensions for oh-my-zsh

# Make ZSH the default shell for the current user in the container
# To check that the shell was indeed added: `chsh -l` and you should see it in the  list.
RUN chsh -s ~/.zshrc

# Run zsh on container start
CMD [ "zsh" ]

其他方法

Dockerfile CMD

这个不行,因为执行顺序:

CMD [ "zsh && source ~/.zshrc" ]

但这应该可以工作(未经测试):

# using `root` user, adjust as needed for your case
CMD [ "source /root/.zshrc", "zsh"]

Docker 入口点

如果您不想将其添加到 Dockerfile 中,请将其用作入口点:

docker run --rm -it --entrypoint "chsh -s /root/.zshrc" image-name

请注意,该示例假设容器中的用户为root,请根据您的情况进行相应调整。

【讨论】:

    【解决方案2】:

    我认为你应该更新$HOME/.profile 的内容。这是一个例子。

    .profile:

    source .zshrc
    

    .zshrc:

    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    echo "I'm getting executed!!!!"
    

    Dockerfile:

    FROM ubuntu:18.04
    
    RUN apt update \
        && apt install -yyq zsh curl git
    
    RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    
    COPY .profile /root/.profile
    COPY .zshrc /root/.zshrc
    
    CMD ["zsh"]
    

    那你可以试试

    docker build -t zsh:latest .
    docker run --rm -it zsh:latest
    

    编辑:

    如果您不想复制一个新的.profile,您可以随时在现有的.profile 中添加内容。例如

    FROM ubuntu:18.04
    
    RUN apt update \
        && apt install -yyq zsh curl git
    
    RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    
    RUN curl -fsSL https://www/.zshrc -o /root/.yet_another_zshrc_file \
        && echo 'source /root/.yet_another_zshrc_file' | tee -a /root/.profile
    
    CMD ["zsh"]
    

    【讨论】:

    • 这确实有效。在旁注中,我想知道我是否可以使用容器中已经存在的文件来做到这一点。是否可以卷曲 .profile 和 .zshrc 文件而不是将它们复制到容器中?
    • 确定这是可能的。我已经更新了答案以提供示例
    • 不过,@Exadra37 方法更简洁
    【解决方案3】:

    问题并不完全在于Dockerfile。是的,正如@Exadra37 提到的CMD [ "zsh && source ~/.zshrc" ] 由于执行顺序而不起作用。但是,问题出在我的 .zshrc 配置上。

    在我自己的机器上再次测试后,我意识到它也没有自动加载。

    结论

    CMD [ "/bin/zsh" ] 作为我 Dockerfile 中的最后一行自动加载位于 /root/.zshrc 的 .zshrc 文件,我只需要确保我的配置文件写入正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      • 1970-01-01
      相关资源
      最近更新 更多