Docker RUN 不会保留文件

2023-11-22

我有一个 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步中没有被持久化吗?为什么在第 5 步中重新创建“.bash_logout”文件?为什么在第 7 步中“bar”不再出现在我的“.profile”文件中?

当然,如果我基于这个镜像启动一个容器,我的任何修改都不会被保留......所以我的 Dockerfile ......毫无用处。有什么线索吗?


这些更改没有持续存在的原因是它们位于volume詹金斯 Dockerfilemarks /var/jenkins_home/ as a VOLUME.

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

如何解决这个问题?

我认为解决这个问题的最好方法是;

  • 里面添加你要修改的文件jenkins_home在图像内的不同位置,例如/var/jenkins_home_overrides/
  • 基于或“包装”创建自定义入口点,默认入口点脚本复制你的内容jenkins_home_overrides to jenkins_home容器第一次启动时。

实际上...

就在我写下这些的时候;看起来 Jenkins 官方镜像已经开箱即用地支持这一点;https://github.com/jenkinsci/docker/blob/683b0d6ed17016ee3211f247304ef2f265102c2b/jenkins.sh#L5-L23

根据文档,您需要将文件添加到/usr/share/jenkins/ref/目录,这些将被复制到/var/jenkins/home开始时。

另请参阅https://issues.jenkins-ci.org/browse/JENKINS-24986

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Docker RUN 不会保留文件 的相关文章

随机推荐