Docksal: How to add grpc php extension?

Created on 27 Jun 2018  路  3Comments  路  Source: docksal/docksal

Hi, what is the proper way to add grpc php extension into docksal environment?
I've read issue https://github.com/docksal/service-cli/issues/9 , grpc is not a regular php extension it requires multiple steps during installation especially when build from source.
Please see - https://cloud.google.com/php/grpc
Docker hub has needed image - https://hub.docker.com/r/grpc/php/ , I am not sure how to apply it into docksal default stack.

Thanks in advance!

馃彿documentation 馃彿question

Most helpful comment

@Bogdan1988 With docksal/cli v2 images installing additional php extensions is not as straightforward as it used to be before. v2 images are based on the official Docker PHP images, which install PHP from source and not from Debian packages.

Debian packages handle missing dependencies for you and also ship with pre-compiled binaries.
With the "from source approach", you often have to install missing tools/libraries manually to be able to compile extensions.

In this case, looks like grpc is missing g++ and zlib1g-dev to compile sucessfully.

Here's how you can add them and install the extension.


Option 1: Manually inside cli (used this for testing and debugging purposes only)

sudo apt-get update
sudo apt-get -y --no-install-recommends install g++ zlib1g-dev
sudo pecl install grpc
sudo docker-php-ext-enable grpc
$ php -i | grep grpc
/usr/local/etc/php/conf.d/docker-php-ext-grpc.ini,
grpc
grpc support => enabled
grpc module version => 1.12.0

Option 2 (recommended): using the stock image extension technique.

.docksal/docksal.yml

version: "2.1"

services:
  cli:
    image: ${COMPOSE_PROJECT_NAME_SAFE}_cli
    build: services/cli

.docksal/services/cli/Dockerfile

# Use a stock Docksal image as the base
FROM docksal/cli:2.2-php7.1

# Install grpc extension for PHP
RUN set -xe; \
    # Build dependencies
    buildDeps=" \
        g++ \
        zlib1g-dev \
    "; \
    apt-get update >/dev/null; \
    apt-get -y --no-install-recommends install >/dev/null \
        $buildDeps \
    ; \
    # Install pecl extensions
    pecl install >/dev/null </dev/null \
        grpc \
    ; \
    # Enable pecl extensions
    docker-php-ext-enable \
        grpc \
    ; \
    # Cleanup
    rm -rf /tmp/pear ~/.pearrc; \
    apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $buildDeps >/dev/null; \
    apt-get clean; rm -rf /var/lib/apt/lists/*
$ fin project start
...
$fin exec php -i | grep grpc
/usr/local/etc/php/conf.d/docker-php-ext-grpc.ini,
grpc
grpc support => enabled
grpc module version => 1.12.0

Note: I followed the docksal/cli stock Dockerfile style, so the above looks so intimidating.
This can also be written in a cheap and dirty way to keep things simple:

# Use a stock Docksal image as the base
FROM docksal/cli:2.2-php7.1

# Install grpc extension for PHP
RUN set -xe; \
    apt-get update; \
    apt-get -y --no-install-recommends install g++ zlib1g-dev; \
    pecl install grpc; \
    docker-php-ext-enable grpc

All 3 comments

@Bogdan1988 there are 2 ways

  1. Create you own image derived from docksal:cli, publish to the Docker Hub and use in your projects
  2. Use project-level Dockerfile to alter docksal:cli on the fly (don't worry, it will not re-compile it every time you start the project)
    Here is Manual: https://docksal.readthedocs.io/en/master/advanced/extend-images/
    It needs some updates, specifically it uses variables to refer the path to the Dockerfile, which is not needed anymore, because path to Dockerfile can be relative to the .docksal folder. It also uses specific version tag of the cli image, while I would use just latest version of the certain image e.g. docksal/cli:php7.1
    Here is a working example: https://github.com/docksal/example-hugo/blob/master/.docksal/docksal.yml

You can take a look at the Dockerfile that is used to compile docksal:cli to get an understanding of how to add yet another extension.
https://github.com/docksal/service-cli/blob/develop/7.1/Dockerfile

@Bogdan1988 With docksal/cli v2 images installing additional php extensions is not as straightforward as it used to be before. v2 images are based on the official Docker PHP images, which install PHP from source and not from Debian packages.

Debian packages handle missing dependencies for you and also ship with pre-compiled binaries.
With the "from source approach", you often have to install missing tools/libraries manually to be able to compile extensions.

In this case, looks like grpc is missing g++ and zlib1g-dev to compile sucessfully.

Here's how you can add them and install the extension.


Option 1: Manually inside cli (used this for testing and debugging purposes only)

sudo apt-get update
sudo apt-get -y --no-install-recommends install g++ zlib1g-dev
sudo pecl install grpc
sudo docker-php-ext-enable grpc
$ php -i | grep grpc
/usr/local/etc/php/conf.d/docker-php-ext-grpc.ini,
grpc
grpc support => enabled
grpc module version => 1.12.0

Option 2 (recommended): using the stock image extension technique.

.docksal/docksal.yml

version: "2.1"

services:
  cli:
    image: ${COMPOSE_PROJECT_NAME_SAFE}_cli
    build: services/cli

.docksal/services/cli/Dockerfile

# Use a stock Docksal image as the base
FROM docksal/cli:2.2-php7.1

# Install grpc extension for PHP
RUN set -xe; \
    # Build dependencies
    buildDeps=" \
        g++ \
        zlib1g-dev \
    "; \
    apt-get update >/dev/null; \
    apt-get -y --no-install-recommends install >/dev/null \
        $buildDeps \
    ; \
    # Install pecl extensions
    pecl install >/dev/null </dev/null \
        grpc \
    ; \
    # Enable pecl extensions
    docker-php-ext-enable \
        grpc \
    ; \
    # Cleanup
    rm -rf /tmp/pear ~/.pearrc; \
    apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $buildDeps >/dev/null; \
    apt-get clean; rm -rf /var/lib/apt/lists/*
$ fin project start
...
$fin exec php -i | grep grpc
/usr/local/etc/php/conf.d/docker-php-ext-grpc.ini,
grpc
grpc support => enabled
grpc module version => 1.12.0

Note: I followed the docksal/cli stock Dockerfile style, so the above looks so intimidating.
This can also be written in a cheap and dirty way to keep things simple:

# Use a stock Docksal image as the base
FROM docksal/cli:2.2-php7.1

# Install grpc extension for PHP
RUN set -xe; \
    apt-get update; \
    apt-get -y --no-install-recommends install g++ zlib1g-dev; \
    pecl install grpc; \
    docker-php-ext-enable grpc

Hi @lmakarov @achekulaev

Thank you for all your help.
Option 2 (recommended) - works perfectly!

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasonglisson picture jasonglisson  路  35Comments

ArtuGit picture ArtuGit  路  19Comments

lmakarov picture lmakarov  路  29Comments

StevenH1901 picture StevenH1901  路  19Comments

amjad-wp picture amjad-wp  路  18Comments