Dockerfile 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # syntax=docker/dockerfile:1
  2. # Copyright 2020 Docker Compose CLI authors
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. ARG GO_VERSION=1.19.4
  13. ARG XX_VERSION=1.1.2
  14. ARG GOLANGCI_LINT_VERSION=v1.49.0
  15. ARG ADDLICENSE_VERSION=v1.0.0
  16. ARG BUILD_TAGS="e2e"
  17. ARG DOCS_FORMATS="md,yaml"
  18. ARG LICENSE_FILES=".*\(Dockerfile\|Makefile\|\.go\|\.hcl\|\.sh\)"
  19. # xx is a helper for cross-compilation
  20. FROM --platform=${BUILDPLATFORM} tonistiigi/xx:${XX_VERSION} AS xx
  21. FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint
  22. FROM ghcr.io/google/addlicense:${ADDLICENSE_VERSION} AS addlicense
  23. FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}-alpine AS base
  24. COPY --from=xx / /
  25. RUN apk add --no-cache \
  26. docker \
  27. file \
  28. git \
  29. make \
  30. protoc \
  31. protobuf-dev
  32. WORKDIR /src
  33. ENV CGO_ENABLED=0
  34. FROM base AS build-base
  35. COPY go.* .
  36. RUN --mount=type=cache,target=/go/pkg/mod \
  37. --mount=type=cache,target=/root/.cache/go-build \
  38. go mod download
  39. FROM build-base AS vendored
  40. RUN --mount=type=bind,target=.,rw \
  41. --mount=type=cache,target=/go/pkg/mod \
  42. go mod tidy && mkdir /out && cp go.mod go.sum /out
  43. FROM scratch AS vendor-update
  44. COPY --from=vendored /out /
  45. FROM vendored AS vendor-validate
  46. RUN --mount=type=bind,target=.,rw <<EOT
  47. set -e
  48. git add -A
  49. cp -rf /out/* .
  50. diff=$(git status --porcelain -- go.mod go.sum)
  51. if [ -n "$diff" ]; then
  52. echo >&2 'ERROR: Vendor result differs. Please vendor your package with "make go-mod-tidy"'
  53. echo "$diff"
  54. exit 1
  55. fi
  56. EOT
  57. FROM build-base AS build
  58. ARG BUILD_TAGS
  59. ARG TARGETPLATFORM
  60. RUN xx-go --wrap
  61. RUN --mount=type=bind,target=. \
  62. --mount=type=cache,target=/root/.cache \
  63. --mount=type=cache,target=/go/pkg/mod \
  64. make build GO_BUILDTAGS="$BUILD_TAGS" DESTDIR=/usr/bin && \
  65. xx-verify --static /usr/bin/docker-compose
  66. FROM build-base AS lint
  67. ARG BUILD_TAGS
  68. RUN --mount=type=bind,target=. \
  69. --mount=type=cache,target=/root/.cache \
  70. --mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \
  71. golangci-lint run --build-tags "$BUILD_TAGS" ./...
  72. FROM build-base AS test
  73. ARG CGO_ENABLED=0
  74. ARG BUILD_TAGS
  75. RUN --mount=type=bind,target=. \
  76. --mount=type=cache,target=/root/.cache \
  77. --mount=type=cache,target=/go/pkg/mod \
  78. go test -tags "$BUILD_TAGS" -v -coverprofile=/tmp/coverage.txt -covermode=atomic $(go list $(TAGS) ./... | grep -vE 'e2e') && \
  79. go tool cover -func=/tmp/coverage.txt
  80. FROM scratch AS test-coverage
  81. COPY --from=test /tmp/coverage.txt /coverage.txt
  82. FROM base AS license-set
  83. ARG LICENSE_FILES
  84. RUN --mount=type=bind,target=.,rw \
  85. --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \
  86. find . -regex "${LICENSE_FILES}" | xargs addlicense -c 'Docker Compose CLI' -l apache && \
  87. mkdir /out && \
  88. find . -regex "${LICENSE_FILES}" | cpio -pdm /out
  89. FROM scratch AS license-update
  90. COPY --from=set /out /
  91. FROM base AS license-validate
  92. ARG LICENSE_FILES
  93. RUN --mount=type=bind,target=. \
  94. --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \
  95. find . -regex "${LICENSE_FILES}" | xargs addlicense -check -c 'Docker Compose CLI' -l apache -ignore validate -ignore testdata -ignore resolvepath -v
  96. FROM base AS docsgen
  97. WORKDIR /src
  98. RUN --mount=target=. \
  99. --mount=target=/root/.cache,type=cache \
  100. go build -o /out/docsgen ./docs/yaml/main/generate.go
  101. FROM --platform=${BUILDPLATFORM} alpine AS docs-build
  102. RUN apk add --no-cache rsync git
  103. WORKDIR /src
  104. COPY --from=docsgen /out/docsgen /usr/bin
  105. ARG DOCS_FORMATS
  106. RUN --mount=target=/context \
  107. --mount=target=.,type=tmpfs <<EOT
  108. set -e
  109. rsync -a /context/. .
  110. docsgen --formats "$DOCS_FORMATS" --source "docs/reference"
  111. mkdir /out
  112. cp -r docs/reference /out
  113. EOT
  114. FROM scratch AS docs-update
  115. COPY --from=docs-build /out /out
  116. FROM docs-build AS docs-validate
  117. RUN --mount=target=/context \
  118. --mount=target=.,type=tmpfs <<EOT
  119. set -e
  120. rsync -a /context/. .
  121. git add -A
  122. rm -rf docs/reference/*
  123. cp -rf /out/* ./docs/
  124. if [ -n "$(git status --porcelain -- docs/reference)" ]; then
  125. echo >&2 'ERROR: Docs result differs. Please update with "make docs"'
  126. git status --porcelain -- docs/reference
  127. exit 1
  128. fi
  129. EOT
  130. FROM scratch AS binary-unix
  131. COPY --link --from=build /usr/bin/docker-compose /
  132. FROM binary-unix AS binary-darwin
  133. FROM binary-unix AS binary-linux
  134. FROM scratch AS binary-windows
  135. COPY --link --from=build /usr/bin/docker-compose /docker-compose.exe
  136. FROM binary-$TARGETOS AS binary
  137. FROM --platform=$BUILDPLATFORM alpine AS releaser
  138. WORKDIR /work
  139. ARG TARGETOS
  140. ARG TARGETARCH
  141. ARG TARGETVARIANT
  142. RUN --mount=from=binary \
  143. mkdir -p /out && \
  144. # TODO: should just use standard arch
  145. TARGETARCH=$([ "$TARGETARCH" = "amd64" ] && echo "x86_64" || echo "$TARGETARCH"); \
  146. TARGETARCH=$([ "$TARGETARCH" = "arm64" ] && echo "aarch64" || echo "$TARGETARCH"); \
  147. cp docker-compose* "/out/docker-compose-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}$(ls docker-compose* | sed -e 's/^docker-compose//')"
  148. FROM scratch AS release
  149. COPY --from=releaser /out/ /
  150. # docs-reference is a target used as remote context to update docs on release
  151. # with latest changes on docs.docker.com.
  152. # see open-pr job in .github/workflows/docs.yml for more details
  153. FROM scratch AS docs-reference
  154. COPY docs/reference/*.yaml .