Install Composer in PHP Base Image Docker Container

Install Composer in PHP Base Image Docker Container

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

System Requirements

Composer in its latest version requires PHP 7.2.5 to run. A long-term-support version (2.2.x) still offers support for PHP 5.3.2+ in case you are stuck with a legacy PHP version. A few sensitive php settings and compile flags are also required, but when using the installer you will be warned about any incompatibilities.

Create Dockerfile from base image

FROM php:8.2-apache

Install image

Composer needs several supporting applications to work effectively, making the process of handling package dependencies more efficient. For decompressing files, Composer relies on tools like 7z (or 7zz), gzip, tar, unrar, unzip and xz. As for version control systems, Composer integrates seamlessly with Fossil, Git, Mercurial, Perforce and Subversion, thereby ensuring the application's smooth operation and management of library repositories. Before using Composer, ensure that these dependencies are correctly installed on your system.

# Install dependencies
RUN apt-get update && \
    apt-get install -y \
        libzip-dev \
        unzip \
        libonig-dev \
        libxml2-dev \
        libpng-dev \
        libjpeg-dev && \
    docker-php-ext-configure gd --with-jpeg && \
    docker-php-ext-install \
        pdo_mysql \
        zip \
        mbstring \
        exif \
        pcntl \
        bcmath \

Note: You may also use composer instead of composer/composer as image name above. It is shorter and is a Docker official image but is not published directly by docker and thus usually receives new releases with a delay of a few days. Important: short-aliased images don't have binary-only equivalents, so for COPY --from approach it's better to use composer/composer ones.

Install Composer

# Install Composer
RUN php -r "copy('', 'composer-setup.php');" \
    && php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
    && php -r "unlink('composer-setup.php');"
RUN composer install

Using Composer

Now that you’ve installed Composer, you are ready to use it!