Dockerfile 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. FROM golang:latest as build-env
  2. RUN mkdir /app
  3. WORKDIR /app
  4. COPY go.mod .
  5. COPY go.sum .
  6. # This ensures that CGO is disabled for go test running AND for the build
  7. # step. This prevents a build failure when building an ARM64 image with
  8. # docker buildx. I believe this is because the ARM64 version of the
  9. # golang:latest image does not contain GCC, while the AMD64 version does.
  10. ARG CGO_ENABLED=0
  11. ARG version=dev
  12. ARG commit=HEAD
  13. # COPY the source code as the last step
  14. COPY . .
  15. # Get dependencies - will also be cached if we won't change mod/sum
  16. RUN go mod download
  17. # Build the binary
  18. RUN set -e ;\
  19. go test ./test/*.go;\
  20. go test ./pkg/*;\
  21. cd cmd/costmodel;\
  22. GOOS=linux \
  23. go build -a -installsuffix cgo \
  24. -ldflags \
  25. "-X github.com/opencost/opencost/core/pkg/version.Version=${version} \
  26. -X github.com/opencost/opencost/core/pkg/version.GitCommit=${commit}" \
  27. -o /go/bin/app
  28. FROM alpine:latest
  29. RUN apk add --update --no-cache ca-certificates
  30. COPY --from=build-env /go/bin/app /go/bin/app
  31. ADD --chmod=644 ./configs/default.json /models/default.json
  32. ADD --chmod=644 ./configs/azure.json /models/azure.json
  33. ADD --chmod=644 ./configs/aws.json /models/aws.json
  34. ADD --chmod=644 ./configs/gcp.json /models/gcp.json
  35. ADD --chmod=644 ./configs/alibaba.json /models/alibaba.json
  36. ADD --chmod=644 ./configs/oracle.json /models/oracle.json
  37. USER 1001
  38. ENTRYPOINT ["/go/bin/app"]