启动容器时加载外壳配置文件



>问题

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

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

其他方法

Dockerfile CMD

这不起作用,因为执行顺序:

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

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

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

码头工人入口点

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

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文件,我只需要确保我的配置文件已正确写入即可。

我认为您应该更新$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"]

最新更新