-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
50 lines (39 loc) · 1.64 KB
/
Dockerfile
File metadata and controls
50 lines (39 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Build stage
FROM --platform=$BUILDPLATFORM golang:1.26-alpine@sha256:2389ebfa5b7f43eeafbd6be0c3700cc46690ef842ad962f6c5bd6be49ed82039 AS builder
# Define ARGs to specify the target platform and build metadata
ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH
ARG BUILD_DATE
ARG VCS_REF
# Set the working directory inside the container
WORKDIR /app
# Copy go.mod to download dependencies
COPY go.mod ./
# Download dependencies
RUN go mod download
# Copy the source code into the container
COPY . .
# Compile the application to a binary with all dependencies included
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-s -w" -a -o /main cmd/echo-app/main.go
# Final stage
FROM scratch
# Re-declare ARGs in the final stage to use them
ARG BUILD_DATE
ARG VCS_REF
# Add metadata to the image using opencontainers labels
LABEL org.opencontainers.image.title="echo-app" \
org.opencontainers.image.description="Tiny golang app which returns a timestamp, a customizable message, the hostname, the request source IP, and optionally the HTTP request headers." \
org.opencontainers.image.url="https://github.com/philipschmid/echo-app" \
org.opencontainers.image.source="https://github.com/philipschmid/echo-app" \
org.opencontainers.image.vendor="philipschmid" \
org.opencontainers.image.licenses="Apache-2.0 license" \
org.opencontainers.image.revision="$VCS_REF" \
org.opencontainers.image.created="$BUILD_DATE"
# Copy the compiled binary from the builder stage
COPY --from=builder /main /main
# Set the binary as the entrypoint of the container
ENTRYPOINT ["/main"]
# Expose port 8080
EXPOSE 8080