使用 Dockerized gRPC 生成 PHP 库

Generating PHP library with Dockerized gRPC

我正在尝试在 docker 中构建 gRPC PHP 客户端和 gRPC NodeJs 服务器。但问题是我无法将 protoc-gen-php-grpc 安装到我的 docker 服务器。当我尝试 运行 这个 运行 这个 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

使用此命令:make proto

在构建 docker 个容器后收到此错误消息:

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

这是我的 docker-compose 文件

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]

这是我的 grpc-server docker 文件:

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"]

你有什么建议吗?

经过大量的搜索和阅读,我终于设法构建了一个相互通信的完整应用程序。 问题出在 Makefile,在这一步:

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

我为 grpc_php_plugin 分配了错误的路径。

这是我的新 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" ]

对于我的完整申请,click