【问题标题】:Docker containers retaining data保留数据的 Docker 容器
【发布时间】:2020-04-22 06:26:30
【问题描述】:

我正在学习 Docker,并且一直在为 Ubuntu 容器制作 Dockerfile。

我的问题是我不断获取不同容器之间的持久信息。我已经退出,移除了容器,然后移除了它的图像。在对 Dockerfile 进行更改后,我执行了docker build -t playbuntu . 执行以下 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 "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 个卷被与我的项目无关的其他容器使用之外的所有卷。

【问题讨论】:

    标签: docker dockerfile lamp


    【解决方案1】:

    我怀疑,在您更改主机上的index.php 之后,您确实没有重新运行docker build ... 命令。每当图像的任何内容发生更改时,您都需要重新创建容器图像。

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

    背景

    Docker 映像包含多个层之一。

    图像(层)和体积是不同的。

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

    重建图像可能不会替换图像层,因此会发生缓存。但是,就您而言,我认为这不是问题。有关添加层的 Dockerfile 命令的概述,请参阅 Docker 文档 (link)。

    由于层的工作方式,如果您的 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 "Dockerfile" -t 59582820:0939 "."
    Sending build context to Docker daemon  3.584kB
    Step 1/10 : FROM ubuntu:latest
     ---> 549b9b86cb8d
    Step 2/10 : ENV DEBIAN_FRONTEND noninteractive
     ---> Using cache
     ---> 1529d0e293f3
    Step 3/10 : ENV DEBCONF_NONINTERACTIVE_SEEN true
     ---> Using cache
     ---> 1ba10410d06a
    Step 4/10 : 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
     ---> Using cache
     ---> afb861da52e4
    Step 5/10 : 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
     ---> Running in 6f05bbb8e80a
    

    证据表明你在更改 index.php 后没有重建。

    【讨论】:

    • 谢谢。我再次运行它,但我绝对确定我删除了我的 ubuntu 映像并每次都执行 docker build。另外,最初我试图创建一个连接以允许我从 Windows 编辑容器文件。那行不通
    • 所以,这是一个奇怪的结果。尽管我断言我一直在重建图像,但这次我在 index.php 中得到了正确的内容。但是,我退出并删除了该容器,并从同一个图像创建了第二个容器。这次唯一的不同是我添加了 -v c:/localfile-system:/var/www 来尝试从 Windows 编辑容器代码。现在,index.php 再次显示调试代码
    • 所以... Docker Image(s) 和 Container(s) 之间也有区别。运行映像时,它会创建一个容器。可以从同一个镜像创建多个容器。我认为您的问题是您没有重建图像,因此从中创建的任何容器都使用了原始的index.php
    【解决方案2】:

    您可以使用卷来保存一些文件夹,例如配置或属性等。Docker Storage

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2020-11-08
      • 2019-09-04
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多