-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat!: run container images as a non-root user #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,7 @@ | ||
| # Git | ||
| .git | ||
| .gitignore | ||
|
|
||
| # GitHub | ||
| .github/ | ||
|
|
||
| # Docker | ||
| Dockerfile | ||
| .dockerignore | ||
|
|
||
| # Build artifacts | ||
| *.exe | ||
| *.exe~ | ||
| *.dll | ||
| *.so | ||
| *.dylib | ||
| *.test | ||
| *.out | ||
|
|
||
| # IDE files | ||
| .idea/ | ||
| .vscode/ | ||
| *.swp | ||
| *.swo | ||
|
|
||
| # Misc | ||
| README.md | ||
| assets/ | ||
| LICENSE | ||
| # Only application source and module metadata belong in the build context. | ||
| ** | ||
| !Dockerfile | ||
| !go.mod | ||
| !go.sum | ||
| !*.go | ||
| *_test.go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,5 +7,5 @@ doormouse | |
| go-wol-proxy | ||
| *.migrated.toml | ||
|
|
||
| # goreleaser output | ||
| # release build output | ||
| dist/ | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,56 @@ | ||
| FROM golang:1.23-alpine AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Copy go.mod and go.sum files | ||
| # Compile on the builder's native architecture, even for cross-platform images. | ||
| ARG BUILDPLATFORM | ||
| FROM --platform=$BUILDPLATFORM golang:1.24.3-alpine AS build | ||
| WORKDIR /src | ||
| COPY go.mod go.sum ./ | ||
|
|
||
| # Download dependencies | ||
| RUN go mod download | ||
| COPY *.go ./ | ||
| ARG TARGETOS=linux | ||
| ARG TARGETARCH | ||
| RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/doormouse . | ||
|
|
||
| # Copy the source code | ||
| COPY . . | ||
|
|
||
| # Build the application | ||
| RUN CGO_ENABLED=0 GOOS=linux go build -o doormouse . | ||
|
|
||
| # Create a minimal runtime image | ||
| FROM alpine:3.22 | ||
|
|
||
| WORKDIR /app | ||
| # Everything that does not need the binary happens before it is copied in, so a | ||
| # new release busts as little cache as possible. Only setcap has to come after. | ||
| # | ||
| # The account is dedicated and unprivileged. UID and GID are pinned to 1000, the | ||
| # first user on most Linux hosts, so an SSH key that is 0600 and owned by the | ||
| # host user stays readable through a bind mount with no chown. | ||
| # | ||
| # libcap-setcap rather than libcap: it is the only piece needed here and pulls | ||
| # two packages instead of five. It stays in the image, which costs about 60 kB. | ||
| # Removing it after the COPY would save nothing, since the files would still sit | ||
| # in this layer with only a whiteout on top, and it would put an apk fetch back | ||
| # on the path every release rebuilds. | ||
| RUN addgroup -g 1000 doormouse \ | ||
| && adduser -D -u 1000 -G doormouse doormouse \ | ||
| && apk add --no-cache libcap-setcap | ||
|
|
||
| # Copy the binary from the builder stage | ||
| COPY --from=builder /app/doormouse /app/ | ||
| WORKDIR /app | ||
|
|
||
| # Expose the default port. TCP routes listen on their own ports; with | ||
| # network_mode: host they are reachable directly, otherwise publish each one. | ||
| # The binary lives outside /app so that /app can be bind-mounted as a whole | ||
| # writable config directory without handing the runtime user its own binary. | ||
| COPY --from=build /out/doormouse /usr/local/bin/doormouse | ||
|
|
||
| # doormouse runs as a non-root user, and the kernel would otherwise stop it from | ||
| # binding ports below 1024. This file capability grants that one bind permission, | ||
| # so `port = ":443"` works with neither root nor a host sysctl. | ||
| # CAP_NET_BIND_SERVICE is in Docker's default set, so no cap_add is needed. Drop | ||
| # it and the container will not start at all: the kernel refuses to exec a file | ||
| # whose capability it cannot grant. | ||
| # | ||
| # The one step that cannot move above the COPY: setcap stamps the binary, so the | ||
| # binary has to be there. It is a single local syscall, with nothing to fetch. | ||
| RUN setcap cap_net_bind_service=+ep /usr/local/bin/doormouse | ||
|
|
||
| # Numeric, not the name: Kubernetes cannot verify runAsNonRoot against a | ||
| # username and refuses to start the pod, so the number has to be on the image. | ||
| USER 1000:1000 | ||
|
|
||
| # Same contract as before: the default port, and a config mounted at | ||
| # /app/config.toml. TCP routes listen on their own ports; with network_mode: | ||
| # host they are reachable directly, otherwise publish each one. | ||
| EXPOSE 8080 | ||
|
|
||
| # Run the application | ||
| ENTRYPOINT ["/app/doormouse", "/app/config.toml"] | ||
| ENTRYPOINT ["/usr/local/bin/doormouse", "/app/config.toml"] |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.