Docker

如何強制 Docker 重新執行apt-get update

  • September 12, 2021

一些安全更新剛剛發布,我想重建我的 Docker 映像以利用這些更新。

但是,當我執行docker build .它時,它立即完成而不更新任何內容,因為 中沒有任何更改Dockerfile,並且所有內容都被記憶體。它甚至沒有嘗試apt-get update在我的Dockerfile.

apt-get update即使沒有任何變化,我如何強制 Docker 再次執行該命令?

有一個--no-cache選項說它在建構期間不會使用記憶體,但我希望它在之前的命令中使用記憶體,apt-get update並且我希望將結果保存到記憶體中以供下次執行(替換目前記憶體的圖像),所以我肯定想使用記憶體。

我也不能docker rmi用來刪除在執行後生成的圖像apt-get,因為它拒絕將此圖像刪除為image has dependent child images.

您可以嘗試以下方法:

FROM ubuntu:16.04

# LAYER 1
RUN echo "$(date), layer1" > /tmp/cache.txt

# LAYER 2
RUN echo "$(date), layer2" >> /tmp/cache.txt

# LAYER 3
ARG FORCE_UPDATE=no
RUN echo "$(date), layer3" >> /tmp/cache.txt

# LAYER 4
RUN echo "$(date), layer4" >> /tmp/cache.txt

CMD ["cat", "/tmp/cache.txt"]

第一次建構鏡像

$ docker build -t serverfault:900445 .
Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM ubuntu:16.04
---> 0458a4468cbc
Step 2/7 : RUN echo "$(date), layer1" > /tmp/cache.txt
---> Running in ac7f6b1e915a
Removing intermediate container ac7f6b1e915a
---> 42a6d14cc4cc
Step 3/7 : RUN echo "$(date), layer2" >> /tmp/cache.txt
---> Running in ba4cf5b54c35
Removing intermediate container ba4cf5b54c35
---> 783957979b21
Step 4/7 : ARG FORCE_UPDATE=no
---> Running in 818fd652d5cb
Removing intermediate container 818fd652d5cb
---> b8afb473cd9d
Step 5/7 : RUN echo "$(date), layer3" >> /tmp/cache.txt
---> Running in 38c0e6cbb94e
Removing intermediate container 38c0e6cbb94e
---> 03ac41df5bfa
Step 6/7 : RUN echo "$(date), layer4" >> /tmp/cache.txt
---> Running in 0294b5a4078e
Removing intermediate container 0294b5a4078e
---> 141667a2d5f3
Step 7/7 : CMD ["cat", "/tmp/cache.txt"]
---> Running in 86b852d8222d
Removing intermediate container 86b852d8222d
---> dcd57aca0c25
Successfully built dcd57aca0c25
Successfully tagged serverfault:900445

正如我們所見,所有層都已建構。再跑一次

$ docker build -t serverfault:900445 .
Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM ubuntu:16.04
---> 0458a4468cbc
Step 2/7 : RUN echo "$(date), layer1" > /tmp/cache.txt
---> Using cache
---> 42a6d14cc4cc
Step 3/7 : RUN echo "$(date), layer2" >> /tmp/cache.txt
---> Using cache
---> 783957979b21
Step 4/7 : ARG FORCE_UPDATE=no
---> Using cache
---> b8afb473cd9d
Step 5/7 : RUN echo "$(date), layer3" >> /tmp/cache.txt
---> Using cache
---> 03ac41df5bfa
Step 6/7 : RUN echo "$(date), layer4" >> /tmp/cache.txt
---> Using cache
---> 141667a2d5f3
Step 7/7 : CMD ["cat", "/tmp/cache.txt"]
---> Using cache
---> dcd57aca0c25
Successfully built dcd57aca0c25
Successfully tagged serverfault:900445

現在所有圖層都從記憶體中獲取。簡單檢查

$ docker run -it --rm serverfault:900445
Wed Mar  7 15:44:22 UTC 2018, layer1
Wed Mar  7 15:44:23 UTC 2018, layer2
Wed Mar  7 15:44:24 UTC 2018, layer3
Wed Mar  7 15:44:25 UTC 2018, layer4

現在,如果您需要為某些特定圖層強制更新記憶體,請使用以下命令

$ docker build --build-arg FORCE_UPDATE=$(date '+%s') -t serverfault:900445 .
Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM ubuntu:16.04
---> 0458a4468cbc
Step 2/7 : RUN echo "$(date), layer1" > /tmp/cache.txt
---> Using cache
---> 42a6d14cc4cc
Step 3/7 : RUN echo "$(date), layer2" >> /tmp/cache.txt
---> Using cache
---> 783957979b21
Step 4/7 : ARG FORCE_UPDATE=no
---> Using cache
---> b8afb473cd9d
Step 5/7 : RUN echo "$(date), layer3" >> /tmp/cache.txt
---> Running in f8ad1cd195eb
Removing intermediate container f8ad1cd195eb
---> b22972691095
Step 6/7 : RUN echo "$(date), layer4" >> /tmp/cache.txt
---> Running in 9994175a082e
Removing intermediate container 9994175a082e
---> 7ed42904373f
Step 7/7 : CMD ["cat", "/tmp/cache.txt"]
---> Running in 67de76e45d43
Removing intermediate container 67de76e45d43
---> 833f3faf9fd7
Successfully built 833f3faf9fd7
Successfully tagged serverfault:900445

如您所見,第 1,2 層是從記憶體中獲取的,但第 3 層和所有最新層均已重建

$ docker run -it --rm serverfault:900445
Wed Mar  7 15:44:22 UTC 2018, layer1
Wed Mar  7 15:44:23 UTC 2018, layer2
Wed Mar  7 15:45:35 UTC 2018, layer3
Wed Mar  7 15:45:35 UTC 2018, layer4

再重複一次

$ docker build --build-arg FORCE_UPDATE=$(date '+%s') -t serverfault:900445 .
Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM ubuntu:16.04
---> 0458a4468cbc
Step 2/7 : RUN echo "$(date), layer1" > /tmp/cache.txt
---> Using cache
---> 42a6d14cc4cc
Step 3/7 : RUN echo "$(date), layer2" >> /tmp/cache.txt
---> Using cache
---> 783957979b21
Step 4/7 : ARG FORCE_UPDATE=no
---> Using cache
---> b8afb473cd9d
Step 5/7 : RUN echo "$(date), layer3" >> /tmp/cache.txt
---> Running in 618880ba45be
Removing intermediate container 618880ba45be
---> b0512372ddfd
Step 6/7 : RUN echo "$(date), layer4" >> /tmp/cache.txt
---> Running in 0cb552431048
Removing intermediate container 0cb552431048
---> 61be6f0c0f21
Step 7/7 : CMD ["cat", "/tmp/cache.txt"]
---> Running in 5f9ee850c28e
Removing intermediate container 5f9ee850c28e
---> ac73b7754107
Successfully built ac73b7754107
Successfully tagged serverfault:900445


$ docker run -it --rm serverfault:900445
Wed Mar  7 15:44:22 UTC 2018, layer1
Wed Mar  7 15:44:23 UTC 2018, layer2
Wed Mar  7 15:46:10 UTC 2018, layer3
Wed Mar  7 15:46:11 UTC 2018, layer4

引用自:https://serverfault.com/questions/900445