Browse Source

Improve Dockerfile structure and efficiency

This commit improves the Dockerfile as follows:

- Combine ENV variables into a single line - making it easier to read.
- Adds `--no-cache` to the apk command - helps the Dockerfile use less space.
- Adds comments to explain what each line of code does, making it easier to understand.

These changes help improve the readability and efficiency of the Dockerfile.
BenjaminS 2 years ago
parent
commit
1ca6a6cc32
1 changed files with 20 additions and 3 deletions
  1. 20 3
      Dockerfile

+ 20 - 3
Dockerfile

@@ -1,13 +1,30 @@
+# Define argument for linker flags
 ARG LDFLAGS=-s -w
 ARG LDFLAGS=-s -w
+
+# Use a temporary build image based on Golang 1.20-alpine
 FROM golang:1.20-alpine as builder
 FROM golang:1.20-alpine as builder
-ENV LDFLAGS=$LDFLAGS
+
+# Set environment variables: linker flags and disable CGO
+ENV LDFLAGS=$LDFLAGS CGO_ENABLED=0
+
+# Add the current directory contents to the work directory in the container
 ADD . /work
 ADD . /work
-RUN cd /work && \
-    CGO_ENABLED=0 && \
+
+# Set the current work directory inside the container
+WORKDIR /work
+
+# Install git and build the edgevpn binary with the provided linker flags
+# --no-cache flag ensures the package cache isn't stored in the layer, reducing image size
+RUN apk add --no-cache git && \
     go build -ldflags="$LDFLAGS" -o edgevpn
     go build -ldflags="$LDFLAGS" -o edgevpn
 
 
 # TODO: move to distroless
 # TODO: move to distroless
+
+# Use a new, clean alpine image for the final stage
 FROM alpine
 FROM alpine
+
+# Copy the edgevpn binary from the builder stage to the final image
 COPY --from=builder /work/edgevpn /usr/bin/edgevpn
 COPY --from=builder /work/edgevpn /usr/bin/edgevpn
 
 
+# Define the command that will be run when the container is started
 ENTRYPOINT ["/usr/bin/edgevpn"]
 ENTRYPOINT ["/usr/bin/edgevpn"]