Skip to content

Commit cab8b3d

Browse files
yosida95mccutchen
andauthored
docker: switch base image from distroless/base to distroless/static:nonroot (#224)
This cuts image size in half and improves security posture by default, at the cost of a bit more complexity for what should be slightly unusual deployments (privileged ports AND host network). --------- Signed-off-by: Kohei YOSHIDA <[email protected]> Co-authored-by: Will McCutchen <[email protected]>
1 parent d0da72b commit cab8b3d

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ COPY . .
88
RUN --mount=type=cache,id=gobuild,target=/root/.cache/go-build \
99
make build buildtests
1010

11-
FROM gcr.io/distroless/base
11+
FROM gcr.io/distroless/static:nonroot
1212

1313
COPY --from=build /go/src/github.com/mccutchen/go-httpbin/dist/go-httpbin* /bin/
1414

README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ automatically published to these public registries for every tagged release:
1919
- [mccutchen/go-httpbin][docker-hub]
2020

2121
```bash
22-
# Run http server
2322
$ docker run -P ghcr.io/mccutchen/go-httpbin
24-
25-
# Run https server
26-
$ docker run -e HTTPS_CERT_FILE='/tmp/server.crt' -e HTTPS_KEY_FILE='/tmp/server.key' -p 8080:8080 -v /tmp:/tmp ghcr.io/mccutchen/go-httpbin
2723
```
2824

25+
> [!NOTE]
26+
> Prebuilt image versions >= 2.19.0 run as a non-root user by default. See
27+
> [Configuring non-root docker images](#configuring-non-root-docker-images)
28+
> below for details.
29+
2930
### Kubernetes
3031

3132
```
@@ -109,10 +110,9 @@ variables (or a combination of the two):
109110
| `-srv-read-timeout` | `SRV_READ_TIMEOUT` | Value to use for the http.Server's ReadTimeout option | 5s |
110111
| `-use-real-hostname` | `USE_REAL_HOSTNAME` | Expose real hostname as reported by os.Hostname() in the /hostname endpoint | false |
111112

112-
#### ⚠️ **HERE BE DRAGONS** ⚠️
113-
114-
These configuration options are dangerous and/or deprecated and should be
115-
avoided unless backwards compatibility is absolutely required.
113+
> [!WARNING]
114+
> These configuration options are dangerous and/or deprecated and should be
115+
> avoided unless backwards compatibility is absolutely required.
116116
117117
| Argument| Env var | Documentation | Default |
118118
| - | - | - | - |
@@ -123,6 +123,35 @@ avoided unless backwards compatibility is absolutely required.
123123
- See [Production considerations] for recommendations around safe configuration
124124
of public instances of go-httpbin
125125

126+
#### Configuring non-root docker images
127+
128+
Prebuilt image versions >= 2.19.0 run as a non-root user by default to improve
129+
container security at the cost of additional complexity for some non-standard
130+
deployments:
131+
132+
- To run the go-httpbin image a) on a privileged port (i.e. below 1024) _and_
133+
b) using the Docker host network, you may need to run the container as root
134+
in order to enable the `CAP_NET_BIND_SERVICE` capability:
135+
136+
```bash
137+
$ docker run \
138+
--network host \
139+
--user root \
140+
--cap-drop ALL \
141+
--cap-add CAP_NET_BIND_SERVICE \
142+
ghcr.io/mccutchen/go-httpbin \
143+
/bin/go-httpbin -port=80
144+
```
145+
146+
- If you enable HTTPS directly in the image, make sure that the certificate
147+
and private key files are readable by the user running the process:
148+
149+
```bash
150+
$ chmod 644 /tmp/server.crt
151+
$ chmod 640 /tmp/server.key
152+
# GID 65532: primary group of the nonroot user in distroless/static:nonroot.
153+
$ chown root:65532 /tmp/server.crt /tmp/server.key
154+
```
126155

127156
## Installation
128157

kustomize/resources.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ spec:
2121
path: /status/200
2222
port: http
2323
resources: {}
24+
securityContext:
25+
runAsNonRoot: true
2426
---
2527
apiVersion: v1
2628
kind: Service

0 commit comments

Comments
 (0)