Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiplatform docker builds #1233

Merged
merged 4 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ xtask = "run --package xtask --"

[registries.crates-io]
protocol = "sparse"

# This is needed to enable cross-platform docker builds, as cargo doesn't use the correct linker sometimes:
# https://github.com/rust-lang/cargo/issues/4133
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,10 @@ jobs:
password: ${{ secrets.DOCKER_IO_READ_ONLY_TOKEN }}

- name: Build and push the image to ghcr.io
uses: docker/build-push-action@v2
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
file: deployment/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
Expand Down
21 changes: 17 additions & 4 deletions deployment/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# Stage 1: Build
FROM rust:1.68.1 AS chef
FROM --platform=$BUILDPLATFORM tonistiigi/xx AS xx
FROM --platform=$BUILDPLATFORM rust:1.69.0 AS chef

ARG TARGETPLATFORM
RUN cargo install cargo-chef
WORKDIR /build/

COPY --from=xx / /

# hadolint ignore=DL3008
RUN apt-get update && \
apt-get install -y --no-install-recommends \
lld \
clang \
libclang-dev \
protobuf-compiler \
&& xx-apt-get update \
&& xx-apt-get install -y libc6-dev g++ binutils \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*


FROM chef as planner
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
COPY . .
Expand All @@ -20,15 +29,19 @@ RUN cargo chef prepare --recipe-path recipe.json
FROM chef as builder
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
COPY --from=planner /build/recipe.json recipe.json

# Build our project dependecies, not our application!
RUN cargo chef cook --release --no-default-features --features "production" -p fuel-core-bin --recipe-path recipe.json
RUN xx-cargo chef cook --release --no-default-features --features "production" -p fuel-core-bin --recipe-path recipe.json
# Up to this point, if our dependency tree stays the same,
# all layers should be cached.
COPY . .
RUN cargo build --release --no-default-features --features "production" -p fuel-core-bin
RUN xx-cargo build --release --no-default-features --features "production" -p fuel-core-bin \
&& xx-verify ./target/$(xx-cargo --print-target-triple)/release/fuel-core \
&& mv ./target/$(xx-cargo --print-target-triple)/release/fuel-core ./target/release/fuel-core \
&& mv ./target/$(xx-cargo --print-target-triple)/release/fuel-core.d ./target/release/fuel-core.d
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to clean/remove ./target/$(xx-cargo --print-target-triple folder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, since the final docker image only copies the resultant binary over and leaves the target folder behind from the build stage image. It may even degrade the caching performance during the build stage if we remove it, but I haven't tested that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goooood


# Stage 2: Run
FROM ubuntu:22.04 as run
FROM --platform=$BUILDPLATFORM ubuntu:22.04 as run

ARG IP=0.0.0.0
ARG PORT=4000
Expand Down
Loading