Murat Colyaran
Murat Colyaran

Reputation: 2189

Generating PHP library with Dockerized gRPC

I'm trying to build a gRPC PHP Client and gRPC NodeJs Server in docker. But the problem is I can't install protoc-gen-php-grpc to my docker server. When I try to run this run this makefile:

proto_from_within_container:
    # PHP
    protoc /var/www/protos/smellycat.proto \
        --php_out=/var/www/php-client/src \
        $(: 👇 generate server interface) \
        --php-grpc_out=/var/www/php-client/src \
        $(: 👇 generates the client code) \
        --grpc_out=/var/www/php-client/src \
        --plugin=protoc-gen-grpc=/protobuf/grpc/bins/opt/grpc_php_plugin \
        --proto_path /var/www/protos

proto:
    powershell rm -r -fo php-client/src -ErrorAction SilentlyContinue
    powershell New-Item -ItemType Directory -Path php-client/src -Force -ErrorAction SilentlyContinue
    docker-compose run grpc-server make proto_from_within_container

With this command: make proto

Getting this error message after docker containers builded:

protoc /var/www/protos/smellycat.proto \

--php_out=/var/www/php-client/src \

\

--php-grpc_out=/var/www/php-client/src \

\

--grpc_out=/var/www/php-client/src \

--plugin=protoc-gen-grpc=/protobuf/grpc/bins/opt/grpc_php_plugin \

--proto_path /var/www/protos

protoc-gen-php-grpc: program not found or is not executable

Please specify a program using absolute path or make sure the program is available in your PATH system variable

--php-grpc_out: protoc-gen-php-grpc: Plugin failed with status code 1.

Makefile:4: recipe for target 'proto_from_within_container' failed

make: *** [proto_from_within_container] Error 1

This is my docker-compose file

version: "3"

services:
  grpc-server:
    container_name: grpc-server
    build:
      context: .
      dockerfile: Dockerfile-server
    working_dir: /var/www
    volumes:
      - .:/var/www

  grpc-client:
    image: php:7.4-cli
    container_name: grpc-client
    build:
      context: .
      dockerfile: Dockerfile-client
    working_dir: /var/www
    volumes:
      - .:/var/www
    command: bash -c [php php_client.php && composer install]

And this is my grpc-server docker file:

FROM node:latest

ENV DEBIAN_FRONTEND=noninteractive

#Versions
ARG PROTOBUF_VERSION=3.14.0
ARG PHP_GRPC_VERSION=1.34.0

# Utils
RUN apt-get update -yqq \
  && apt-get install -yqq wget unzip zlib1g-dev git autoconf libtool automake build-essential software-properties-common curl zip \
  && rm -rf /var/lib/apt/lists/*

# Protobuf
RUN mkdir -p /protobuf
RUN cd /protobuf \
    && wget https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/protoc-${PROTOBUF_VERSION}-linux-x86_64.zip -O protobuf.zip \
    && unzip protobuf.zip && rm protobuf.zip

# grpc PHP (generate client)
RUN apt-get update -yqq && apt-get upgrade -yqq
RUN apt-get install php php-dev php-pear phpunit zlib1g-dev -yqq
RUN pecl install grpc-${PHP_GRPC_VERSION}
RUN cd /protobuf && git clone -b v${PHP_GRPC_VERSION} https://github.com/grpc/grpc \
    && cd /protobuf/grpc && git submodule update --init
RUN cd /protobuf/grpc && make grpc_php_plugin

ENV PATH "/protobuf/bin:${PATH}"
ENV PATH "/protobuf/grpc/bins/opt:${PATH}"

# NPM Installation
WORKDIR /var/www
COPY . /var/www
RUN npm install

CMD ["node", "server.js"]

Do you have any advice?

Upvotes: 1

Views: 2667

Answers (1)

Murat Colyaran
Murat Colyaran

Reputation: 2189

After a lot of search and readings, I finally managed to build a full application that communicates with each other. The problem was at the Makefile, at this step:

--plugin=protoc-gen-grpc=/protobuf/grpc/bins/opt/grpc_php_plugin

I was assigning the wrong path for grpc_php_plugin.

There is my new dockerfile:

FROM php:7.4-cli

# Environment variables
ENV DEBIAN_FRONTEND=noninteractive

# Utils
RUN apt-get update -yqq && \
    apt-get upgrade -yqq && \ 
    apt-get install -y unzip build-essential git software-properties-common curl pkg-config zip zlib1g-dev

# Composer installation
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

# Install grpc and probuf with pecl
RUN pecl install grpc && pecl install protobuf

# Enable grpc and protobuf extensions in php.ini file
RUN echo starting && \
    docker-php-ext-enable grpc && \
    docker-php-ext-enable protobuf

# Install cmake
RUN apt-get update -yqq && apt-get -y install cmake

# Install grpc_php_plugin and protoc
RUN git clone -b v1.36.2 https://github.com/grpc/grpc && \
    cd grpc && git submodule update --init && \
    mkdir cmake/build && cd cmake/build && \
    cmake ../.. && make protoc grpc_php_plugin

# Setting node, protoc and grpc_php_plugin paths
ENV PATH "/grpc/cmake/build:${PATH}"
ENV PATH "/grpc/cmake/build/third_party/protobuf:${PATH}"


# Moving client folder to vm
WORKDIR /var/www
COPY ./client /var/www

# Packages
RUN composer install

# Generate php libraries from proto file
RUN make proto

CMD [ "php", "./handler.php" ]

For my full application, click.

Upvotes: 2

Related Questions