Skip to content

Commit

Permalink
feat: Create Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjjaramillo committed May 6, 2024
1 parent 6527c02 commit ad8fff8
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
# Build the manager binary
FROM golang:1.22 as builder
ARG TARGETOS
ARG TARGETARCH

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
# syntax=docker/dockerfile:1

FROM --platform=$BUILDPLATFORM golang:1.22.2@sha256:450e3822c7a135e1463cd83e51c8e2eb03b86a02113c89424e6f0f8344bb4168 AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

# Copy the go source
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/ internal/
COPY pkg/ pkg/
# COPY versions.txt versions.txt

ARG VERSION_PKG
ARG VERSION
ARG VERSION_DATE
ARG NEWRELIC_INSTRUMENTATION_JAVA_VERSION
ARG NEWRELIC_INSTRUMENTATION_NODEJS_VERSION
ARG NEWRELIC_INSTRUMENTATION_PYTHON_VERSION
ARG NEWRELIC_INSTRUMENTATION_DOTNET_VERSION
ARG NEWRELIC_INSTRUMENTATION_PHP_VERSION
ARG AUTO_INSTRUMENTATION_GO_VERSION
# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags="-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.autoInstrumentationJava=${NEWRELIC_INSTRUMENTATION_JAVA_VERSION} -X ${VERSION_PKG}.autoInstrumentationNodeJS=${NEWRELIC_INSTRUMENTATION_NODEJS_VERSION} -X ${VERSION_PKG}.autoInstrumentationPython=${NEWRELIC_INSTRUMENTATION_PYTHON_VERSION} -X ${VERSION_PKG}.autoInstrumentationDotNet=${NEWRELIC_INSTRUMENTATION_DOTNET_VERSION} -X ${VERSION_PKG}.autoInstrumentationPhp=${NEWRELIC_INSTRUMENTATION_PHP_VERSION}" -a -o manager cmd/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
COPY ./src/ ./src/
COPY Makefile .

ARG TARGETOS TARGETARCH
ARG GOOS=$TARGETOS
ARG GOARCH=$TARGETARCH

RUN make build

# Use minimal base image to package the operator
# Source: https://github.com/GoogleContainerTools/distroless
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
WORKDIR /bin
COPY --from=builder /app/bin/operator .
USER 65532:65532

ENTRYPOINT ["/manager"]
ENTRYPOINT ["/bin/operator"]

0 comments on commit ad8fff8

Please sign in to comment.