【问题标题】:Docker run command is not applied to the imageDocker 运行命令未应用于映像
【发布时间】:2018-05-04 16:54:12
【问题描述】:

我有一个 Dockerfile 如下:

FROM jenkins/jenkins:2.119

USER jenkins
ENV HOME /var/jenkins_home

COPY --chown=jenkins:jenkins ssh ${HOME}/.ssh/

RUN chmod 700 ${HOME}/.ssh && \
    chmod 600 ${HOME}/.ssh/*

ssh 目录在构建机器的 dir/file 上有 755/644。但是,当我使用

docker build -t my/temp .

并使用 ls 命令启动映像

docker run -it --rm my/temp ls -la /var/jenkins_home/.ssh

chmod 命令都不应用于图像

drwxr-xr-x 2 jenkins jenkins 4096 May  3 12:46 .
drwxr-xr-x 4 jenkins jenkins 4096 May  3 12:46 ..
-rw-r--r-- 1 jenkins jenkins  391 May  3 11:42 known_hosts

在构建过程中我看到

Step 4/6 : COPY --chown=jenkins:jenkins ssh ${HOME}/.ssh/
 ---> 58e0d8242fac
Step 5/6 : RUN chmod 700 ${HOME}/.ssh &&     chmod 600 ${HOME}/.ssh/*
 ---> Running in 0c805d4d4252
Removing intermediate container 0c805d4d4252
 ---> bbfc828ace79

看起来 chmod 已被丢弃。我怎样才能阻止这种情况发生?

我在 Mac OSX 上使用最新的 Docker (Edge) 版本 18.05.0-ce-rc1-mac63 (24246);边缘 3b5a9a44cd

编辑

使用 --rm 也不起作用(在删除图像并重建之后)但没有收到删除消息

docker build -t my/temp --rm=false .

run -it --rm my/temp ls -la /var/jenkins_home/.ssh
drwxr-xr-x 2 jenkins jenkins 4096 May  3 15:42 .
drwxr-xr-x 4 jenkins jenkins 4096 May  3 15:42 ..
-rw-r--r-- 1 jenkins jenkins  391 May  3 11:42 known_hosts

编辑 2

所以基本上是 Docker 中的一个错误,其中带有 VOLUME 的基本映像导致 chmod 失败,并且类似地在卷上运行 mkdir 失败,但 COPY 失败了,但留下了错误权限的目录。感谢bkconrad

编辑 3

在这里创建了带有修复的分支https://github.com/systematicmethods/jenkins-docker build.sh 会在本地构建镜像

【问题讨论】:

    标签: docker dockerfile


    【解决方案1】:

    这与 Docker 如何处理 VOLUMEs 的图像有关。

    来自docker inspect my/temp

    "Volumes": {
      "/var/jenkins_home": {}
    },
    

    moby 项目有一张关于此的有用票:

    https://github.com/moby/moby/issues/12779

    基本上,您需要在运行时执行chmod

    将您的 HOME envvar 设置为像 /tmp 这样的非卷路径会显示预期的行为:

    $ docker run -it --rm my/temp ls -la /tmp/.ssh
    total 8
    drwx------ 2 jenkins jenkins 4096 May  3 17:31 .
    drwxrwxrwt 6 root    root    4096 May  3 17:31 ..
    -rw------- 1 jenkins jenkins    0 May  3 17:24 dummy
    

    【讨论】:

    • 该链接非常有帮助,还解释了为什么在同一卷上运行命令不起作用。基本上是 Docker imho 中的一个错误。花了一天中最好的时间来寻找解决方法,因为我认为我一定做错了什么,因为我对 Docker 还比较陌生。
    【解决方案2】:
    Step 5/6 : RUN chmod 700 ${HOME}/.ssh &&     chmod 600 ${HOME}/.ssh/*
     ---> Running in 0c805d4d4252
    
    Removing intermediate container 0c805d4d4252
    

    如您所见,“中间容器”正在被删除,这是 docker 保留的正常行为,如果您想将这些使用保留在命令之下。

    docker build -t my/temp --rm=false .
    

    其中一篇文章也有解释

    Why docker build image from docker file will create container when build exit incorrectly?

    【讨论】:

    • 删除图片后试过了,不行,但是没有收到删除信息
    • 更改 ssh 目录文件的权限有帮助,但不会更改 .ssh 目录的权限,COPY 语句有问题,不保留权限但更奇怪的是不允许您也可以更改权限。
    猜你喜欢
    • 2017-10-18
    • 2023-03-26
    • 2019-07-01
    • 1970-01-01
    • 2021-03-13
    • 2014-08-15
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    相关资源
    最近更新 更多