Skip to content

Commit

Permalink
fix: more work on optimization proliferation of docker layers
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Mitchell <[email protected]>
  • Loading branch information
starpit committed Aug 15, 2024
1 parent 0922d8e commit 22e7554
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
/tests
.git
*.log
*~
*.tar.gz
**/*~
**/*.tar.gz
25 changes: 16 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
FROM docker.io/golang:1.22.6-alpine as builder
# download go modules
FROM docker.io/golang:1.22.6-alpine as base
LABEL lunchpail=temp
WORKDIR /init

ENV CGO_ENABLED=0
COPY go.* .
RUN go mod download

COPY go.mod .
COPY go.sum .
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg/mod go mod download -x

COPY cmd cmd
COPY pkg pkg
COPY charts charts

# build the CLI
RUN go generate ./... && \
FROM base as builder
LABEL lunchpail=temp
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg/mod \
go generate ./... && \
go generate ./... && \
go build -ldflags="-s -w" -o lunchpail cmd/main.go && \
chmod a+rX lunchpail && \
find . -name '*.tar.gz' -exec rm {} \;
go build -ldflags="-s -w" -o /tmp/lunchpail cmd/main.go && \
find . -name '*.tar.gz' -exec rm {} \; && \
chmod a+rX /tmp/lunchpail

FROM docker.io/alpine:3
LABEL org.opencontainers.image.source="https://github.com/IBM/lunchpail"
LABEL lunchpail=final org.opencontainers.image.source="https://github.com/IBM/lunchpail"

RUN adduser -u 2000 lunchpail -G root --disabled-password && echo "lunchpail:lunchpail" | chpasswd && chmod -R g=u /home/lunchpail
ENV HOME=/home/lunchpail
WORKDIR /home/lunchpail

COPY --from=builder /init/lunchpail /usr/local/bin/lunchpail
COPY --from=builder /tmp/lunchpail /usr/local/bin/lunchpail

USER lunchpail
CMD ["lunchpail"]
22 changes: 22 additions & 0 deletions pkg/lunchpail/images/build/buildImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ func buildIt(dir, name, dockerfile string, kind ImageOrManifest, cli ContainerCl
".",
)
} else {
// clean out prior "final" and "temp" layers before building a new one
if err := clean(cli, "final"); err != nil {
return "", err
}
if err := clean(cli, "temp"); err != nil {
return "", err
}

cmd = exec.Command(
string(cli),
"build",
Expand Down Expand Up @@ -119,6 +127,20 @@ func buildIt(dir, name, dockerfile string, kind ImageOrManifest, cli ContainerCl
// TODO --build-arg registry=$IMAGE_REGISTRY --build-arg repo=$IMAGE_REPO --build-arg version=$VERSION \
}

func clean(cli ContainerCli, label string) error {
cmd := exec.Command(
string(cli),
"image",
"prune",
"--force",
"--filter",
"label=lunchpail="+label,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func buildProd(dir, name, dockerfile string, cli ContainerCli, verbose bool, force bool) (string, error) {
image := imageName(name)

Expand Down

0 comments on commit 22e7554

Please sign in to comment.