/var/jenkins_-6ren">
gpt4 book ai didi

Docker RUN 不会保留文件

转载 作者:行者123 更新时间:2023-12-03 01:47:04 25 4
gpt4 key购买 nike

我的 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 ......毫无用处。有什么线索吗?

最佳答案

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

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

如何解决这个问题?

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

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

实际上...

就在我写下这些的时候;看起来 Jenkins 官方镜像已经开箱即用地支持这一点; 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

关于Docker RUN 不会保留文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34787667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com