Skip to content

Commit 8da53e0

Browse files
committed
Code review from @RomainMuller
1 parent 80e28a6 commit 8da53e0

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

content/en/security/application_security/setup/go/dockerfile.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ further_reading:
1717

1818
# Introduction
1919

20-
App and API Protection for Go installation requirements can be abstract and the Go toolchain
21-
cross-compilation capabilities can make it hard to understand what has to be done precisely.
20+
App and API Protection for Go installation requirements can be abstract. Moreover the Go toolchain
21+
cross-compilation and CGO capabilities can make it hard to understand what has to be done precisely.
2222

2323
In these cases, a more precise way to materialize these examples like a Dockerfile can be interesting.
2424
The goal of this guide is to be a step-by-step guide to a working Dockerfile, tailor-fitted for your usecase.
@@ -40,37 +40,35 @@ FROM golang:1 AS build
4040
WORKDIR /app
4141
COPY . .
4242

43-
RUN go install github.com/DataDog/orchestrion@latest && \
44-
orchestrion pin
43+
RUN go install github.com/DataDog/orchestrion # Resolved from go.mod dependencies
4544

4645
RUN orchestrion go build -o main .
4746

48-
FROM ubuntu:noble
47+
FROM debian:bookworm
4948
COPY --from=build /app/main /usr/local/bin
5049

5150
ENV DD_APPSEC_ENABLED=true
5251
ENTRYPOINT [ "/usr/local/bin/main" ]
5352
```
5453

55-
This constitutes the simplest version of a working Dockerfile for a Go application with Datadog's WAF enabled.
56-
It would be better to run `orchestrion pin` ahead of time and commit it to your VCS for future builds, but this is not a strict requirement.
54+
This constitutes the simplest version of a working Dockerfile for a Go application with Datadog's WAF enabled. If this is your first use of [Orchestrion][5]. This dockerfile requires to run `orchestrion pin` beforehand and commit the resulting changes for it to work. Please refer to our [Getting Started for Go][-]
5755

5856
This Dockerfile is split into two stages:
5957
1. The build stage builds from an Debian image the Go application using the [Orchestrion][5] tool to instrument it with App and API Protection features.
6058
2. The runtime stage copies the built application into a minimal Ubuntu image and sets the environment variable `DD_APPSEC_ENABLED` to `true` to enable App and API Protection.
6159

62-
This two-stage build process is beneficial because it allows you to keep the final image small and free of unnecessary build tools,
63-
while still ensuring that your application is instrumented correctly for App and API Protection.
60+
This two-stage build process is beneficial because it allows you to keep the final image small and free of unnecessary build tools.
61+
While still ensuring that your application is instrumented correctly for App and API Protection.
6462

6563
The following sections show different Dockerfile scenarios, each with their specific considerations and complete examples.
6664

6765
## Dockerfile scenarios
6866

6967
Two main dimensions impact your Dockerfile choice for App and API Protection:
7068
* **libc implementation**: glibc (Debian/Ubuntu) or musl (Alpine)
71-
* **CGO**: enabled (default) or disabled (`CGO_ENABLED=0`)
69+
* **CGO**: enabled or disabled (with the env var `CGO_ENABLED`).
7270

73-
These dimensions affect both build requirements and runtime compatibility. The Datadog WAF requires specific shared libraries (`libc.so.6` and `libpthread.so.0`) at runtime, and the build approach varies depending on these choices. When CGO is enabled, those libraries will always be required so the Datadog WAF can be baked in without issue. But CGO being disabled is often synonymous with no shared library, which cannot be the case out-of-the-box for Datadog WAF. This is why, by default, when CGO is disabled, the `-tags appsec` flag need to be passed.
71+
These dimensions affect both build requirements and runtime compatibility. The Datadog WAF requires specific shared libraries (`libc.so.6` and `libpthread.so.0`) at runtime, and the build approach varies depending on these choices. Those dependencies are required by all programs built with CGO enabled, so the Datadog WAF will work out-of-the-box on runtime environments that support such programs. When CGO is disabled however, Go usually produces a fully statically linked binary that does not require these libraries but this is not true when using the Datadog WAF, which is why when CGO is disabled, the `-tags=appsec` flag needs to be passed in order for the Datadog WAF to be enabled.
7472

7573
### Standard glibc-based Dockerfile
7674

@@ -82,7 +80,7 @@ FROM golang:1 AS build
8280
WORKDIR /app
8381
COPY . .
8482

85-
RUN go install github.com/DataDog/orchestrion@latest
83+
RUN go install github.com/DataDog/orchestrion
8684
RUN orchestrion go build -o main .
8785

8886
FROM ubuntu:noble
@@ -108,7 +106,7 @@ FROM golang:1 AS build
108106
WORKDIR /app
109107
COPY . .
110108

111-
RUN go install github.com/DataDog/orchestrion@latest
109+
RUN go install github.com/DataDog/orchestrion
112110
RUN orchestrion go build -o main .
113111

114112
FROM alpine
@@ -134,7 +132,7 @@ FROM golang:1-alpine AS build
134132
WORKDIR /app
135133
COPY . .
136134

137-
RUN go install github.com/DataDog/orchestrion@latest
135+
RUN go install github.com/DataDog/orchestrion
138136
RUN orchestrion go build -tags appsec -o main .
139137

140138
FROM alpine
@@ -157,7 +155,7 @@ FROM golang:1 AS build
157155
WORKDIR /app
158156
COPY . .
159157

160-
RUN go install github.com/DataDog/orchestrion@latest
158+
RUN go install github.com/DataDog/orchestrion
161159

162160
# Build with appsec tag for CGO-disabled builds
163161
ENV CGO_ENABLED=0
@@ -182,14 +180,14 @@ ENTRYPOINT [ "/main" ]
182180

183181
### Distroless Dockerfile
184182

185-
For security-focused deployments using Google's distroless images:
183+
For security-focused deployments using [Google's distroless][7] images:
186184

187185
```dockerfile
188186
FROM golang:1 AS build
189187
WORKDIR /app
190188
COPY . .
191189

192-
RUN go install github.com/DataDog/orchestrion@latest
190+
RUN go install github.com/DataDog/orchestrion
193191

194192
ENV CGO_ENABLED=0
195193
RUN orchestrion go build -tags appsec -o main .
@@ -223,7 +221,7 @@ RUN apt-get update && apt-get install -y gcc-aarch64-linux-gnu
223221
WORKDIR /app
224222
COPY . .
225223

226-
RUN go install github.com/DataDog/orchestrion@latest
224+
RUN go install github.com/DataDog/orchestrion
227225

228226
# Cross-compile for ARM64
229227
ENV CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOOS=linux GOARCH=arm64
@@ -241,9 +239,9 @@ ENTRYPOINT [ "/usr/local/bin/main" ]
241239
* The runtime stage must match the target architecture
242240
* CGO must be enabled for proper WAF integration
243241

244-
## Run your application
242+
## Try it out
245243

246-
Now that the docker image is ready, run [appsec-go-test-app][4]:
244+
Most of these Dockerfiles are availabe in [appsec-go-test-app][4], trying them out is very easy:
247245

248246
```sh
249247
docker build -f ./examples/alpine/Dockerfile -t appsec-go-test-app .
@@ -260,3 +258,5 @@ docker run appsec-go-test-app
260258
[3]: https://github.com/DataDog/appsec-go-test-app/blob/main/examples/docker
261259
[4]: https://github.com/DataDog/appsec-go-test-app
262260
[5]: /tracing/trace_collection/automatic_instrumentation/dd_libraries/go/?tab=compiletimeinstrumentation
261+
[6]: /security/application_security/setup/go/setup
262+
[7]: https://github.com/GoogleContainerTools/distroless

0 commit comments

Comments
 (0)