启动容器时加载 shell 配置文件

Load shell configuration file when starting container

问题

我想在我使用 docker run -it container_name 进入 docker 容器时加载自定义 .zshrc,因为 .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 容器的预期方式,但它更像是一种学习体验,我想看看是否可以做到。

我认为你应该更新 $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,您可以随时在现有的中添加内容。例如

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

DOCKERFILE

您需要添加命令 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" ]

其他方法

Docker文件命令

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

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

但这应该有效(未测试):

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

Docker入口点

如果您不想将其添加到 Docker 文件中,则将其用作入口点:

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

请注意,该示例假设容器中的用户是 root,请相应地调整您的情况。

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

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

结论

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