How to add php-redis for a dockerfile of laravel to kubernetes?

1/16/2019

I'm setting a deploy of laravel to kubernetes and want to have redis.

Actually i have a Dockerfile for nginx and another one for php-fpm-alpine and all the kubernetes yaml files(the ingress with tls, deployments and services)

I expected to add the php redis to the php-fpm container, any ideas?

here the actual php /Dockerfile

#
# PHP Dependencies
#
FROM composer:1 as vendor

COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install \
    --ignore-platform-reqs \
    --no-interaction \
    --no-plugins \
    --no-scripts \
    --prefer-dist

#
# Application
#
FROM php:fpm-alpine
RUN apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        curl \
        libtool \
        libxml2-dev \
    && apk add --no-cache \
        curl \
        git \
        mysql-client \
    && docker-php-ext-install \
        mbstring \
        pdo \
        pdo_mysql \
        tokenizer \
        bcmath \
        opcache \
        xml \
    && apk del -f .build-deps \
    && docker-php-ext-enable pdo_mysql

WORKDIR /var/www/html

COPY . /var/www/html
COPY --from=vendor /app/vendor/ /var/www/html/vendor/
COPY .env.example /var/www/html/.env

RUN chown -R root:www-data . 

EXPOSE 9000

CMD ["php-fpm"]

and the nginx /Dockerfile

FROM nginx:stable-alpine

ADD default.conf /etc/nginx/conf.d/default.conf

COPY public /var/www/html/public
WORKDIR /var/www/html/public

finally the nginx default /conf.d

server {
    listen 80;

    index index.php index.html;
    root /var/www/html/public;

    client_max_body_size 32M;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
    fastcgi_pass   php:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
    }
}
-- madwyatt
docker
kubernetes
laravel
php

2 Answers

5/25/2020

Update for alpine php 7.3.5

RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
        && pecl install redis \
        && docker-php-ext-enable redis.so
-- mihnsen
Source: StackOverflow

1/17/2019

Since you are using official PHP docker image, you can install php-redis extension via PECL:

RUN pecl install redis \
    && docker-php-ext-enable redis

Simple as that!

You can learn more about installing PHP extensions from official PHP docker docs (in case of php-redis, installing PECL extensions).

So in your case, RUN command can look something like this:

# Your PHP Dockerfile
RUN apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        curl \
        libtool \
        libxml2-dev \
    && apk add --no-cache \
        curl \
        git \
        mysql-client \
    && pecl install redis \     # install redis extension via PECL
    && docker-php-ext-install \
        mbstring \
        pdo \
        pdo_mysql \
        tokenizer \
        bcmath \
        opcache \
        xml \
    && apk del -f .build-deps \
    && docker-php-ext-enable \
       pdo_mysql \
       redis                    # don't forget to enable redis extension
-- krlv
Source: StackOverflow