Search code examples
phpdockerdocker-composedockerfilephp-extension

Install PHP extensions on Debian base image


I'm trying to have a basic lamp stack up and running in containers. I use docker-compose and have a container for mysql, and I'm building my own thing for the site. It's a site that relies on a .htaccess for redirects for example. So I need to make a few changes to the basic image.

The docker-compose:

version: '3.1'

services:
  web:
    working_dir: /var/www/html
    build:
      context: ./docker
      dockerfile: Dockerfile
    volumes:
      - ./www/web:/var/www/html
    ports:
      - 50080:80
      - 50443:443
    networks:
      - netw

  database:
    image: mariadb
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - TZ=Asia/Jerusalem
    volumes:
      - db:/var/lib/mysql:cached
    ports:
      - 53306:3306
    networks:
      - netw

  adminer:
    image: adminer
    environment:
      - ADMINER_DESIGN=pappu687
    ports:
      - 50088:8080
    depends_on:
      - database
    networks:
      - netw

networks:
  netw:
    driver: bridge
volumes:
  db: ~

The dockerfile:

FROM php:8.1-apache

RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 \
#    php8.1-imap php8.1-mysql php8.1-mbstring \
    && apt-get install -y default-mysql-client \
    && docker-php-ext-install mysqli pdo pdo_mysql

RUN php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer

# config Apache
RUN a2enmod rewrite
RUN a2enmod ssl

COPY php.ini /usr/local/etc/php/php.ini

I want to install PHP extensions (the commented line in dockerfile). It says that those packages have no installation cadidate. I tried to add Sury's packages in a number of ways, no luck. I tried to use php:8.2-apache and 8.2 extensions, not working either.


Solution

  • The PHP that is installed in the docker images is not from packages, it is compiled. As such all extensions must be compiled for this version of PHP.

    Excerpted from the Dockerhub PHP image readme:

    How to install more PHP extensions

    Many extensions are already compiled into the image, so it's worth checking the output of php -m or php -i before going through the effort of compiling more.

    We provide the helper scripts docker-php-ext-configure, docker-php-ext-install, and docker-php-ext-enable to more easily install PHP extensions.

    More information on using these tools, as well as installing extensions from PECL, can be found in the linked documentation.