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

我正在尝试通过修改现有图像中的某些文件来构build图像。 但是,这些文件不会被RUN命令改变。 我的dockerfile是

 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 

基础镜像来自公共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 . . ADD . . 已经工作了,因为passwords.txt和queues.xml都出现在lswhoami显示当前用户是root用户,所以应该没有权限问题。

但是,现有文件不会更改。 如果我运行该映像,但使用bash作为启动命令(见下文),则没有任何文件具有当前date,尽pipe用于replace现有文件的文件已经不存在了。 如果我将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命令改变?

不是一个完整的答案,但至less有一个线索:你不改变构build图像的入口点。
这意味着你的图像仍然会执行从vromero/activemq-artemis ,根据其Dockerfile是:

 ENTRYPOINT ["/docker-entrypoint.sh"] 

docker-entrypoint.sh可能会重置docker-entrypoint.sh一些更改。

实际的问题与如何构build基础映像有关。 如果你运行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文档状态

注意:如果任何构build步骤在声明之后更改了卷中的数据,则这些更改将被丢弃。

这意味着基本映像中的configuration被locking。

我解决了我的问题,通过创build我自己的dockerfile基于历史命令的输出,没有volume线。

在Dockerfile中有两个RUN命令。

你正在运行这个: RUN <command> (该命令运行在shell – /bin/sh -cshellforms

另一个是这样的: RUN ["executable", "param1", "param2"]execforms

尝试这个:

 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"]