build docker image with cargo chef cache (#309)

This commit is contained in:
QP Hou 2023-11-19 11:31:39 -08:00 committed by GitHub
parent a322d1b443
commit b2454f48e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 8 deletions

View File

@ -300,13 +300,17 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56
with:
context: .
push: false
load: true
tags: roapi:latest
cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/roapi:buildcache
- name: Test
run: |
docker run --rm roapi:latest --help
docker run --rm roapi:latest --version

View File

@ -279,3 +279,5 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/roapi:buildcache
cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/roapi:buildcache,mode=max

View File

@ -1,18 +1,51 @@
FROM instrumentisto/rust:nightly-bullseye-2023-09-15 AS builder
WORKDIR /roapi_src
COPY ./ /roapi_src
ARG RUST_VER=nightly-bullseye-2023-09-15
ARG RUSTFLAGS='-C target-cpu=skylake'
ARG FEATURES="simd,database"
# Step 0: Install cargo-chef
FROM instrumentisto/rust:${RUST_VER} AS chef
# We only pay the installation cost once,
# it will be cached from the second build onwards
RUN cargo +nightly install cargo-chef
# install cmake for snmalloc
RUN apt-get update \
&& apt-get install --no-install-recommends -y cmake
RUN RUSTFLAGS='-C target-cpu=skylake' \
cargo +nightly install --locked --bins roapi --features "simd database" --path roapi
# Step 1: Compute a recipe file
FROM chef AS planner
WORKDIR /roapi_src
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
# Step 2: Cache project dependencies
FROM chef AS cacher
ARG RUSTFLAGS
ARG FEATURES
WORKDIR /roapi_src
COPY --from=planner /roapi_src/recipe.json recipe.json
RUN RUSTFLAGS=${RUSTFLAGS} \
cargo +nightly chef cook --features ${FEATURES} --release --recipe-path recipe.json
# Step 3: Build the release binary
FROM chef AS builder
ARG RUSTFLAGS
ARG FEATURES
WORKDIR /roapi_src
COPY ./ /roapi_src
COPY --from=cacher /roapi_src/target target
COPY --from=cacher /usr/local/cargo /usr/local/cargo
RUN RUSTFLAGS=${RUSTFLAGS} \
cargo +nightly build --release --locked --bin roapi --features ${FEATURES}
# Step 4: Assemble the final image
FROM debian:bullseye-slim
LABEL org.opencontainers.image.source https://github.com/roapi/roapi
RUN apt-get update && apt-get install -y libssl-dev ca-certificates && rm -rf /var/lib/apt/lists/*
RUN apt-get update \
&& apt-get install -y libssl-dev ca-certificates \
&& rm -rf /var/lib/apt/lists/*
COPY test_data /test_data
COPY --from=builder /usr/local/cargo/bin/roapi /usr/local/bin/roapi
COPY --from=builder /roapi_src/target/release/roapi /usr/local/bin/roapi
EXPOSE 8080
ENTRYPOINT ["roapi"]