Home > Back-end >  Steps kept being rebuilt again and again, they are never cached
Steps kept being rebuilt again and again, they are never cached

Time:12-09

I have a multistage build:

FROM almalinux:8 AS builder

WORKDIR /build
ADD . /build
RUN dnf install -y yum-utils dnf-plugins-core
RUN yum install -y epel-release
RUN dnf config-manager --set-enabled powertools
RUN yum install -y gcc gcc-c   cmake3 make
RUN yum install -y git libpcap-devel
RUN yum module -y install go-toolset
RUN ./install.sh

#                                                                           
#   Final smaller image
#                                                                           
FROM almalinux:8
...

It never goes to the end because there is an error in the final smaller image. Nevertheless, podman/docker always re-run all the RUN instructions of the builder part. It just takes forever to debug and build the image.

How can I instruct podman to stop rebuilding these steps (yum install -y)?!

CodePudding user response:

The reason it can't reuse the cached layers is that the statement

ADD . /build

will very often lead to a different file system than the one from last build. The reason is that if you change any files in the host directory, the image will be changed.

If you move the ADD statement to the end of the stage in the Dockerfile, the image will not have been different when you do yum install, so it can reuse the cached layers:

FROM almalinux:8 AS builder

WORKDIR /build
RUN dnf install -y yum-utils dnf-plugins-core
RUN yum install -y epel-release
RUN dnf config-manager --set-enabled powertools
RUN yum install -y gcc gcc-c   cmake3 make
RUN yum install -y git libpcap-devel
RUN yum module -y install go-toolset
ADD . /build
RUN ./install.sh

CodePudding user response:

I am not use podman, But Conceptually it will be the same.

Debug your dockerfile , you can run a container, and execute command one by one, find error, fix command , until no error message, then take all correct command to your dockerfile

docker run -it almalinux:8 bash

then copy command, not include RUN, and find which command have error.

Dockerfile - myalmalinux8 a build base image

FROM almalinux:8
RUN dnf update -y \
 && dnf install -y yum-utils dnf-plugins-core epel-release \
 && dnf config-manager --set-enabled powertools \
 && dnf install -y gcc gcc-c   cmake3 make git libpcap-devel \
 && dnf module -y install go-toolset

Command

docker build -t myalmalinux8:0.0.1 .

Your Dockerfile

FROM myalmalinux8:0.0.1
WORKDIR /build
ADD . /build
RUN ./install.sh

#                                                                           
#   Final smaller image
#                                                                           
FROM almalinux:8
...
  • Related