Dockerfile 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. findutils \
  29. git \
  30. make \
  31. protoc \
  32. protobuf-dev
  33. WORKDIR /src
  34. ENV CGO_ENABLED=0
  35. FROM base AS build-base
  36. COPY go.* .
  37. RUN --mount=type=cache,target=/go/pkg/mod \
  38. --mount=type=cache,target=/root/.cache/go-build \
  39. go mod download
  40. FROM build-base AS vendored
  41. RUN --mount=type=bind,target=.,rw \
  42. --mount=type=cache,target=/go/pkg/mod \
  43. go mod tidy && mkdir /out && cp go.mod go.sum /out
  44. FROM scratch AS vendor-update
  45. COPY --from=vendored /out /
  46. FROM vendored AS vendor-validate
  47. RUN --mount=type=bind,target=.,rw <<EOT
  48. set -e
  49. git add -A
  50. cp -rf /out/* .
  51. diff=$(git status --porcelain -- go.mod go.sum)
  52. if [ -n "$diff" ]; then
  53. echo >&2 'ERROR: Vendor result differs. Please vendor your package with "make go-mod-tidy"'
  54. echo "$diff"
  55. exit 1
  56. fi
  57. EOT
  58. FROM build-base AS build
  59. ARG BUILD_TAGS
  60. ARG TARGETPLATFORM
  61. RUN xx-go --wrap
  62. RUN --mount=type=bind,target=. \
  63. --mount=type=cache,target=/root/.cache \
  64. --mount=type=cache,target=/go/pkg/mod \
  65. make build GO_BUILDTAGS="$BUILD_TAGS" DESTDIR=/usr/bin && \
  66. xx-verify --static /usr/bin/docker-compose
  67. FROM build-base AS lint
  68. ARG BUILD_TAGS
  69. RUN --mount=type=bind,target=. \
  70. --mount=type=cache,target=/root/.cache \
  71. --mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \
  72. golangci-lint run --build-tags "$BUILD_TAGS" ./...
  73. FROM build-base AS test
  74. ARG CGO_ENABLED=0
  75. ARG BUILD_TAGS
  76. RUN --mount=type=bind,target=. \
  77. --mount=type=cache,target=/root/.cache \
  78. --mount=type=cache,target=/go/pkg/mod \
  79. go test -tags "$BUILD_TAGS" -v -coverprofile=/tmp/coverage.txt -covermode=atomic $(go list $(TAGS) ./... | grep -vE 'e2e') && \
  80. go tool cover -func=/tmp/coverage.txt
  81. FROM scratch AS test-coverage
  82. COPY --from=test /tmp/coverage.txt /coverage.txt
  83. FROM base AS license-set
  84. ARG LICENSE_FILES
  85. RUN --mount=type=bind,target=.,rw \
  86. --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \
  87. find . -regex "${LICENSE_FILES}" | xargs addlicense -c 'Docker Compose CLI' -l apache && \
  88. mkdir /out && \
  89. find . -regex "${LICENSE_FILES}" | cpio -pdm /out
  90. FROM scratch AS license-update
  91. COPY --from=set /out /
  92. FROM base AS license-validate
  93. ARG LICENSE_FILES
  94. RUN --mount=type=bind,target=. \
  95. --mount=from=addlicense,source=/app/addlicense,target=/usr/bin/addlicense \
  96. find . -regex "${LICENSE_FILES}" | xargs addlicense -check -c 'Docker Compose CLI' -l apache -ignore validate -ignore testdata -ignore resolvepath -v
  97. FROM base AS docsgen
  98. WORKDIR /src
  99. RUN --mount=target=. \
  100. --mount=target=/root/.cache,type=cache \
  101. go build -o /out/docsgen ./docs/yaml/main/generate.go
  102. FROM --platform=${BUILDPLATFORM} alpine AS docs-build
  103. RUN apk add --no-cache rsync git
  104. WORKDIR /src
  105. COPY --from=docsgen /out/docsgen /usr/bin
  106. ARG DOCS_FORMATS
  107. RUN --mount=target=/context \
  108. --mount=target=.,type=tmpfs <<EOT
  109. set -e
  110. rsync -a /context/. .
  111. docsgen --formats "$DOCS_FORMATS" --source "docs/reference"
  112. mkdir /out
  113. cp -r docs/reference /out
  114. EOT
  115. FROM scratch AS docs-update
  116. COPY --from=docs-build /out /out
  117. FROM docs-build AS docs-validate
  118. RUN --mount=target=/context \
  119. --mount=target=.,type=tmpfs <<EOT
  120. set -e
  121. rsync -a /context/. .
  122. git add -A
  123. rm -rf docs/reference/*
  124. cp -rf /out/* ./docs/
  125. if [ -n "$(git status --porcelain -- docs/reference)" ]; then
  126. echo >&2 'ERROR: Docs result differs. Please update with "make docs"'
  127. git status --porcelain -- docs/reference
  128. exit 1
  129. fi
  130. EOT
  131. FROM scratch AS binary-unix
  132. COPY --link --from=build /usr/bin/docker-compose /
  133. FROM binary-unix AS binary-darwin
  134. FROM binary-unix AS binary-linux
  135. FROM scratch AS binary-windows
  136. COPY --link --from=build /usr/bin/docker-compose /docker-compose.exe
  137. FROM binary-$TARGETOS AS binary
  138. FROM --platform=$BUILDPLATFORM alpine AS releaser
  139. WORKDIR /work
  140. ARG TARGETOS
  141. ARG TARGETARCH
  142. ARG TARGETVARIANT
  143. RUN --mount=from=binary \
  144. mkdir -p /out && \
  145. # TODO: should just use standard arch
  146. TARGETARCH=$([ "$TARGETARCH" = "amd64" ] && echo "x86_64" || echo "$TARGETARCH"); \
  147. TARGETARCH=$([ "$TARGETARCH" = "arm64" ] && echo "aarch64" || echo "$TARGETARCH"); \
  148. cp docker-compose* "/out/docker-compose-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}$(ls docker-compose* | sed -e 's/^docker-compose//')"
  149. FROM scratch AS release
  150. COPY --from=releaser /out/ /
  151. # docs-reference is a target used as remote context to update docs on release
  152. # with latest changes on docs.docker.com.
  153. # see open-pr job in .github/workflows/docs.yml for more details
  154. FROM scratch AS docs-reference
  155. COPY docs/reference/*.yaml .