Add extension BCMath for CLI and FPM

Hi there,

Could you let me know if there’s any way to add a PHP extension to the PHP-CLI and FPM using the docker-PHP images?

Thanks.

Great question, thanks for asking!

In this PR, I added some extra detail. If this answers your questions, I will merge this documentation to help others as well.

Preview the new docs addition

Click this link to see a preview how I do it with Docker Compose: https://github.com/serversideup/docker-php/tree/docs/custom-addon-build#other-customizations

Package name to use in your case

When you get to configuring your Dockerfile, be sure to use the correct PHP version in your package name (example: php8.0-bcmath).

Let me know if this works for you! Any questions or additional clarity, let me know and I will update the docs before merging them :+1:

1 Like

It works! :tada::tada::tada:

But I’ve got some issues. My app requires to run horizon (php-cli) and, from what I understand, my Dockerfile should implement the multi-stage feature:

Dockerfile

FROM serversideup/php:8.0-cli AS php-nginx

# Install required extensions for fpm: php8.0-bcmath
# COPY ...

FROM serversideup/php:8.0-cli AS php-cli

# Install required extensions for cli: php8.0-imagick, php8.0-etc

Then my docker-compose.yml services should target the right stages in the build process.

version: "3.7"
services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
      target: php-nginx
    volumes:
      - .:/var/www/html/:cached

  horizon:
    build:
      context: .
      dockerfile: Dockerfile
      target: php-cli
    volumes:
      - .:/var/www/html/:cached
    command: "php /var/www/html/artisan horizon"

docker-composer.yml

version: "3.7"
services:
  php:
     -networks=...
     -etc=...
  horizon:
     -networks=...
     -etc=...

Finally, run the following command:

docker composer -f docker-compose.yml -f docker-composer.dev.yml up --build

But it fails 🥲

[+] Building 0.3s (7/10)                                                                                                                                                                                                                                           
 => [inventory-saas-api_php internal] load build definition from Dockerfile                                                                                                                                                                                   0.0s
 => => transferring dockerfile: 777B                                                                                                                                                                                                                          0.0s
 => [inventory-saas-api_horizon internal] load build definition from Dockerfile                                                                                                                                                                               0.0s
 => => transferring dockerfile: 777B                                                                                                                                                                                                                          0.0s
 => [inventory-saas-api_php internal] load .dockerignore                                                                                                                                                                                                      0.0s
 => => transferring context: 34B                                                                                                                                                                                                                              0.0s
 => [inventory-saas-api_horizon internal] load .dockerignore                                                                                                                                                                                                  0.0s
 => => transferring context: 34B                                                                                                                                                                                                                              0.0s
 => [inventory-saas-api_php internal] load metadata for docker.io/serversideup/php:8.0-cli                                                                                                                                                                    0.0s
 => CANCELED [inventory-saas-api_php internal] load build context                                                                                                                                                                                             0.1s
 => => transferring context: 25.21kB                                                                                                                                                                                                                          0.1s
 => [inventory-saas-api_php 1/4] FROM docker.io/serversideup/php:8.0-cli                                                                                                                                                                                      0.0s
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: target stage php-cli could not be found

What am I doing wrong? Sorry but I am still learning Docker, and maybe things are wrong with my config or steps.

Anyway, thanks!

Note
The multi-stage feature works only if you have activated the Buildkit in Docker Desktop or CLI

Never mind my previous reply; my Dockerfile had an extra \ after the first stage.
Everything works perfectly.

Diff checker here

Thanks :vulcan_salute::vulcan_salute::vulcan_salute::vulcan_salute:

1 Like

Nice!!! I’m very glad this helped.

I need to dive into Multi Stage builds more. That seems like a really good approach.

Keep up the good work :+1: