gpt4 book ai didi

保留数据的 Docker 容器

转载 作者:行者123 更新时间:2023-12-02 19:21:12 36 4
gpt4 key购买 nike

我正在学习 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 个卷被与我的项目无关的其他容器使用之外的所有卷。

最佳答案

我怀疑,在您更改 index.php 之后在你的主机上你做了不是 重新运行 docker build ...命令。每当图像的任何内容发生更改时,您都需要重新创建容器图像。
请确认是否运行docker build ...命令再次解决了这个问题。
背景
Docker 镜像包含多个层之一。
图像(层)和体积是不同的。
由于您的docker run ...命令不包含例如--volume=绑定(bind)安装(或等效),我怀疑 Docker 卷与这个问题无关。
重建图像有可能不是 替换图像层,因此确实会发生缓存。但是,在您的情况下,我认为这是 不是 问题。有关添加层的 Dockerfile 命令的概述,请参阅 Docker 文档 (link)。
由于层的工作方式,如果 Dockerfile 中的前面层未更改 index.php没有改变,那么 Docker 就不会重建这些层。但是,因为您的 Dockerfile 包含一个层,apt-get update && 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 后没有重建。

关于保留数据的 Docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59582820/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com