Skip to content

Instantly share code, notes, and snippets.

@h4ck4life
Created December 28, 2023 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h4ck4life/80b7bd6ea371330ecaae8c78ef2c7dd1 to your computer and use it in GitHub Desktop.
Save h4ck4life/80b7bd6ea371330ecaae8c78ef2c7dd1 to your computer and use it in GitHub Desktop.
golang + sqlite3 dockerfile
FROM golang:alpine3.18 AS build
# Important:
# Because this is a CGO enabled package, you are required to set it as 1.
ENV CGO_ENABLED=1
RUN apk add --no-cache \
# Important: required for go-sqlite3
gcc \
# Required for Alpine
musl-dev
WORKDIR /workspace
COPY . /workspace/
RUN \
go mod tidy && \
go install -ldflags='-s -w -extldflags "-static"' ./main.go
FROM scratch
COPY --from=build /go/bin/main /usr/local/bin/main
ENV PORT=8080
WORKDIR /app
ENTRYPOINT [ "/usr/local/bin/main" ]
@h4ck4life
Copy link
Author

h4ck4life commented Jan 31, 2024

Use for Chi framework:

FROM golang:alpine3.18 AS build

# Important:
#   Because this is a CGO enabled package, you are required to set it as 1.
ENV CGO_ENABLED=0

RUN apk update && apk add --no-cache \
    # Important: required for go-sqlite3
    gcc \
    # Required for Alpine
    musl-dev \
    ca-certificates && update-ca-certificates

WORKDIR /workspace

COPY . /workspace/

RUN \
    go mod tidy && \
    go install -ldflags='-s -w -extldflags "-static"' ./main.go

FROM scratch

# Copy all certificates to final image
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

COPY --from=build /go/bin/main /usr/local/bin/main

WORKDIR /app

ENTRYPOINT [ "/usr/local/bin/main" ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment