Skip to content

Commit 6c8f203

Browse files
committed
BUILD/MINOR: docker: optimize image layers and build caching
Refactor Dockerfile and Dockerfile.dev to improve build cache utilization and reduce the final image size. - Separate the base image setup (package installations, cleanup, and directory initialization) into a distinct step so it remains cached across source code changes. - Consolidate the installation of build artifacts and permission changes into a single layer using `--mount` instead of multiple `COPY` instructions. - Rename the golang builder stage to `builder-go` for better clarity. - Output the controller binary directly to `fs/usr/local/sbin/hug` during the build stage to streamline the final assembly.
1 parent 49b1969 commit 6c8f203

2 files changed

Lines changed: 37 additions & 30 deletions

File tree

build/Dockerfile

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM golang:1.26-alpine AS builder
15+
FROM golang:1.26-alpine AS builder-go
1616

1717
RUN apk --no-cache add git openssh
1818

@@ -24,11 +24,15 @@ WORKDIR /src
2424
RUN go install -trimpath -ldflags="-s" github.com/haproxytech/gopherd@v1.0.2
2525
COPY / /src
2626

27-
RUN mkdir -p /var/run/vars && \
28-
cd /src && \
29-
CGO_ENABLED=0 go build -buildvcs=true \
27+
RUN mkdir -p /var/run/vars
28+
29+
# stage gopherd into the fs overlay tree
30+
RUN mkdir -p fs/usr/local/sbin && \
31+
cp -a /go/bin/gopherd fs/usr/local/sbin/gopherd
32+
33+
RUN CGO_ENABLED=0 go build -buildvcs=true \
3034
-tags="$BUILD_TAGS" \
31-
-o fs/kubernetes-controller ./cmd/controller
35+
-o fs/usr/local/sbin/hug ./cmd/controller
3236

3337
FROM haproxytech/haproxy-alpine:3.2 AS builder-c
3438
RUN apk add --no-cache build-base gcc musl-dev
@@ -59,28 +63,29 @@ FROM haproxytech/haproxy-alpine:3.2
5963

6064
ARG TARGETPLATFORM
6165

62-
COPY /fs /
63-
COPY --from=builder /go/bin/gopherd /usr/local/sbin
64-
COPY --from=builder-c /src/libblock_secrets.so /usr/local/lib/libblock_secrets.so
65-
COPY --from=builder-c /src/haproxy_wrapper /usr/local/sbin/haproxy_wrapper
66-
66+
# base image setup only: stays cached across source changes
6767
RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
6868
rm -f /usr/local/bin/dataplaneapi /usr/bin/dataplaneapi /etc/haproxy/dataplaneapi.yml && \
6969
rm -f /usr/local/bin/dataplaneapi-v2 /usr/bin/dataplaneapi-v2 && \
7070
rm -f /etc/haproxy/haproxy.cfg && \
71-
rm usr/local/hug/defaults.go && \
72-
mkdir -p /usr/local/hug/aux && \
7371
mkdir -p /var/run/haproxy && \
72+
chown -R "haproxy:haproxy" /usr/local/etc/haproxy /run /var && \
73+
chmod -R ug+rwx /usr/local/etc/haproxy /run /var
74+
75+
# all build artifacts installed and fixed up in a single layer
76+
RUN --mount=from=builder-go,source=/src/fs,target=/mnt/builder-go \
77+
--mount=from=builder-c,source=/src,target=/mnt/builder-c \
78+
cp -a /mnt/builder-go/. / && \
79+
rm /usr/local/hug/defaults.go && \
80+
cp -a /mnt/builder-c/libblock_secrets.so /usr/local/lib/libblock_secrets.so && \
81+
cp -a /mnt/builder-c/haproxy_wrapper /usr/local/sbin/haproxy_wrapper && \
82+
mkdir -p /usr/local/hug/aux && \
7483
chgrp -R haproxy /usr/local/hug && \
7584
chmod -R ug+rwx /usr/local/hug && \
76-
chown -R "haproxy:haproxy" /usr/local/etc/haproxy /run /var && \
77-
chmod -R ug+rwx /usr/local/etc/haproxy /run /var && \
7885
chmod 644 /etc/gopherd/gopherd.yml && \
7986
chmod u+rx /usr/local/sbin/haproxy_wrapper && \
8087
setcap 'cap_net_bind_service=+ep' /usr/local/sbin/haproxy_wrapper
8188

82-
COPY --from=builder /src/fs/kubernetes-controller /usr/local/sbin/hug
83-
8489
STOPSIGNAL SIGTERM
8590

8691
ENTRYPOINT ["/usr/local/sbin/gopherd"]

build/Dockerfile.dev

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
FROM golang:1.26-alpine AS builder
14+
FROM golang:1.26-alpine AS builder-go
1515

1616
RUN apk --no-cache add git openssh
1717
WORKDIR /src
@@ -46,30 +46,32 @@ FROM haproxytech/haproxy-alpine:3.2
4646

4747
ARG TARGETPLATFORM
4848

49-
COPY /fs /
50-
COPY --from=builder /go/bin/gopherd /usr/local/sbin
51-
COPY --from=builder-c /src/libblock_secrets.so /usr/local/lib/libblock_secrets.so
52-
COPY --from=builder-c /src/haproxy_wrapper /usr/local/sbin/haproxy_wrapper
53-
54-
RUN mkdir -p /usr/local/hug/aux && \
55-
chgrp -R haproxy /usr/local/hug && \
56-
chmod -R ug+rwx /usr/local/hug
57-
49+
# base image setup only: stays cached across source changes
5850
RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
5951
rm -f /usr/local/bin/dataplaneapi /usr/bin/dataplaneapi /etc/haproxy/dataplaneapi.yml && \
6052
rm -f /usr/local/bin/dataplaneapi-v2 /usr/bin/dataplaneapi-v2 && \
6153
rm -f /etc/haproxy/haproxy.cfg && \
62-
rm usr/local/hug/defaults.go && \
63-
mkdir -p /usr/local/hug/aux && \
6454
mkdir -p /var/run/haproxy && \
55+
chown -R "haproxy:haproxy" /usr/local/etc/haproxy /run /var && \
56+
chmod -R ug+rwx /usr/local/etc/haproxy /run /var
57+
58+
# all build artifacts installed and fixed up in a single layer
59+
RUN --mount=type=bind,source=fs,target=/mnt/fs \
60+
--mount=from=builder-go,source=/go/bin,target=/mnt/builder-go \
61+
--mount=from=builder-c,source=/src,target=/mnt/builder-c \
62+
cp -a /mnt/fs/. / && \
63+
rm /usr/local/hug/defaults.go && \
64+
cp -a /mnt/builder-go/gopherd /usr/local/sbin/gopherd && \
65+
cp -a /mnt/builder-c/libblock_secrets.so /usr/local/lib/libblock_secrets.so && \
66+
cp -a /mnt/builder-c/haproxy_wrapper /usr/local/sbin/haproxy_wrapper && \
67+
mkdir -p /usr/local/hug/aux && \
6568
chgrp -R haproxy /usr/local/hug && \
6669
chmod -R ug+rwx /usr/local/hug && \
67-
chown -R "haproxy:haproxy" /usr/local/etc/haproxy /run /var && \
68-
chmod -R ug+rwx /usr/local/etc/haproxy /run /var && \
6970
chmod 644 /etc/gopherd/gopherd.yml && \
7071
chmod u+rx /usr/local/sbin/haproxy_wrapper && \
7172
setcap 'cap_net_bind_service=+ep' /usr/local/sbin/haproxy_wrapper
7273

74+
# host-built binary last: only this layer rebuilds during dev iteration
7375
COPY build/kubernetes-controller /usr/local/sbin/hug
7476

7577
STOPSIGNAL SIGTERM

0 commit comments

Comments
 (0)