Skip to content

Commit

Permalink
feat(docker): add a dockerfile and lint it in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
willejs committed Aug 20, 2024
1 parent 1d5a456 commit aeb8948
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci-cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ jobs:
uses: golangci/golangci-lint-action@v6
with:
version: v1.60

# lint the dockerfile too
- uses: hadolint/[email protected]
with:
dockerfile: Dockerfile

test:
name: Test
Expand Down
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Build stage
FROM golang:1.22 AS builder

# do people even care about the LFS hirarchy anymore?
WORKDIR /app

# Copy go mod and sum files in first so we can cache the dependencies
COPY go.mod go.sum ./
RUN go mod download

# copy in the app and build it
COPY . .

# statically compile the go binary for the presumed target of amd64 linux
# whilst its larger, its more portable and will run in a scratch container
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o api-server ./cmd/api-server

# hack to create the nobody user for the scratch container.
# hadolint ignore=DL3059
RUN echo "nobody:x:65534:65534:Nobody:/:" > /etc_passwd

# Use multi stage builds. This is the final runtime stage. We can use ephemeral containers in kubernetes now :tada:
# We could make a development target if this displeases people too.
FROM scratch

# expose the port and hardcode it for now
EXPOSE 8080

# copy in the nobody user in
COPY --from=builder /etc_passwd /etc/passwd

WORKDIR /app
COPY --from=builder /app/api-server /app/api-server

# dont run the app as root, it is insecure
USER nobody
CMD ["./api-server"]

0 comments on commit aeb8948

Please sign in to comment.