Is there anyway to use docker composer image with custom php?



I want to use docker composer image in multistage dockerfile for my laravel application in order to reduce its size. The problem I need PHP 7.4 for my dependencies but the composer’s PHP is 8.1.1. Is there any way to customize this? If there’s no way to downgrade the PHP version of it, how can I reduce the size of my docker image and what should I use instead to install my project dependencies?

I tried installing dependencies with --ignore-platform-reqs flag but the app doesn’t work after the build.

FROM composer:latest

COPY database/ database/

COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install


The Composer image is going to use whatever the latest version of PHP currently is, so composer:latest will never get you 7.4. You could scan backwards through the old Composer docker images until you find the most recent one that is based on 7.4, and then use that specific version of the image.

FROM composer:1.10.19

Note however that you’ll never get updated versions of composer this way.

Alternatively, if size is a concern then you could base your image on alpine:latest (which is tiny and currently uses 7.4 in its app repo) and then install PHP with apk add php-cli. This will give you an image of about 20mb, but sooner or later Alpine is going to update their base PHP to 8.0 and your image will break.

Probably your best option would be to base your image on one of the explicitly-versioned PHP containers like php:7.4-cli-alpine and then install the latest Composer into that image. This will give you much better control over what version of PHP runs in your image, and it’ll be very easy to later copy this image to base it off 8.0 for upgrade testing.

Answered By – Alex Howansky

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More