Skip to content

Commit 9e25085

Browse files
tac0turtleauricom
andauthored
chore: docker fixes (#2)
* docker fixes * bump rust version in docker * more fixes * more fixes * attempt * ci: fix remote cross compilation (#4) * only create images on push to main --------- Co-authored-by: auricom <27022259+auricom@users.noreply.github.com>
1 parent 019fb82 commit 9e25085

7 files changed

Lines changed: 210 additions & 21 deletions

File tree

.dockerignore

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Rust build artifacts
2+
target/
3+
**/*.rs.bk
4+
*.pdb
5+
6+
# Git
7+
.git/
8+
.gitignore
9+
10+
# IDE
11+
.idea/
12+
.vscode/
13+
*.swp
14+
*.swo
15+
*~
16+
17+
# OS
18+
.DS_Store
19+
Thumbs.db
20+
21+
# Documentation
22+
*.md
23+
!README.md
24+
!LICENSE-*
25+
26+
# Development files
27+
.env
28+
.env.local
29+
.env.*.local
30+
31+
# Test artifacts
32+
tarpaulin-report.html
33+
cobertura.xml
34+
35+
# Temporary files
36+
tmp/
37+
temp/
38+
*.tmp
39+
*.temp
40+
41+
# Docker
42+
.dockerignore
43+
Dockerfile*
44+
docker-compose*.yml
45+
46+
# CI/CD
47+
.github/
48+
.gitlab-ci.yml
49+
.travis.yml
50+
51+
# Benchmarks and profiling
52+
benches/
53+
perf.data
54+
perf.data.old
55+
flamegraph.svg

.github/workflows/docker.yml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ name: docker
55
on:
66
workflow_dispatch: {}
77
push:
8+
branches:
9+
- main
810
tags:
911
- v*
1012

@@ -24,6 +26,18 @@ jobs:
2426
contents: read
2527
steps:
2628
- uses: actions/checkout@v4
29+
- name: Install build dependencies
30+
run: |
31+
sudo apt-get update
32+
sudo apt-get install -y \
33+
build-essential \
34+
pkg-config \
35+
libssl-dev \
36+
libclang-dev \
37+
clang \
38+
llvm-dev \
39+
gcc-multilib \
40+
g++-multilib
2741
- uses: dtolnay/rust-toolchain@stable
2842
- uses: Swatinem/rust-cache@v2
2943
with:
@@ -37,4 +51,14 @@ jobs:
3751
docker run --privileged --rm tonistiigi/binfmt --install arm64,amd64
3852
docker buildx create --use --name cross-builder
3953
- name: Build and push image
40-
run: make docker-build-push
54+
run: |
55+
export LIBCLANG_PATH=/usr/lib/llvm-18/lib
56+
export BINDGEN_EXTRA_CLANG_ARGS="-I/usr/include"
57+
export CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc
58+
export CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++
59+
export AR_x86_64_unknown_linux_gnu=x86_64-linux-gnu-ar
60+
export CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
61+
export CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++
62+
export AR_aarch64_unknown_linux_gnu=aarch64-linux-gnu-ar
63+
export PKG_CONFIG_ALLOW_CROSS=1
64+
make docker-build-push

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,12 @@ too_long_first_doc_paragraph = "allow"
205205
opt-level = 3
206206
lto = "thin"
207207
strip = "debuginfo"
208+
codegen-units = 1
209+
210+
# Memory-optimized release profile
211+
[profile.docker]
212+
inherits = "release"
213+
opt-level = 2
214+
lto = false
215+
codegen-units = 1
216+
incremental = false

Cross.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[build]
2+
# Custom Docker images with proper build environment for native dependencies
3+
4+
[target.x86_64-unknown-linux-gnu]
5+
dockerfile = "Dockerfile.cross-x86_64"
6+
7+
[target.aarch64-unknown-linux-gnu]
8+
dockerfile = "Dockerfile.cross-aarch64"
9+
10+
[build.env]
11+
passthrough = [
12+
"RUST_LOG",
13+
"CARGO_TERM_COLOR",
14+
]

Dockerfile

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,73 @@
1-
# syntax=docker/dockerfile:1
1+
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
2+
WORKDIR /app
23

3-
# Build stage
4-
FROM rust:1.81-slim AS builder
4+
LABEL org.opencontainers.image.licenses="MIT OR Apache-2.0"
55

6-
# Install build dependencies
7-
RUN apt-get update && apt-get install -y \
6+
RUN apt-get update && \
7+
apt-get -y upgrade && \
8+
apt-get install -y \
9+
build-essential \
810
pkg-config \
911
libssl-dev \
10-
&& rm -rf /var/lib/apt/lists/*
12+
clang-14 \
13+
libclang-14-dev \
14+
llvm-14-dev \
15+
libc6-dev \
16+
&& ln -sf /usr/lib/llvm-14/lib/libclang.so /usr/lib/libclang.so
1117

12-
WORKDIR /app
18+
FROM chef AS planner
19+
COPY . .
20+
RUN cargo chef prepare --recipe-path recipe.json
21+
22+
FROM chef AS builder
23+
COPY --from=planner /app/recipe.json recipe.json
1324

14-
# Copy manifests
25+
# Copy workspace Cargo files for better caching
1526
COPY Cargo.toml Cargo.lock ./
16-
COPY bin/ bin/
17-
COPY crates/ crates/
27+
COPY bin/lumen/Cargo.toml bin/lumen/
28+
COPY crates/common/Cargo.toml crates/common/
29+
COPY crates/node/Cargo.toml crates/node/
30+
COPY crates/rollkit/Cargo.toml crates/rollkit/
31+
COPY crates/tests/Cargo.toml crates/tests/
32+
33+
ARG BUILD_PROFILE=docker
34+
ENV BUILD_PROFILE=$BUILD_PROFILE
35+
36+
# Set memory-efficient build flags
37+
ARG RUSTFLAGS="-C codegen-units=1"
38+
ENV RUSTFLAGS="$RUSTFLAGS"
39+
ENV CARGO_BUILD_JOBS=2
40+
ENV CARGO_INCREMENTAL=0
41+
42+
# Cook dependencies first (better layer caching)
43+
RUN cargo chef cook --profile $BUILD_PROFILE --recipe-path recipe.json --manifest-path bin/lumen/Cargo.toml
1844

19-
# Build the application
20-
RUN cargo build --release --bin lumen
45+
# Copy all source code
46+
COPY . .
2147

22-
# Runtime stage
23-
FROM gcr.io/distroless/cc-debian12
48+
# Build the binary with memory-efficient settings
49+
RUN cargo build --profile $BUILD_PROFILE --bin lumen --manifest-path bin/lumen/Cargo.toml -j 2
50+
51+
# Copy binary from correct location
52+
RUN ls -la /app/target/$BUILD_PROFILE/lumen
53+
RUN cp /app/target/$BUILD_PROFILE/lumen /lumen
54+
55+
FROM ubuntu:22.04 AS runtime
56+
57+
RUN apt-get update && \
58+
apt-get install -y ca-certificates libssl-dev pkg-config strace && \
59+
rm -rf /var/lib/apt/lists/*
60+
61+
WORKDIR /app
62+
COPY --from=builder /lumen /usr/local/bin/
63+
RUN chmod +x /usr/local/bin/lumen
64+
COPY LICENSE-* ./
2465

25-
# Copy the binary from builder
26-
COPY --from=builder /app/target/release/lumen /usr/local/bin/lumen
66+
# Expose ports: P2P, Discovery, Metrics, JSON-RPC, WebSocket, GraphQL, Engine API
67+
EXPOSE 30303 30303/udp 9001 8545 8546 7545 8551
2768

28-
# Expose default ports
29-
EXPOSE 8545 8546 30303 6060 9001
69+
# Add health check
70+
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
71+
CMD /usr/local/bin/lumen --version || exit 1
3072

31-
# Set the entrypoint
32-
ENTRYPOINT ["/usr/local/bin/lumen"]
73+
ENTRYPOINT ["/usr/local/bin/lumen"]

Dockerfile.cross-aarch64

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM ghcr.io/cross-rs/cross:main
2+
3+
# Install ARM64 cross-compilation toolchain
4+
RUN apt-get update && \
5+
apt-get install -y \
6+
gcc-aarch64-linux-gnu \
7+
g++-aarch64-linux-gnu \
8+
libc6-dev-arm64-cross \
9+
pkg-config-aarch64-linux-gnu \
10+
build-essential \
11+
clang \
12+
libclang-dev && \
13+
rm -rf /var/lib/apt/lists/*
14+
15+
# Set up environment variables for ARM64 cross-compilation
16+
# Keep host compiler for build scripts, only set target-specific variables
17+
ENV CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
18+
ENV CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++
19+
ENV AR_aarch64_unknown_linux_gnu=aarch64-linux-gnu-ar
20+
ENV STRIP_aarch64_unknown_linux_gnu=aarch64-linux-gnu-strip
21+
ENV PKG_CONFIG_aarch64_unknown_linux_gnu=aarch64-linux-gnu-pkg-config
22+
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
23+
ENV BINDGEN_EXTRA_CLANG_ARGS="-I/usr/aarch64-linux-gnu/include -I/usr/include"
24+
ENV CFLAGS_aarch64_unknown_linux_gnu="-I/usr/aarch64-linux-gnu/include -I/usr/include"
25+
ENV CPPFLAGS_aarch64_unknown_linux_gnu="-I/usr/aarch64-linux-gnu/include -I/usr/include"
26+
ENV JEMALLOC_SYS_WITH_LG_PAGE=16
27+
ENV PKG_CONFIG_ALLOW_CROSS=1

Dockerfile.cross-x86_64

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM ghcr.io/cross-rs/cross:main
2+
3+
# Install additional build tools and headers
4+
RUN apt-get update && \
5+
apt-get install -y \
6+
build-essential \
7+
pkg-config \
8+
libclang-dev \
9+
clang && \
10+
rm -rf /var/lib/apt/lists/*
11+
12+
# Set up environment variables for x86_64 cross-compilation
13+
ENV CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc
14+
ENV CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++
15+
ENV AR_x86_64_unknown_linux_gnu=x86_64-linux-gnu-ar
16+
ENV STRIP_x86_64_unknown_linux_gnu=x86_64-linux-gnu-strip
17+
ENV BINDGEN_EXTRA_CLANG_ARGS_x86_64_unknown_linux_gnu="-I/usr/include -I/usr/include/x86_64-linux-gnu"
18+
ENV CFLAGS_x86_64_unknown_linux_gnu="-I/usr/include -I/usr/include/x86_64-linux-gnu"
19+
ENV CPPFLAGS_x86_64_unknown_linux_gnu="-I/usr/include -I/usr/include/x86_64-linux-gnu"

0 commit comments

Comments
 (0)