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

Use multi-stage for building docker images #3243

Open
wants to merge 2 commits into
base: albatross
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .dockerignore
118 changes: 77 additions & 41 deletions .github/workflows/github_release_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,20 @@ on:
workflow_dispatch:

jobs:
rust_build:
name: Build Nimiq with release flag
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Build binaries in release mode
run: |
cargo build --release
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
name: nimiq-dist
path: |
target/release/nimiq-client
target/release/nimiq-bls
target/release/nimiq-address
target/release/nimiq-rpc

build_docker:
runs-on: ubuntu-22.04
runs-on: ${{ matrix.runs-on }}
name: Build & publish docker image
needs: rust_build
strategy:
fail-fast: false
matrix:
runs-on:
- ubuntu-24.04
- ubuntu-24.04-arm
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Download nimiq client artifact from previous job
uses: actions/download-artifact@v4
with:
name: nimiq-dist
path: target/release/

- name: Run some commands
run: |
ls target/release/ -al
chmod -R +x target/release/
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
Expand All @@ -53,26 +29,86 @@ jobs:
flavor: latest=true

- name: Login to image repository
if: github.ref_type == 'tag'
# if: github.ref_type == 'tag'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
- name: Build and push by digest
id: build
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355
with:
context: .
file: Dockerfile
push: ${{ github.ref_type == 'tag' }}
# This is required or a package with unknown architecture will be published too.
# See https://github.com/docker/build-push-action/issues/820 for further
# details.
# TODO - investigate further and see if we can find a solution where we
# don't have to set this.
provenance: false
tags: ${{ steps.meta.outputs.tags }}
# push: ${{ github.ref_type == 'tag' }}
push: true
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha
outputs: type=image,"name=ghcr.io/${{ github.repository }}",push-by-digest=true,name-canonical=true,push=true

- name: Export digest
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.build.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"

- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1

merge:
#if: ${{ github.ref_type == 'tag' }}
runs-on: ubuntu-latest
needs:
- build_docker
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true

- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
flavor: latest=true
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}

- name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.DOCKERHUB_REPO }}@sha256:%s ' *)
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.GHCR_REPO }}@sha256:%s ' *)

- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.DOCKERHUB_REPO }}:${{ steps.meta.outputs.version }}
docker buildx imagetools inspect ${{ env.GHCR_REPO }}:${{ steps.meta.outputs.version }}


31 changes: 24 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
FROM ubuntu:22.04
FROM ubuntu:24.04 AS build

# Install build dependencies in a single layer to reduce the number of image layers.
RUN apt-get update && \
apt-get --no-install-recommends -y install ca-certificates clang cmake curl git libssl-dev pkg-config && \
rm -rf /var/lib/apt/lists/*

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

# Compile the code
WORKDIR /root
COPY ./ core-rs-albatross
RUN cd core-rs-albatross && \
cargo build --release

FROM ubuntu:24.04

# Install dependencies in a single layer to reduce the number of image layers.
RUN apt-get update && \
Expand All @@ -7,7 +24,7 @@ RUN apt-get update && \

# Run as an unprivileged user, combining commands to reduce layers.
RUN groupadd --system --gid 1001 nimiq && \
adduser --system --home /home/nimiq --uid 1001 --gid 1001 nimiq
useradd --system --home /home/nimiq --uid 1001 --gid 1001 nimiq

# Switch to the unprivileged user and set working directory in one layer.
USER nimiq
Expand All @@ -17,11 +34,11 @@ WORKDIR /home/nimiq
RUN mkdir -p /home/nimiq/.nimiq

# Copy configuration file and binaries in one command to improve caching.
COPY ./lib/src/config/config_file/client.example.toml /home/nimiq/.nimiq/client.toml
COPY ./target/release/nimiq-client \
./target/release/nimiq-bls \
./target/release/nimiq-address \
./target/release/nimiq-rpc /usr/local/bin/
COPY --from=build /root/core-rs-albatross/lib/src/config/config_file/client.example.toml /home/nimiq/.nimiq/client.toml
COPY --from=build /root/core-rs-albatross/target/release/nimiq-client \
/root/core-rs-albatross/target/release/nimiq-bls \
/root/core-rs-albatross/target/release/nimiq-address \
/root/core-rs-albatross/target/release/nimiq-rpc /usr/local/bin/

# Expose the necessary ports
EXPOSE 8443 8648 9100
Expand Down
Loading