|
| 1 | +# Multi-stage Dockerfile for building ext-php-rs with musl libc |
| 2 | +# This ensures PHP and the Rust extension are both compiled against musl |
| 3 | + |
| 4 | +# Stage 1: Build PHP from source with musl |
| 5 | +FROM alpine:3.20 AS php-builder |
| 6 | + |
| 7 | +ARG PHP_VERSION=8.4.3 |
| 8 | +ARG PHP_BRANCH=PHP-${PHP_VERSION} |
| 9 | + |
| 10 | +# Install build dependencies |
| 11 | +RUN apk add --no-cache \ |
| 12 | + autoconf \ |
| 13 | + automake \ |
| 14 | + bison \ |
| 15 | + re2c \ |
| 16 | + gcc \ |
| 17 | + g++ \ |
| 18 | + make \ |
| 19 | + musl-dev \ |
| 20 | + linux-headers \ |
| 21 | + pkgconfig \ |
| 22 | + git \ |
| 23 | + libxml2-dev \ |
| 24 | + sqlite-dev \ |
| 25 | + openssl-dev \ |
| 26 | + curl-dev |
| 27 | + |
| 28 | +# Clone and build PHP with musl |
| 29 | +WORKDIR /tmp/php-src |
| 30 | +RUN git clone --depth 1 -b ${PHP_BRANCH} https://github.com/php/php-src.git . || \ |
| 31 | + git clone --depth 1 https://github.com/php/php-src.git . |
| 32 | + |
| 33 | +RUN ./buildconf --force && \ |
| 34 | + ./configure \ |
| 35 | + --enable-debug \ |
| 36 | + --enable-embed=shared \ |
| 37 | + --with-openssl \ |
| 38 | + --with-curl \ |
| 39 | + --with-sqlite3 \ |
| 40 | + --disable-cgi \ |
| 41 | + --prefix=/usr/local && \ |
| 42 | + make -j$(nproc) && \ |
| 43 | + make install |
| 44 | + |
| 45 | +# Stage 2: Build ext-php-rs |
| 46 | +FROM alpine:3.20 AS rust-builder |
| 47 | + |
| 48 | +# Copy PHP installation from previous stage |
| 49 | +COPY --from=php-builder /usr/local /usr/local |
| 50 | + |
| 51 | +# Install Rust and build dependencies |
| 52 | +RUN apk add --no-cache \ |
| 53 | + curl \ |
| 54 | + gcc \ |
| 55 | + g++ \ |
| 56 | + musl-dev \ |
| 57 | + clang17-dev \ |
| 58 | + clang17-static \ |
| 59 | + llvm17-dev \ |
| 60 | + make \ |
| 61 | + git |
| 62 | + |
| 63 | +# Install Rust |
| 64 | +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable |
| 65 | +ENV PATH="/root/.cargo/bin:${PATH}" |
| 66 | + |
| 67 | +# Add musl target |
| 68 | +RUN rustup target add x86_64-unknown-linux-musl |
| 69 | + |
| 70 | +# Set up environment for musl build |
| 71 | +ENV CC=gcc |
| 72 | +ENV LIBCLANG_PATH=/usr/lib/llvm17/lib |
| 73 | +ENV LLVM_CONFIG_PATH=/usr/bin/llvm-config |
| 74 | +ENV PHP=/usr/local/bin/php |
| 75 | +ENV PHP_CONFIG=/usr/local/bin/php-config |
| 76 | +ENV RUSTFLAGS="-C target-feature=-crt-static" |
| 77 | + |
| 78 | +# Create working directory |
| 79 | +WORKDIR /workspace |
| 80 | + |
| 81 | +# Copy source code |
| 82 | +COPY . . |
| 83 | + |
| 84 | +# Build the project |
| 85 | +RUN cargo build --release \ |
| 86 | + --features closure,anyhow,runtime \ |
| 87 | + --workspace \ |
| 88 | + --target x86_64-unknown-linux-musl |
| 89 | + |
| 90 | +# Stage 3: Runtime image with built artifacts |
| 91 | +FROM alpine:3.20 AS runtime |
| 92 | + |
| 93 | +# Copy PHP runtime |
| 94 | +COPY --from=php-builder /usr/local /usr/local |
| 95 | + |
| 96 | +# Copy built artifacts |
| 97 | +COPY --from=rust-builder /workspace/target/x86_64-unknown-linux-musl/release/cargo-php /usr/local/bin/ |
| 98 | + |
| 99 | +# Install runtime dependencies |
| 100 | +RUN apk add --no-cache \ |
| 101 | + libxml2 \ |
| 102 | + sqlite-libs \ |
| 103 | + openssl \ |
| 104 | + curl |
| 105 | + |
| 106 | +WORKDIR /app |
| 107 | + |
| 108 | +ENTRYPOINT ["/usr/local/bin/cargo-php"] |
| 109 | + |
| 110 | +# Stage 4: Builder image for CI usage |
| 111 | +FROM rust-builder AS ci-builder |
| 112 | + |
| 113 | +# This stage is used in CI to build and test |
| 114 | +# Usage: docker build --target ci-builder -t ext-php-rs-musl-ci . |
| 115 | +ENTRYPOINT ["cargo"] |
| 116 | +CMD ["build", "--release", "--features", "closure,anyhow,runtime", "--workspace", "--target", "x86_64-unknown-linux-musl"] |
0 commit comments