/ kafka

Adding Kafka support to PHP on Docker

Here is how you can add native Apache Kafka support to the official PHP 7 Docker images. This requires the rdkafka PECL extension, which in turn requires the librdkafka C/C++ library.

Debian based PHP Docker image

# Dockerfile
FROM php:7.1

ENV LIBRDKAFKA_VERSION v0.11.0
ENV BUILD_DEPS \
        build-essential \
        git \
        libsasl2-dev \
        libssl-dev \
        python-minimal \
        zlib1g-dev

RUN apt-get update \
    && apt-get install -y --no-install-recommends ${BUILD_DEPS} \
    && cd /tmp \
    && git clone \
        --branch ${LIBRDKAFKA_VERSION} \
        --depth 1 \
        https://github.com/edenhill/librdkafka.git \
    && cd librdkafka \
    && ./configure \
    && make \
    && make install \
    && pecl install rdkafka \
    && docker-php-ext-enable rdkafka \
    && rm -rf /tmp/librdkafka \
    && apt-get purge \
        -y --auto-remove \
        -o APT::AutoRemove::RecommendsImportant=false \
        ${BUILD_DEPS}

You can then check that everything works by building the image and checking the default PHP INI configuration:

$ docker build -t my-php:7.1-kafka .
...
$ docker run -it --rm my-php:7.1-kafka sh -c "php -i | grep kafka"
Additional .ini files parsed => /usr/local/etc/php/conf.d/docker-php-ext-rdkafka.ini
rdkafka
rdkafka support => enabled
librdkafka version (runtime) => 0.11.0
librdkafka version (build) => 0.11.0.0

Alpine based PHP Docker image

FROM php:7.1-alpine

ENV LIBRDKAFKA_VERSION v0.11.0
ENV BUILD_DEPS \
  autoconf \
        bash \
        build-base \
        git \
        pcre-dev \
        python

RUN apk --no-cache --virtual .build-deps add ${BUILD_DEPS} \
    && cd /tmp \
    && git clone \
        --branch ${LIBRDKAFKA_VERSION} \
        --depth 1 \
        https://github.com/edenhill/librdkafka.git \
    && cd librdkafka \
    && ./configure \
    && make \
    && make install \
    && pecl install rdkafka \
    && docker-php-ext-enable rdkafka \
    && rm -rf /tmp/librdkafka \
    && apk del .build-deps

You can then check that everything works by building the image and checking the default PHP INI configuration:

$ docker build -t my-php:7.1-alpine-kafka .
...
$ docker run -it --rm my-php:7.1-alpine-kafka sh -c "php -i | grep kafka"
Additional .ini files parsed => /usr/local/etc/php/conf.d/docker-php-ext-rdkafka.ini
rdkafka
rdkafka support => enabled
librdkafka version (runtime) => 0.11.0
librdkafka version (build) => 0.11.0.0
Jérôme Gamez

Jérôme Gamez

Jérôme's French is good enough to make Germans believe he's a native, but he can't fool the French. He's both, so that's a conflict.

Read More