Easy Solutions for Docker PHP Ext Install Command Not Found
Posted December 1, 2023
Encountering the docker php ext install command not found error is common when managing PHP extension within the Docker. The way you write your Dockerfile dictates how you will use the docker php ext install command to install extensions.
If this command does not correctly fit your Dockerfile structure, Docker gives the docker php ext install command not found error. This means you’re trying to use a command that is not recognized or available within the Docker image.
Dive into this article to uncover the causes of the “docker php ext install command not found” error (error: ‘docker-php-ext-install’ does not exist, or /bin/sh: docker-php-ext-install: not found) in your Dockerfile and explore effective solutions to resolve the issue and run your containers operate smoothly.
If you are looking to use the docker-php-ext-install command for a specific extension, refer to the following guides, each detailing the command for a specific PHP extension:
- Solving PHP Ext Enable Not Found Error
- Masterting the docker php ext install mysqli Command
- Using Redis docker php ext install and enable Commands
- Learn what you need, Install and configure GD with docker php ext Command
What is the docker php ext install command not found
Docker uses docker-php-ext-install command when installing PHP extensions available in the PHP source distribution and extensions bundled with PHP. In this case
docker
is the Docker command-line interface (CLI) for running commands with Docker.php
tells Docker the command is related to PHP and PHP must be available within Docker.ext-install
is a script that comes with the PHP source code.
What Causes the “docker php ext install command not found” Error
Consider the following Dockerfile example:
FROM php:8.0
# Install a PHP extension using the provided script
RUN docker-php-ext-install pdo_mysql
In this case. Docker will first install PHP. This way the official PHP Docker image will get docker-php-ext-install command ready. This script is part of the PHP source code and is bundled with the PHP Docker image.
Now if PHP base Image is not installed, the command will be unavailable. It’s this way you get the docker php ext install command not found error
Let’s dive into docker php ext install command not found error major occurrences and how to solve them.
Solution 1: Incorrect Command Syntax and PHP Extension Not Available
docker-php-ext-install must be executed correctly and using Available PHP extensions. Otherwise, you won’t escape the docker php ext install command not found error.
In this case, double-check the syntax in your Dockerfile as follows:
# Incorrect syntax in Dockerfile
RUN docker php ext install xml
# Correct syntax
RUN docker-php-ext-install xml
At the same time, the extension being installed must be available:
FROM php:8.0
# Incorrect extension name
RUN docker-php-ext-install not_a_real_extension
# Correct extension name
RUN docker-php-ext-install pdo_mysql
Solution 2: Creating Dockerfile with an Alpine Base Image
PHP base image has many distributions such as Alpine. Now Alpine is a Linux distribution. This means the docker-php-ext-install command won’t be available for use and you will probably the docker php ext install command not found error. Alpine Linux-based image doesn’t include the PHP extension manager by default.
If you are using Alpine Linux-based image alongside PHP you will use apk add
to install dependencies:
The Solution is to use an image that includes the PHP extension manager or install it manually
FROM php:8.0-alpine
# Install PHP extension manager
RUN apk --update add php-pear
Here is a Dockerfile Alpine image that will cause the docker php ext install not found error:
FROM php:8.0-alpine
# FROM php:8.0-apache-alpine
# FROM php:8.0-fpm-alpine
# Install the PDO MySQL extension
RUN docker-php-ext-install mysqli pdo_mysql
To avoid the docker php ext install not found error add your alpine image as follows:
FROM php:8.0-alpine
# FROM php:8.0-apache-alpine
# FROM php:8.0-fpm-alpine
# Use correct package names
RUN apk --update add libzip-dev && docker-php-ext-install zip
RUN apk --no-cache add \
$PHPIZE_DEPS \
mysql-client \
libzip-dev \
&& docker-php-ext-install pdo_mysql mysqli \
&& apk del $PHPIZE_DEPS
Solution 3: Using Vanilla Alpine Image will cause docker php ext install not found error
Consider the following two Docker file examples:
Using Vanilla Alpine Linux:
# The Alpine Linux base image
FROM alpine:latest
# Update the package repositories and install necessary packages
RUN apk update && \
apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community \
php php8.2-fpm nginx composer git
# Add dependencies and install xdebug with docker-php-ext-install
RUN apk add --no-cache autoconf file g++ gcc libc-dev make pkgconf re2c \
php7-pecl-xdebug \
&& docker-php-ext-install xdebug
In this case, you are using Linux Alpine which doesn’t have a PHP extension manager. This means your command is not available and Docker will tell you the same using /bin/sh: docker-php-ext-install: not found.
To solve your error, you need to run docker-php-ext-install to build an image where PHP is installed. For example, you will need to use PHP-FPM or PHP-APACHE as the base image that comes with the PHP extension manager bundled as follows:
FROM php:8.0-apache
# FROM php:8.0-fpm
# Install Xdebug extension
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
Solution 4: Using Vanilla Ubuntu Image will cause docker php ext install not found error
Consider using Ubuntu Image to install mysqli as follows:
FROM ubuntu:22.04
RUN apt -yqq update
RUN docker-php-ext-install pdo pdo_mysql
If you are using Ubuntu, always ensure you have a Dockerfile that only packages PHP individually. This way you will have access to the PHP package manager and avoid the docker php ext install not found error as follows:
Create a docker file for running PHP, i.e., php/Dockerfile
then add PHP using fpm or Apache:
FROM php:8.0-fpm
# FROM php:8.0-apache
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install xml
Solution 5: You’re Using a Minimal Image such as Slim
Slim based image variants are minimal. In most cases, slim PHP image doesn’t include unnecessary tools such as the extension manager which lead to docker php ext install command not found errors.
You need to avoid using Slim PHP images. If not, install the extension manager manually:
FROM php:8.0-slim
# Install PHP extension manager
RUN apt-get update && apt-get install -y php-pear
Solution 6: You are Missing Extension Build Dependencies:
Some extensions require you to additional dependencies before installing PHP extensions. For example, the following zip is incorrect and you get docker php ext install command not found errors:
FROM php:8.0-fpm
RUN docker-php-ext-install zip l
Zip requires ibzip-dev. you need to install build dependencies before running docker-php-ext-install as follows:
FROM php:8.0-fpm
# Install build dependencies
RUN apt-get update && apt-get install -y \
libzip-dev \
&& docker-php-ext-install zip
Solution 7: Check the PHP Version isn’t Outdated Image
You are using an outdated base image that doesn’t support the extension manager. Make sure you update to a more recent version of the PHP image.
FROM php:7.4
Solution 8: You are using docker-php-ext-install in your Docker-compose.yml file
Consider the following Docker-compose.yml installing iconv, mysqli, pdo, and pdo_mysql extensions:
version: '3'
services:
# PHP and Apache Service
php-apache:
image: php:8.0-apache
ports:
- 80:80
volumes:
- ./DocumentRoot:/var/www/html:z
links:
- 'mysql'
command: >
bash -c "docker-php-ext-install mysqli pdo pdo_mysql \
&& apt-get update --fix-missing && apt-get install -y \
curl \
build-essential \
libssl-dev \
libfreetype6-dev \
nano \
libxpm-dev \
libjpeg62-turbo-dev \
libpng-dev \
libmcrypt-dev \
libicu-dev \
libxml2-dev \
libwebp-dev \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/"
# MySQL Service
mysql:
image: mysql:5.7
ports:
- 3306:3306
volumes:
- mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "rootpassword"
MYSQL_DATABASE: "testdb"
MYSQL_USER: "testuser"
MYSQL_PASSWORD: "testpassword"
# Define named volumes
volumes:
mysql:
If this is how you run the docker-php-ext command, you will get all sorts of errors such as configure: error: thijpeglib.h not found, docker php ext enable not found, docker php ext install not found, etc.
Here you need to create a Dockerfile and write your command as follows:
# Use the official PHP image with Apache or FPM
FROM php:8.0-apache
# Install necessary extensions
RUN docker-php-ext-install mysqli pdo pdo_mysql && \
apt-get update --fix-missing && apt-get install -y \
curl \
build-essential \
libssl-dev \
libfreetype6-dev \
nano \
libxpm-dev \
libjpeg62-turbo-dev \
libpng-dev \
libmcrypt-dev \
libicu-dev \
libxml2-dev \
libwebp-dev && \
docker-php-ext-install -j$(nproc) iconv && \
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/
Then update the Docker Compose file and use the Dockerfile as follows:
version: '3'
services:
# PHP and Apache Service
php-apache:
build:
# Path to your Dockerfile
context: .
# The name of your Dockerfile as it appears in your project files
dockerfile: Dockerfile
ports:
- 80:80
volumes:
- ./DocumentRoot:/var/www/html:z
links:
- 'mysql'
# MySQL Service
mysql:
image: mysql:5.7
ports:
- 3306:3306
volumes:
- mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "rootpassword"
MYSQL_DATABASE: "testdb"
MYSQL_USER: "testuser"
MYSQL_PASSWORD: "testpassword"
# Define named volumes
volumes:
mysql:
Conclusion
The “docker php ext install command not found” error can arise from various contexts. This guide has provided insights into common scenarios and solutions to help your troubleshoot and resolve this error.
If this post didn’t solve your docker php ext install command not found error, leave your code sample in the comment section and we’ll address it ASAP for you.
For further knowledge on how to use these commands, check: