Skip to content

Instantly share code, notes, and snippets.

@whoiscarlo
Last active June 11, 2025 21:03
Show Gist options
  • Save whoiscarlo/b796c1c708068e93573c948d58615657 to your computer and use it in GitHub Desktop.
Save whoiscarlo/b796c1c708068e93573c948d58615657 to your computer and use it in GitHub Desktop.
Example Dockerfile for Golang
## BUILD GO BINARY
FROM golang:1.24-alpine AS builder
## Set the working directory
WORKDIR /go/backend
## Copy necessary directories and files
COPY . .
## Download dependencies
RUN go mod download && go mod verify
## Build the go binary
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-s -w" ./cmd/exampleProject && \
mv exampleProject /go/backend/cmd/exampleProject;
####################################################################################################
## BUILD SERVICE IMAGE
FROM alpine:latest
RUN apk --no-cache add ca-certificates tzdata
WORKDIR /root
COPY --from=builder go/backend/cmd/exampleProject .
# Expose the port used by the backend
EXPOSE 5677
# Set the environment variable
ARG GOBACK_BUILD_TIMESTAMP
ENV GOBACK_BUILD_TIMESTAMP=${GOBACK_BUILD_TIMESTAMP}
# Run the binary
CMD ["./exampleProject"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment