From 5a4003e3c67f5a5084fe9cdc86b24d2fb61071d7 Mon Sep 17 00:00:00 2001 From: Barak Amar Date: Mon, 15 Jan 2024 14:18:24 +0200 Subject: [PATCH] Fix dockerfile cross compile (#7286) * dockerfile fix cross compile and reduce size * limit platforms to linux amd64 and arm64 * remove darwin{amd64,arm64} as alpine base doesn't have one * Fix apps in path --- .dockerignore | 27 +++-- .../workflows/docker-publish-exp-image.yaml | 13 +-- .github/workflows/docker-publish.yaml | 46 ++++---- Dockerfile | 102 +++--------------- 4 files changed, 59 insertions(+), 129 deletions(-) diff --git a/.dockerignore b/.dockerignore index 32f3f6ed032..5446d48f6fa 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,15 +1,22 @@ -Makefile -cover.out -README.md -.github/ .git/ +.github/ .gitignore -lakefs -lakectl -cmd/lakefs/lakefs -cmd/lakectl/lakefs -test/ +.idea +Makefile +README.md clients/ -docs/ +cmd/lakectl/lakefs +cmd/lakefs/lakefs +cover.out design/ +docs/ +esti/ +examples/ +lakectl +lakefs pkg/gateway/testdata/ +target/ +test/ +venv/ +webui/node_modules/ +webui/src/ diff --git a/.github/workflows/docker-publish-exp-image.yaml b/.github/workflows/docker-publish-exp-image.yaml index 67c373842f9..cafc4edbade 100644 --- a/.github/workflows/docker-publish-exp-image.yaml +++ b/.github/workflows/docker-publish-exp-image.yaml @@ -6,14 +6,11 @@ on: required: true description: The suffix of the published docker image. target: - default: 'lakefs-plugins' + default: 'lakefs' type: choice options: - - lakefs-plugins - lakefs - lakectl - - build-delta-diff-plugin - - build # These permissions are needed to interact with GitHub's OIDC Token endpoint. permissions: @@ -61,12 +58,10 @@ jobs: GOLANGCI_LINT_FLAGS: --out-format github-actions - name: Set up QEMU - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - with: - buildkitd-flags: --debug + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub uses: docker/login-action@v2 @@ -80,7 +75,7 @@ jobs: context: . target: ${{ inputs.target }} push: true - platforms: linux/amd64,linux/arm64,darwin/amd64,darwin/arm64 + platforms: linux/amd64,linux/arm64 build-args: VERSION=${{ steps.version.outputs.tag }} tags: | treeverse/experimental-lakefs:${{ steps.version.outputs.tag }} diff --git a/.github/workflows/docker-publish.yaml b/.github/workflows/docker-publish.yaml index 711c685a079..f915c9c1c95 100644 --- a/.github/workflows/docker-publish.yaml +++ b/.github/workflows/docker-publish.yaml @@ -62,12 +62,10 @@ jobs: role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} - name: Set up QEMU - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - with: - buildkitd-flags: --debug + uses: docker/setup-buildx-action@v3 - name: Login to Amazon ECR id: login-ecr @@ -78,44 +76,48 @@ jobs: with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: lakectl docker metadata - id: lakectl_meta + + - name: lakefs docker metadata + id: lakefs_meta uses: docker/metadata-action@v5 with: images: | - treeverse/lakectl - ${{ steps.login-ecr.outputs.registry }}/lakectl + treeverse/lakefs + ${{ steps.login-ecr.outputs.registry }}/lakefs tags: | type=semver,pattern={{version}},value=${{ steps.version.outputs.tag }} type=semver,pattern={{major}}.{{minor}},value=${{ steps.version.outputs.tag }} type=semver,pattern={{major}},value=${{ steps.version.outputs.tag }} - - name: lakefs docker metadata - id: lakefs_meta + + - name: Build and push lakefs + uses: docker/build-push-action@v5 + with: + context: . + target: lakefs + push: true + platforms: linux/amd64,linux/arm64 + build-args: VERSION=${{ steps.version.outputs.tag }} + tags: ${{ steps.lakefs_meta.outputs.tags }} + + - name: lakectl docker metadata + id: lakectl_meta uses: docker/metadata-action@v5 with: images: | - treeverse/lakefs - ${{ steps.login-ecr.outputs.registry }}/lakefs + treeverse/lakectl + ${{ steps.login-ecr.outputs.registry }}/lakectl tags: | type=semver,pattern={{version}},value=${{ steps.version.outputs.tag }} type=semver,pattern={{major}}.{{minor}},value=${{ steps.version.outputs.tag }} type=semver,pattern={{major}},value=${{ steps.version.outputs.tag }} + - name: Build and push lakectl uses: docker/build-push-action@v5 with: context: . target: lakectl push: true - platforms: linux/amd64,linux/arm64,darwin/amd64,darwin/arm64 + platforms: linux/amd64,linux/arm64 build-args: VERSION=${{ steps.version.outputs.tag }} tags: ${{ steps.lakectl_meta.outputs.tags }} - - name: Build and push lakefs - uses: docker/build-push-action@v5 - with: - context: . - target: lakefs-plugins - push: true - platforms: linux/amd64,linux/arm64,darwin/amd64,darwin/arm64 - build-args: VERSION=${{ steps.version.outputs.tag }} - tags: ${{ steps.lakefs_meta.outputs.tags }} diff --git a/Dockerfile b/Dockerfile index 4aa8c53afed..89d773a578e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,114 +1,40 @@ -# Build lakeFS -FROM --platform=$BUILDPLATFORM golang:1.21.4-alpine3.18 AS build - +# syntax=docker/dockerfile:1 ARG VERSION=dev +FROM --platform=$BUILDPLATFORM golang:1.21.4-alpine3.18 AS build WORKDIR /build - -# Packages required to build -RUN apk add --no-cache build-base - -# Copy project deps first since they don't change often +RUN apk add --no-cache build-base ca-certificates COPY go.mod go.sum ./ RUN --mount=type=cache,target=/go/pkg go mod download - -# Copy project COPY . ./ -# Build a binaries -ARG TARGETOS TARGETARCH +FROM build as build-lakefs +ARG VERSION TARGETOS TARGETARCH RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg \ GOOS=$TARGETOS GOARCH=$TARGETARCH \ go build -ldflags "-X github.com/treeverse/lakefs/pkg/version.Version=${VERSION}" -o lakefs ./cmd/lakefs + +FROM build as build-lakectl +ARG VERSION TARGETOS TARGETARCH RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg \ GOOS=$TARGETOS GOARCH=$TARGETARCH \ go build -ldflags "-X github.com/treeverse/lakefs/pkg/version.Version=${VERSION}" -o lakectl ./cmd/lakectl - -# Build delta diff binary -FROM --platform=$BUILDPLATFORM rust:1.71-alpine3.18 AS build-delta-diff-plugin -RUN apk update && apk add build-base pkgconfig openssl-dev alpine-sdk -RUN cargo new --bin delta-diff -WORKDIR /delta-diff - -# 2. Copy our manifests -COPY ./pkg/plugins/diff/delta_diff_server/Cargo.lock ./ -COPY ./pkg/plugins/diff/delta_diff_server/Cargo.toml ./ - -# 3. Build only the dependencies to cache them in this layer - -# Rust default behavior is to build a static binary (default target is -unknown-linux-musl on Alpine, and musl -# is assumed to be static). It links to openssl statically, but these are dynamic libraries. Setting RUSTFLAGS=-Ctarget-feature=-crt-static -# forces Rust to create a dynamic binary, despite asking for musl. -RUN RUSTFLAGS=-Ctarget-feature=-crt-static cargo build --release -RUN rm src/*.rs - -# 4. Now that the dependency is built, copy your source code -COPY ./pkg/plugins/diff/delta_diff_server/src ./src - -# 5. Build for release. -RUN rm ./target/release/deps/delta_diff* -RUN RUSTFLAGS=-Ctarget-feature=-crt-static cargo build --release - -# Just lakectl -FROM --platform=$BUILDPLATFORM alpine:3.18 AS lakectl - -RUN apk add -U --no-cache ca-certificates - +FROM alpine:3.18 AS lakectl WORKDIR /app ENV PATH /app:$PATH -COPY --from=build /build/lakectl ./ +COPY --from=build-lakectl /build/lakectl /app/ +RUN apk add -U --no-cache ca-certificates RUN addgroup -S lakefs && adduser -S lakefs -G lakefs USER lakefs WORKDIR /home/lakefs ENTRYPOINT ["/app/lakectl"] -# lakefs with lakectl -FROM --platform=$BUILDPLATFORM alpine:3.18 AS lakefs - -RUN apk add -U --no-cache ca-certificates netcat-openbsd - -WORKDIR /app -COPY ./scripts/wait-for ./ -ENV PATH /app:$PATH -COPY --from=build /build/lakefs /build/lakectl ./ - +FROM lakectl AS lakefs +COPY ./scripts/wait-for /app/ +COPY --from=build-lakefs /build/lakefs /app/ EXPOSE 8000/tcp - -# Setup user -RUN addgroup -S lakefs && adduser -S lakefs -G lakefs -USER lakefs -WORKDIR /home/lakefs - -ENTRYPOINT ["/app/lakefs"] -CMD ["run"] - -# Include lakefs-plugins -FROM --platform=$BUILDPLATFORM alpine:3.18 AS lakefs-plugins - -RUN apk add -U --no-cache \ - alpine-sdk \ - ca-certificates \ - libc6-compat \ - netcat-openbsd \ - openssl-dev - -WORKDIR /app -COPY ./scripts/wait-for ./ -ENV PATH /app:$PATH -COPY --from=build /build/lakefs /build/lakectl ./ -COPY --from=build-delta-diff-plugin /delta-diff/target/release/delta_diff ./ - -EXPOSE 8000/tcp - -# Setup user -RUN addgroup -S lakefs && adduser -S lakefs -G lakefs -USER lakefs -WORKDIR /home/lakefs - -RUN mkdir -p /home/lakefs/.lakefs/plugins/diff && ln -s /app/delta_diff /home/lakefs/.lakefs/plugins/diff/delta - ENTRYPOINT ["/app/lakefs"] CMD ["run"]