Quick Solutions for Docker PHP Ext Enable Not Found Error

Posted October 10, 2023
Quick Solutions for Docker PHP Ext Enable Not Found Error

The error docker php ext enable command not found means the docker-php-ext-enable script can’t enable PHP extensions within Docker. This heavily relies on how you write your Docker-compose and Dockerfile structure.

This comprehensive guide teaches what causes the docker php ext enable command not found error (/bin/sh: docker-php-ext-enable: not found) error in your Dockerfiles and the best solutions in hand to resolve the error and get your containers up and running.

What is docker php ext enable command not found error

You expect to use extra PHP extensions when working with Docker and PHP. Now, the docker-php-ext-enable commands enable an already installed extension. This way, the extension entry point will get added to php.ini.

The extension installed may not be enabled by default. This is where you need this additional step using docker-php-ext-enable

What Causes the Docker php ext enable command not found error

If the docker-php-ext-enable command is not correctly used, you will have a stream of errors such as:

  • docker php ext enable command not found,
  • /bin/sh: docker-php-ext-install: not found,
  • error: ‘docker-php-ext-install’ does not exist,
  • /bin/sh: 1: docker-php-ext-enable: not found

The message “docker php ext enable not found” is quite vague. Varying issues will cause it. However, this depends on the context in which it appears.

In Docker, the command docker php ext enable is available by default. You need to have PHP and its package manager ready within a Docker container. For any misconfiguration, the docker php ext enable command not found will likely happen.

Let’s dive in and examine some possible reasons for the docker php ext enable command not found error and solutions we can use to avoid this mess.

If you are looking to use docker-php-ext install, enable and configure commands with a specific extension, I have the following guides describing each command:

Solution 1: You are Enabling a Missing PHP Extension

The extension you want to enable must be installed with the docker-php-ext-install command before attempting to enable it.

FROM php:8.2-apache

RUN docker-php-ext-enable xdebug

Here, xdebug is not installed. Yet the docker-php-ext-enable will try to enable this extension. Because its binaries are missing, Docker will give you an error. You should install the package using the pecl install or docker-php-ext-install command to solve the problem.

Solution: Make sure to install the PHP extension before attempting to enable it.

FROM php:8.2-apache
# First install Xdebug as your extension
RUN pecl install xdebug
# And now enable command is ready
RUN docker-php-ext-enable xdebug

Solution 2: You have Docker Compose Issues

Consider the following example:

services:
  app:
    image: php:7.3-apache
    command: docker-php-ext-enable gd

In this case, you need to check your Docker Compose file and update your command as follows:

services:
  app:
    image: php:7.3-apache
    command: sh -c "docker-php-ext-install gd && docker-php-ext-enable gd"

At the same time, the best solution is to use a Docker file and package PHP individually with its extension as follows:

FROM php:7.3-apache
# An exmaple using gd on Dockerfile
RUN docker-php-ext-install gd
# Enable gd with this command
RUN docker-php-ext-enable gd

Then update the Docker Compose file and point to this dockerfile:

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

Solution 3: You’re Probably Using Vanilla Linux Distros such as Alpine, Ubuntu, and Debian

Look, the Linux-based image doesn’t include the PHP extension manager by default. This means docker-php-ext-enable doesn’t exist in your context. The php ext enable command will not be found.

Let’s say you are using Debian as follows:

FROM debian:10
RUN pecl install xdebug && docker-php-ext-enable xdebug

The first solution is to use the Debian package manager:

FROM debian:10
RUN sudo apt-get install php-xdebug

If not, the best solution is to use a base image and get a PHP package manager as follows:

FROM php:8.2-apache
# First install Xdebug as your extension
RUN pecl install xdebug
# And now enable command is ready
RUN docker-php-ext-enable xdebug

The same case applies to Ubuntu and other distros.

Solution 4: You Mistakenly Used an Image That Doesn’t Have the Extension Script

For example, if you use php:7.4-cli instead of php:7.4, Ensure you use an appropriate PHP image tag that includes the extension management scripts.

FROM php:7.4

Using an outdated PHP version in your Docker image may fail in other examples. Get the latest version added to your Dockerfiles.

Solution 5: Using a Minimal SLIM Image:

Slim PHP image doesn’t include unnecessary tools. In this case, the extension management scripts and PHP package manager could be missing, which will cause the PHP ext enable command not found error.

Avoid Slim and use other higher image variants. If that’s not your taste, install the extension management scripts manually.

FROM php:7.4-slim

# Install PHP extension management scripts
RUN apt-get update
# Use apt to install php-pear manager
RUN apt-get install -y php-pear

Solution 6: Check Command Syntax

The syntax in the Dockerfile is key here. You don’t want to mess it up. For example, RUN docker php ext enable won’t work.

Double-check the syntax in your Dockerfile and the docker run command:

# Incorrect syntax in Dockerfile
RUN docker php ext enable ...

# Correct syntax
RUN docker-php-ext-enable ...

Conclusion

Different contexts will cause the docker php ext to enable the command not found error. If this guide didn’t solve your docker php ext enable command not found error, leave your code sample in the comment section, and we’ll address it ASAP.

For additional insights on using these commands, refer to the following guides:

Quick Solutions for Docker PHP Ext Enable Not Found Error

Written By:

Joseph Chege