Docker RUN不会保留文件

时间:2016-01-14 10:49:57

标签: docker dockerfile

我遇到了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

你们知道为什么" foo"文件在第3步没有持久化?为什么" .bash_logout"文件是在第5步重新创建的?为什么" bar"不在我的" .profile"在第7步再次提交文件?

当然,如果我基于这个图像启动容器,我的修改都没有持久...所以我的Dockerfile是......没用。任何线索?

2 个答案:

答案 0 :(得分:10)

这些更改未被保留的原因是它们位于 Jenkins Dockerfile marks /var/jenkins_home/ as a VOLUME内。

docker build期间,或者更确切地说,卷内的信息不会保留;每个构建步骤都会根据图像的内容创建一个 new 卷,并丢弃上一个构建步骤中使用的卷。

如何解决这个问题?

我认为解决这个问题的最佳方法是:

  • jenkins_home内要修改的文件添加到图片内的其他位置,例如/var/jenkins_home_overrides/
  • 创建一个基于或“包装”the default entrypoint script的自定义入口点,在第一次启动容器时将jenkins_home_overrides的内容复制到jenkins_home

<强>实际上...

就在我写下来的时候;看起来詹金斯的官方形象已经开箱即用了。 https://github.com/jenkinsci/docker/blob/683b0d6ed17016ee3211f247304ef2f265102c2b/jenkins.sh#L5-L23

According to the documentation,您需要将文件添加到/usr/share/jenkins/ref/目录,并在开始时将其复制到/var/jenkins/home

另见https://issues.jenkins-ci.org/browse/JENKINS-24986

答案 1 :(得分:0)

仅用于测试,检查问题是否仍然存在于RUN指令中,如:

RUN bash -c 'echo "foo" > /var/jenkins_home/toto'
RUN bash -c 'echo "bar" >> /var/jenkins_home/.profile'