Skip to content

Commit

Permalink
Fix dockerfile cross compile (#7286)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nopcoder authored Jan 15, 2024
1 parent 3ff0c53 commit 5a4003e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 129 deletions.
27 changes: 17 additions & 10 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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/
13 changes: 4 additions & 9 deletions .github/workflows/docker-publish-exp-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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 }}
46 changes: 24 additions & 22 deletions .github/workflows/docker-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
102 changes: 14 additions & 88 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 <arch>-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"]

0 comments on commit 5a4003e

Please sign in to comment.