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

时间:2015-10-21 22:32:31

标签: docker dockerfile

我正在尝试通过修改现有图像中的某些文件来构建映像。但是,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 . .已经有效,因为passwords.txt和queues.xml都显示在ls中。 whoami表示当前用户是root用户,因此不存在权限问题。

但是,现有文件不会更改。如果我运行图像但使用bash作为启动命令(见下文),则没有任何文件具有当前日期,尽管用mv替换现有文件的文件已经消失。如果我将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命令不会更改这些文件?

3 个答案:

答案 0 :(得分:2)

实际问题与基本图像的构建方式有关。如果您运行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

  

注意:如果任何构建步骤更改了卷之后的数据   已经宣布,这些更改将被丢弃。

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

我通过基于history命令的输出创建自己的dockerfile来解决我的问题,而没有volume行。

答案 1 :(得分:1)

不是一个完整的答案,但至少有一个线索:你不能改变建成图像的入口点。
这意味着您的图片仍将执行来自vromero/activemq-artemis的图片,根据其Dockerfile,该图片为:

ENTRYPOINT ["/docker-entrypoint.sh"]

docker-entrypoint.sh可能会重置docker run上的部分更改。

答案 2 :(得分:0)

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