Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: September 23, 2024
Composer is a PHP dependency management tool; put simply, it assesses our project’s PHP libraries and installs or updates those libraries as needed.
We can install PHP Composer in a Docker container using shell commands, but for readability and efficiency, we can simply copy a composer executable from an existing image to our build. In this tutorial, we discuss how to install PHP Composer inside a Docker container.
We can install composer in a Docker container by downloading the installer with wget or curl and executing it from the command line with php.
Let’s demonstrate how to download and install composer using wget and php in our Dockerfile:
FROM php:8.1-apache-bullseye
RUN ["/bin/bash", "-c", "apt update && apt install wget -y \
&& wget -O- https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer"]
ENTRYPOINT composer --version
After referencing the base image, the Dockerfile installs wget. Thereafter, wget writes the content of the composer installer to stdout and said content is redirected to php for execution.
Let’s build and run the Dockerfile to confirm the installation:
$ docker build . -t test -q && docker run test
...truncated...
Composer version 2.7.9 2024-09-04 14:43:28
PHP version 8.1.29 (/usr/local/bin/php)
Run the "diagnose" command to get more detailed diagnostics output.
As expected of the ENTRYPOINT command in our Dockerfile, the container outputs the composer version.
Now, let’s repeat the same process with curl and php in another Dockerfile:
FROM php:8.1-apache-bullseye
RUN ["/bin/bash", "-c", "curl https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer"]
ENTRYPOINT composer --version
curl was preinstalled in our base image, so we didn’t have to add installation commands. That said, if we’ll be performing other network operations in our Dockerfile besides a download, we’ll opt for curl over wget, as curl supports more protocols.
Once again, we’ll build and run to confirm the installation:
$ docker build . -t test -q && docker run test
...truncated...
Composer version 2.7.9 2024-09-04 14:43:28
PHP version 8.1.29 (/usr/local/bin/php)
Run the "diagnose" command to get more detailed diagnostics output.
The installer script installs the latest version of composer. However, if we want a specific version, we can get a manual download link for our desired composer.phar file and download the file to a PATH directory. Here’s how we’d get composer 2.4.4 installed in our container:
FROM php:8.1-apache-bullseye
ARG path=/usr/local/bin/composer
RUN ["/bin/bash", "-c", "apt update && apt install wget -y \
&& wget -O $path https://getcomposer.org/download/2.4.4/composer.phar && chmod +x $path"]
ENTRYPOINT composer --version
The Dockerfile above installs wget, downloads the composer.phar executable version 2.4.4 to PATH with the filename composer, then makes the composer file executable. Let’s confirm the installation with docker build and docker run:
$ docker build . -t test -q && docker run test
...truncated...
Composer version 2.4.4 2022-10-27 14:39:29
We can achieve the same result with curl instead of wget:
FROM php:8.1-apache-bullseye
ARG path=/usr/local/bin/composer
RUN ["/bin/bash", "-c", "curl -o $path https://getcomposer.org/download/2.4.4/composer.phar && chmod +x $path"]
ENTRYPOINT composer --version
Like wget, curl downloads composer.phar 2.4.4 to PATH, names it “composer”, and makes it executable.
To install composer from an existing image, we use the COPY –from instruction in our Dockerfile:
FROM php:8.1-apache-bullseye
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
ENTRYPOINT composer --version
In the Dockerfile above, the COPY –from instruction simply copies the composer executable from /usr/bin/composer on the composer:latest image to /usr/local/bin/composer on our image. So, when we create a container from our image, the container will have composer:
$ docker build . -t test -q && docker run test
...truncated...
Composer version 2.7.9 2024-09-04 14:43:28
PHP version 8.1.29 (/usr/local/bin/php)
Run the "diagnose" command to get more detailed diagnostics output.
To get a specific composer version using this method, we’ll pass an image with our desired version to the COPY –from instruction. For example, if we wanted composer 2.7.1, we’d use the composer:2.7.1 image:
FROM php:8.1-apache-bullseye
COPY --from=composer:2.7.1 /usr/bin/composer /usr/local/bin/composer
ENTRYPOINT composer --version
Let’s build this image and run a container from it to confirm installation:
$ docker build . -t test -q && docker run test
...truncated...
Composer version 2.7.1 2024-02-09 15:26:28
In this article, we learned how to install PHP composer in a Docker container using shell commands and by copying an executable from an existing image.