Docker容器保留数据

huangapple go评论79阅读模式
英文:

Docker containers retaining data

问题

我正在学习Docker,并且一直在为一个Ubuntu容器编写Dockerfile。

我的问题是,我在不同的容器之间保留了持久的信息。我已经退出、删除了容器,然后删除了它的镜像。在对Dockerfile进行更改后,我执行了docker build -t playbuntu .执行以下Dockerfile:

FROM ubuntu:latest

## 为了使apt非交互式
ENV DEBIAN_FRONTEND noninteractive
ENV DEBCONF_NONINTERACTIVE_SEEN true

## 预设tzdata,更新软件包索引,升级软件包并安装所需软件
RUN echo "tzdata tzdata/Areas select Europe" > /tmp/preseed.txt; \
    echo "tzdata tzdata/Zones/Europe select London" >> /tmp/preseed.txt; \
    debconf-set-selections /tmp/preseed.txt && \
    apt-get update && \
    apt-get install -y tzdata

RUN apt-get update -y && apt-get upgrade -y && apt-get install tree nano vim -y && apt-get install less -y && apt-get install lamp-server^ -y

RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

EXPOSE 80

WORKDIR /var/www

COPY ./index.php /var/www
COPY ./000-default.conf /etc/apache2/sites-available

CMD ["apache2ctl", "start", "&&", "apache2ctl", "restart"]

一旦我执行winpty docker run -it -p 80:80 playbuntu bash,我的问题是,而不是我的index.php文件输出以下内容:

<?php

print "<center><h3>Sisko's LAMP activated!</h3><center>";

phpinfo();

我得到了我几小时前尝试的以下调试代码:

<?php

print "...responding";

phpinfo();

Docker可能在使用缓存系统吗?我已经修剪了所有的Docker卷,以防Docker可能正在使用缓存。除了其他与我的项目无关的容器正在使用的2个卷之外,我修剪了所有卷。

英文:

I'm learning Docker and I've been crafting a Dockerfile for an Ubuntu container.

My problem is I keep getting persistent information between different containers. I have exited, removed the container and then removed its image. After I made changes to my Dockerfile, I executed docker build -t playbuntu . Executing the following Dockerfile:

FROM ubuntu:latest
        
    ## for apt to be noninteractive
    ENV DEBIAN_FRONTEND noninteractive
    ENV DEBCONF_NONINTERACTIVE_SEEN true
    
    ## preesed tzdata, update package index, upgrade packages and install needed software
    RUN echo &quot;tzdata tzdata/Areas select Europe&quot; &gt; /tmp/preseed.txt; \
        echo &quot;tzdata tzdata/Zones/Europe select London&quot; &gt;&gt; /tmp/preseed.txt; \
        debconf-set-selections /tmp/preseed.txt &amp;&amp; \
        apt-get update &amp;&amp; \
        apt-get install -y tzdata
    
    
    RUN apt-get update -y &amp;&amp; apt-get upgrade -y &amp;&amp; apt-get install tree nano vim -y &amp;&amp; apt-get install less -y &amp;&amp; apt-get install lamp-server^ -y
    
    RUN echo &quot;ServerName localhost&quot; &gt;&gt; /etc/apache2/apache2.conf
    
    EXPOSE 80
    
    WORKDIR /var/www
    
    COPY ./index.php /var/www
    COPY ./000-default.conf /etc/apache2/sites-available
    
    CMD [ &quot;apache2ctl&quot;, &quot;start&quot;, &quot;&amp;&amp;&quot;, &quot;apache2ctl&quot;, &quot;restart&quot; ]

Once I execute winpty docker run -it -p 80:80 playbuntu bash, my problem is, instead of my index.php file outputting the following:

&lt;?php

    print &quot;&lt;center&gt;&lt;h3&gt;Sisko&#39;s LAMP activated!&lt;/h3&gt;&lt;center&gt;&quot;;
    
    phpinfo();

I get the following debug code I experimented with hours ago:

&lt;?php

    print &quot;...responding&quot;;
    
    phpinfo();

Is there a caching system Docker might be using? I pruned all Docker volumes just in case that is how Docker might be caching. All except 2 volumes which are being used by other containers unrelated to my project.

答案1

得分: 1

我怀疑,在您更改了主机上的 index.php 后,您没有重新运行 docker build ... 命令。您需要在图像的任何内容更改时重新创建容器映像。

请确认再次运行 docker build ... 命令是否解决了问题。

背景信息

Docker 映像由一个或多个层组成。

映像(层)和卷是不同的。

由于您的 docker run ... 命令中没有例如 --volume= 绑定挂载(或等效项),我怀疑 Docker 卷与此问题无关。

有可能重建图像不会替换图像层,因此会发生缓存。但是,在您的情况下,我认为这不是问题。请查看 Docker 文档(链接)以获取添加层的 Dockerfile 命令的概述。

由于层的工作方式,如果您的 Dockerfile 中的前置层未更改index.php 未更改,则 Docker 将不会重新构建这些层。然而,因为您的 Dockerfile 包含一个 apt-get update &amp;&amp; apt-get install .... 的层,该层将被作废,重新创建,随后的层也将如此。

如果您在主机上更改 index.php 并重新构建映像,此层将始终被重新构建。

我构建了您的 Dockerfile 两次。以下是第二次(!)构建命令的开头。请注意,未更改的层之前的 Using cache 命令,然后是 RUN apt-get update... 层,该层被重新构建。

docker build --rm -f &quot;Dockerfile&quot; -t 59582820:0939 &quot;.&quot;
Sending build context to Docker daemon  3.584kB
Step 1/10 : FROM ubuntu:latest
 ---&gt; 549b9b86cb8d
Step 2/10 : ENV DEBIAN_FRONTEND noninteractive
 ---&gt; Using cache
 ---&gt; 1529d0e293f3
Step 3/10 : ENV DEBCONF_NONINTERACTIVE_SEEN true
 ---&gt; Using cache
 ---&gt; 1ba10410d06a
Step 4/10 : RUN echo &quot;tzdata tzdata/Areas select Europe&quot; &gt; /tmp/preseed.txt;     echo &quot;tzdata tzdata/Zones/Europe select London&quot; &gt;&gt; /tmp/preseed.txt;     debconf-set-selections /tmp/preseed.txt &amp;&amp;     apt-get update &amp;&amp;     apt-get install -y tzdata
 ---&gt; Using cache
 ---&gt; afb861da52e4
Step 5/10 : RUN apt-get update -y &amp;&amp; apt-get upgrade -y &amp;&amp; apt-get install tree nano vim -y &amp;&amp; apt-get install less -y &amp;&amp; apt-get install lamp-server^ -y
 ---&gt; Running in 6f05bbb8e80a

根据证据,我认为您在更改 index.php 后没有重新构建。

英文:

I suspect that, after you changed index.php on your host machine you did not rerun the docker build ... command. You will need to recreate the container image any time any of the image's content changes.

Please confirm whether running the docker build ... command again solves the issue.

Background

Docker images comprise one of more layers.

Image (layers) and Volumes are distinct.

Since your docker run ... command contains no e.g. --volume= bind mounts (or equivalent), I suspect Docker Volumes are not relevant to this question.

There is a possibility that rebuilding images does not replace image layers and thus caching does occur. However, in your case, I think this is not the issue. See the Docker documentation (link) for an overview of the Dockerfile commands that add layers.

Because of the way that layers work, if the preceding layers in your Dockerfile were unchanged and the index.php were unchanged, then Docker would not rebuild these layers. However, because your Dockerfile includes a layer that apt-get update &amp;&amp; apt-get install ...., the layer will be invalidated, recreated and so will subsequent layers.

If you change index.php on the host and rebuild the image, this layer will always be rebuilt.

I built your Dockerfile twice. Here's the beginning of the second (!) build command. NB the Using cache commands for the unchanged layers preceding the RUN apt-get update... layer which is rebuilt.

docker build --rm -f &quot;Dockerfile&quot; -t 59582820:0939 &quot;.&quot;
Sending build context to Docker daemon  3.584kB
Step 1/10 : FROM ubuntu:latest
 ---&gt; 549b9b86cb8d
Step 2/10 : ENV DEBIAN_FRONTEND noninteractive
 ---&gt; Using cache
 ---&gt; 1529d0e293f3
Step 3/10 : ENV DEBCONF_NONINTERACTIVE_SEEN true
 ---&gt; Using cache
 ---&gt; 1ba10410d06a
Step 4/10 : RUN echo &quot;tzdata tzdata/Areas select Europe&quot; &gt; /tmp/preseed.txt;     echo &quot;tzdata tzdata/Zones/Europe select London&quot; &gt;&gt; /tmp/preseed.txt;     debconf-set-selections /tmp/preseed.txt &amp;&amp;     apt-get update &amp;&amp;     apt-get install -y tzdata
 ---&gt; Using cache
 ---&gt; afb861da52e4
Step 5/10 : RUN apt-get update -y &amp;&amp; apt-get upgrade -y &amp;&amp; apt-get install tree nano vim -y &amp;&amp; apt-get install less -y &amp;&amp; apt-get install lamp-server^ -y
 ---&gt; Running in 6f05bbb8e80a

The evidence suggests to me that you didn't rebuild after changing the index.php.

答案2

得分: 0

你可以使用卷来持久化一些文件夹,例如配置文件或属性等。 Docker 存储

英文:

You can use volumes to persist some folders like configs or properties etc. Docker Storage

huangapple
  • 本文由 发表于 2020年1月4日 01:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定