无法从 docker 修改容器中的文件

Unable to modify files in container from docker

我正在尝试通过修改现有映像中的某些文件来构建映像。但是,文件不会被 RUN 命令更改。我的 docker 文件是

FROM vromero/activemq-artemis

ADD . .

RUN ls
RUN whoami

# Overwrite existing password file.  The existing file is invulnerable, and
# cannot be modified by docker.  I have no idea why.
RUN rm -f /var/lib/artemis/etc/artemis-users.properties
RUN ls -l /var/lib/artemis/etc
RUN mv passwords.txt /var/lib/artemis/etc/artemis-users.properties
RUN cat /var/lib/artemis/etc/artemis-users.properties
RUN touch /var/lib/artemis/etc/touch-test

# Add the predefined queues
RUN sed -i.bak '/<core/r queues.xml' /var/lib/artemis/etc/broker.xml

# EOF

基本图像来自 public docker 存储库。当我 运行 它时,我得到以下输出

$ docker build .
Sending build context to Docker daemon 4.608 kB
Step 0 : FROM vromero/activemq-artemis
 ---> 4e0f54c798cc
Step 1 : ADD . .
 ---> 3efde5a1fdea
Removing intermediate container c8621adc900b
Step 2 : RUN ls
 ---> Running in 5c5dca9449da
Dockerfile
artemis
artemis-service
passwords.txt
queues.xml
 ---> 22c541f98339
Removing intermediate container 5c5dca9449da
Step 3 : RUN whoami
 ---> Running in f11fcd2e2c5b
root
 ---> 15ee9aeb4c15
Removing intermediate container f11fcd2e2c5b
Step 4 : RUN rm -f /var/lib/artemis/etc/artemis-users.properties
 ---> Running in ab4383f0bb91
 ---> 10877bdb08ee
Removing intermediate container ab4383f0bb91
Step 5 : RUN ls -l /var/lib/artemis/etc
 ---> Running in a5669c8808e8
total 24
-rw-r--r-- 1 artemis artemis  959 Oct  4 05:40 artemis-roles.properties
-rw-r--r-- 1 artemis artemis  968 Oct  4 05:40 artemis-users.properties
-rwxrwxr-x 1 artemis artemis 1342 Oct  4 05:40 artemis.profile
-rw-r--r-- 1 artemis artemis 1302 Oct  4 05:40 bootstrap.xml
-rw-r--r-- 1 artemis artemis 4000 Oct  4 05:40 broker.xml
-rw-r--r-- 1 artemis artemis 2203 Oct  4 05:40 logging.properties
 ---> 02e3acc58653
Removing intermediate container a5669c8808e8
Step 6 : RUN mv passwords.txt /var/lib/artemis/etc/artemis-users.properties
 ---> Running in 68000aa34f6b
 ---> ec057d5adc67
Removing intermediate container 68000aa34f6b
Step 7 : RUN cat /var/lib/artemis/etc/artemis-users.properties
 ---> Running in 934a36d8c4d1
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements.  See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License.  You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
apollo=ollopaehcapa ---> ca1bad8a8903
Removing intermediate container 934a36d8c4d1
Step 8 : RUN touch /var/lib/artemis/etc/touch-test
 ---> Running in cb931c5cfcd1
 ---> 6961b4fcde75
Removing intermediate container cb931c5cfcd1
Step 9 : RUN sed -i.bak '/<core/r queues.xml' /var/lib/artemis/etc/broker.xml
 ---> Running in a829642b29ab
 ---> effd394fc02f
Removing intermediate container a829642b29ab
Successfully built effd394fc02f

ADD . . 起作用了,因为 passwords.txt 和 queues.xml 都出现在 ls 中。 whoami说明当前用户是root,所以应该没有权限问题。

但是,现有文件没有更改。如果我 运行 图像但使用 bash 作为开始命令(见下文),none 个文件有当前日期,尽管 mved 的文件替换现有文件消失了。如果我将 sed 命令粘贴到 shell,它会更新文件。

$ docker run -it effd394fc02f bash
root@51d1cc0a94cb:/var/lib/artemis/bin# ls -l
total 16
-rw-r--r-- 1 root    root     543 Oct 21 22:12 Dockerfile
-rwxrwxr-x 1 artemis artemis 3416 Oct  4 05:40 artemis
-rwxrwxr-x 1 artemis artemis 3103 Oct  4 05:40 artemis-service
-rw-r--r-- 1 root    root     329 Oct 21 22:18 queues.xml
root@51d1cc0a94cb:/var/lib/artemis/bin# cd ../etc
root@51d1cc0a94cb:/var/lib/artemis/etc# ls -l
total 24
-rw-r--r-- 1 artemis artemis  959 Oct  4 05:40 artemis-roles.properties
-rw-r--r-- 1 artemis artemis  968 Oct  4 05:40 artemis-users.properties
-rwxrwxr-x 1 artemis artemis 1342 Oct  4 05:40 artemis.profile
-rw-r--r-- 1 artemis artemis 1302 Oct  4 05:40 bootstrap.xml
-rw-r--r-- 1 artemis artemis 4000 Oct  4 05:40 broker.xml
-rw-r--r-- 1 artemis artemis 2203 Oct  4 05:40 logging.properties

为什么 run 命令没有更改这些文件?

不是一个完整的答案,但至少是一个线索:您不更改构建映像的入口点。
这意味着您的图像仍将执行来自 vromero/activemq-artemis, which, according to its Dockerfile 的图像:

ENTRYPOINT ["/docker-entrypoint.sh"]

并且 docker-entrypoint.sh 可能会重置您在 docker run 上所做的一些更改。

Dockerfile 中有两个 RUN 命令。

您正在 运行宁此:RUN <command>(命令是 运行 在 shell - /bin/sh -c (shell形式)

另一个是这样的:RUN ["executable", "param1", "param2"] (exec form)

试试这个:

RUN ["rm", "-f", "/var/lib/artemis/etc/artemis-users.properties"]
RUN ["ls", "-l", "/var/lib/artemis/etc"]
RUN ["mv", "passwords.txt", "/var/lib/artemis/etc/artemis-users.properties"]
RUN ["cat", "/var/lib/artemis/etc/artemis-users.properties"]
RUN ["touch", "/var/lib/artemis/etc/touch-test"]

# Add the predefined queues
RUN ["sed", "-i.bak", "'/<core/r queues.xml'", "/var/lib/artemis/etc/broker.xml"]

实际问题与基础镜像的构建方式有关。如果你 运行 docker history --no-trunc vromero/activemq-artemis,你会看到这些命令(以及其他命令):

<id>   6 weeks ago         /bin/sh -c #(nop) VOLUME [/var/lib/artemis/etc]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    0 B                                                                                                                           
<id>   6 weeks ago         /bin/sh -c #(nop) VOLUME [/var/lib/artemis/tmp]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    0 B                                                                                                                           
<id>   6 weeks ago         /bin/sh -c #(nop) VOLUME [/var/lib/artemis/data]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   0 B                                                                                                                           

Dockerfile volume documentation

Note: If any build steps change the data within the volume after it has been declared, those changes will be discarded.

这意味着基础映像中的配置已锁定。

我通过根据历史命令的输出创建自己的 dockerfile 解决了我的问题,没有 volume 行。