Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Improve Dockerfile (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
craigpastro authored Nov 9, 2023
1 parent a2eae5f commit 4353a8d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**/.dockerignore
**/.github
**/.gitignore
**/.vscode
**/compose*
**/Dockerfile*
LICENSE
README.md
todoapp
29 changes: 23 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
FROM cgr.dev/chainguard/go:latest as builder
# syntax=docker/dockerfile:1

WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -o todoapp ./cmd/todoapp
FROM cgr.dev/chainguard/go:latest as build

WORKDIR /src

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into
# the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go mod download -x

# Build the application.
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage a bind mount to the current directory to avoid having to copy the
# source code into the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,target=. \
CGO_ENABLED=0 go build -o /bin/server ./cmd/todoapp

FROM cgr.dev/chainguard/static:latest

COPY --from=build /bin/server /bin/
EXPOSE 8080
COPY --from=builder /app/todoapp /todoapp

ENTRYPOINT ["/todoapp"]
ENTRYPOINT [ "/bin/server" ]

0 comments on commit 4353a8d

Please sign in to comment.