Docker RUN不保留文件
我有一个Docker的问题,它不会通过“RUN”持续启动命令。
这是我的Dockerfile:
FROM jenkins:latest RUN echo "foo" > /var/jenkins_home/toto ; ls -alh /var/jenkins_home RUN ls -alh /var/jenkins_home RUN rm /var/jenkins_home/.bash_logout ; ls -alh /var/jenkins_home RUN ls -alh /var/jenkins_home RUN echo "bar" >> /var/jenkins_home/.profile ; cat /var/jenkins_home/.profile RUN cat /var/jenkins_home/.profile
这里是输出:
Sending build context to Docker daemon 373.8 kB Step 1 : FROM jenkins:latest ---> fc39417bd5fb Step 2 : RUN echo "foo" > /var/jenkins_home/toto ; ls -alh /var/jenkins_home ---> Using cache ---> c614b13d9d83 Step 3 : RUN ls -alh /var/jenkins_home ---> Using cache ---> 8a16a0c92f67 Step 4 : RUN rm /var/jenkins_home/.bash_logout ; ls -alh /var/jenkins_home ---> Using cache ---> f6ca5d5bdc64 Step 5 : RUN ls -alh /var/jenkins_home ---> Using cache ---> 3372c3275b1b Step 6 : RUN echo "bar" >> /var/jenkins_home/.profile ; cat /var/jenkins_home/.profile ---> Running in 79842be2c6e3 # ~/.profile: executed by the command interpreter for login shells. # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login # exists. # see /usr/share/doc/bash/examples/startup-files for examples. # the files are located in the bash-doc package. # the default umask is set in /etc/profile; for setting the umask # for ssh logins, install and configure the libpam-umask package. #umask 022 # if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi # set PATH so it includes user's private bin if it exists if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi bar ---> 28559b8fe041 Removing intermediate container 79842be2c6e3 Step 7 : RUN cat /var/jenkins_home/.profile ---> Running in c694e0cb5866 # ~/.profile: executed by the command interpreter for login shells. # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login # exists. # see /usr/share/doc/bash/examples/startup-files for examples. # the files are located in the bash-doc package. # the default umask is set in /etc/profile; for setting the umask # for ssh logins, install and configure the libpam-umask package. #umask 022 # if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi # set PATH so it includes user's private bin if it exists if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi ---> b7e47d65d65e Removing intermediate container c694e0cb5866 Successfully built b7e47d65d65e
你们知道为什么“富”文件没有坚持在步骤3? 为什么在步骤5重新创build“.bash_logout”文件? 为什么“bar”不在我的“.profile”文件中,在第7步?
当然,如果我根据这个图像启动一个容器,我的修改都不会被保留下来,所以我的Dockerfile是无用的。 任何线索?
这些更改没有被持久化的原因是,它们在一个卷内Jenkins Dockerfile将/var/jenkins_home/
标记为VOLUME
。
在docker build
期间,体积内的信息不会持续存在,更确切地说, 每个构build步骤都会根据映像的内容创build一个新卷,并放弃之前构build步骤中使用的卷。
如何解决这个问题?
我认为解决这个问题的最好办法是,
- 在
jenkins_home
里面添加你要修改的文件,在/var/jenkins_home_overrides/
- 创build一个基于或“包装” 的默认入口点脚本 ,在第一次启动容器时将
jenkins_home
的内容jenkins_home_overrides
到jenkins_home
的自定义入口点。
其实…
就在我写这些的时候 看起来jenkins的官方形象已经支持这个框框; https://github.com/jenkinsci/docker/blob/683b0d6ed17016ee3211f247304ef2f265102c2b/jenkins.sh#L5-L23
根据文档 ,你需要将你的文件添加到/usr/share/jenkins/ref/
目录,这些文件将在启动时被复制到/var/jenkins/home
。
只是为了testing,检查是否问题仍然存在与写入如下的RUN指令:
RUN bash -c 'echo "foo" > /var/jenkins_home/toto' RUN bash -c 'echo "bar" >> /var/jenkins_home/.profile'