Skip to content

Commit f5cabdc

Browse files
committed
multistage build for grpc
1 parent 8e56a47 commit f5cabdc

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

Dockerfile

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1+
FROM php:8.5-cli AS builder
2+
3+
# Install strictly what is needed to COMPILE gRPC
4+
RUN apt-get update && apt-get install -y \
5+
git \
6+
cmake \
7+
build-essential \
8+
autoconf \
9+
libtool \
10+
pkg-config
11+
12+
# Download and Compile gRPC (PR #40337)
13+
# We use /tmp/grpc to build
14+
WORKDIR /tmp/grpc
15+
RUN git clone --depth 1 https://github.com/grpc/grpc.git . \
16+
&& git fetch origin pull/40337/head:pr-40337 \
17+
&& git checkout pr-40337 \
18+
&& git submodule update --init --recursive --depth 1 \
19+
&& mkdir -p cmake/build && cd cmake/build \
20+
# Build the C++ Core (Static Lib)
21+
&& cmake ../.. -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF \
22+
&& make -j$(nproc) \
23+
&& make install \
24+
# Build the PHP Extension
25+
&& cd ../../src/php/ext/grpc \
26+
&& phpize \
27+
&& ./configure \
28+
&& make -j$(nproc) \
29+
# We do NOT need 'make install' here, we just need the compiled .so file
30+
&& cp modules/grpc.so /tmp/grpc.so
31+
132
FROM php:8.5-cli
233

3-
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
34+
# Standard Environment Setup
435
ENV COMPOSER_ALLOW_SUPERUSER=1
536

637
# Install system dependencies
@@ -13,13 +44,7 @@ RUN apt-get update && apt-get install -y \
1344
libonig-dev \
1445
libxml2-dev \
1546
default-mysql-client \
16-
cmake \
17-
build-essential \
18-
autoconf \
19-
libtool \
20-
pkg-config \
21-
&& apt-get clean \
22-
&& rm -rf /var/lib/apt/lists/*
47+
&& apt-get clean && rm -rf /var/lib/apt/lists/*
2348

2449
# Copy Composer from official image
2550
COPY --from=composer:2.8.9 /usr/bin/composer /usr/bin/composer
@@ -28,8 +53,7 @@ COPY --from=composer:2.8.9 /usr/bin/composer /usr/bin/composer
2853
COPY --from=mlocati/php-extension-installer:latest /usr/bin/install-php-extensions /usr/local/bin/
2954

3055
# Install PHP extensions declaratively
31-
RUN install-php-extensions \
32-
mysqli \
56+
RUN install-php-extensions mysqli \
3357
redis \
3458
pdo_mysql \
3559
pdo_pgsql \
@@ -42,26 +66,8 @@ RUN install-php-extensions \
4266
zip \
4367
pcov
4468

45-
# Manually Build gRPC from PR #40337
46-
# This creates a temporary directory, clones the repo, fetches the specific PR,
47-
# builds the C++ core, then builds the PHP extension, and finally cleans up.
48-
RUN mkdir -p /tmp/grpc-build && cd /tmp/grpc-build \
49-
&& git clone https://github.com/grpc/grpc.git . \
50-
&& git fetch origin pull/40337/head:pr-40337 \
51-
&& git checkout pr-40337 \
52-
&& git submodule update --init --recursive \
53-
# Build and install the gRPC C++ Core libraries first
54-
&& mkdir -p cmake/build && cd cmake/build \
55-
&& cmake ../.. -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF \
56-
&& make -j$(nproc) \
57-
&& make install \
58-
# Now build the PHP extension referencing the core libs
59-
&& cd ../../src/php/ext/grpc \
60-
&& phpize \
61-
&& ./configure \
62-
&& make -j$(nproc) \
63-
&& make install \
64-
# Enable the extension
65-
&& docker-php-ext-enable grpc \
66-
# Cleanup source files to reduce image size
67-
&& cd / && rm -rf /tmp/grpc-build
69+
# Copy grpc to a temporary spot first, then move it to the correct dir dynamically
70+
COPY --from=builder /tmp/grpc.so /tmp/grpc.so
71+
RUN mv /tmp/grpc.so $(php-config --extension-dir) \
72+
&& docker-php-ext-enable grpc
73+

0 commit comments

Comments
 (0)