Skip to content

Commit 9d6f215

Browse files
committed
images: slim down golang binaries, by building without DWARF/debug symbols, stripping prebuilts, and removing unneeded bins
- strip golang binaries (both during build with ldflags and prebuilt ones with 'strip'/binutils) - don't ship apk caches - we won't use docker-buildx nor docker-compose bins, which are huge; remove them - remove stray 'hook-bootkit' binary from source directory (leftover from ?) Signed-off-by: Ricardo Pardini <[email protected]>
1 parent 89ad28b commit 9d6f215

File tree

6 files changed

+12
-6
lines changed

6 files changed

+12
-6
lines changed

images/hook-bootkit/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM golang:1.21-alpine as dev
22
COPY . /src/
33
WORKDIR /src
44
RUN go mod download
5-
RUN CGO_ENABLED=0 go build -a -ldflags '-w -extldflags "-static"' -o /bootkit
5+
RUN CGO_ENABLED=0 go build -a -ldflags '-s -w -extldflags "-static"' -o /bootkit
66

77
FROM alpine
88
COPY --from=dev /bootkit .

images/hook-bootkit/hook-bootkit

-9.07 MB
Binary file not shown.

images/hook-containerd/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ RUN mkdir -p $GOPATH/src/github.com/containerd && \
1818
git checkout $CONTAINERD_COMMIT
1919
RUN apk add --no-cache btrfs-progs-dev gcc libc-dev linux-headers make libseccomp-dev
2020
WORKDIR $GOPATH/src/github.com/containerd/containerd
21-
RUN make binaries EXTRA_FLAGS="-buildmode pie" EXTRA_LDFLAGS='-extldflags "-fno-PIC -static"' BUILDTAGS="static_build no_devmapper"
21+
RUN make binaries EXTRA_FLAGS="-buildmode pie" EXTRA_LDFLAGS='-w -s -extldflags "-fno-PIC -static"' BUILDTAGS="static_build no_devmapper"
2222

2323
# install nerdctl
2424
RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then ARCHITECTURE=amd64; elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then ARCHITECTURE=arm64; else ARCHITECTURE=amd64; fi \

images/hook-docker/Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
FROM golang:1.20-alpine as dev
22
COPY . /src/
33
WORKDIR /src
4-
RUN CGO_ENABLED=0 go build -a -ldflags '-w -extldflags "-static"' -o /hook-docker
4+
RUN CGO_ENABLED=0 go build -a -ldflags '-s -w -extldflags "-static"' -o /hook-docker
55

66
FROM docker:26.1.0-dind
77
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
8-
RUN apk update; apk add kexec-tools
8+
RUN apk update && apk add kexec-tools binutils && rm -rf /var/cache/apk/*
9+
# Won't use docker-buildx nor docker-compose
10+
RUN rm -rf /usr/local/libexec/docker/cli-plugins
11+
# Strip some large binaries
12+
RUN strip /usr/local/bin/docker /usr/local/bin/dockerd /usr/local/bin/docker-proxy /usr/local/bin/runc
13+
# Purge binutils package after stripping
14+
RUN apk del binutils
915
COPY --from=dev /hook-docker .
1016
ENTRYPOINT ["/hook-docker"]

images/hook-mdev/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM alpine
22

33
USER root:root
44

5-
RUN apk add --no-cache mdev-conf
5+
RUN apk add --no-cache mdev-conf && rm -rf /var/cache/apk/*
66

77
CMD ["mdev", "-v", "-df"]
88

images/hook-runc/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ RUN mkdir -p $GOPATH/src/github.com/opencontainers && \
1919
git clone https://github.com/opencontainers/runc.git
2020
WORKDIR $GOPATH/src/github.com/opencontainers/runc
2121
RUN git checkout $RUNC_COMMIT
22-
RUN make static BUILDTAGS="seccomp" EXTRA_FLAGS="-buildmode pie" EXTRA_LDFLAGS="-extldflags \\\"-fno-PIC -static\\\""
22+
RUN make static BUILDTAGS="seccomp" EXTRA_FLAGS="-buildmode pie" EXTRA_LDFLAGS="-s -w -extldflags \\\"-fno-PIC -static\\\""
2323
RUN cp runc /usr/bin/
2424

2525
RUN mkdir -p /etc/init.d && ln -s /usr/bin/service /etc/init.d/010-onboot

0 commit comments

Comments
 (0)