1. Introduction

PHP Composer is an essential tool for managing dependencies in PHP projects. It simplifies the installation and management of external libraries, ensuring that our projects are up-to-date with the latest versions.

However, when working within a Dockerized environment, properly installing Composer inside the Docker container is important.

In this tutorial, we’ll explore installing a PHP Composer inside a Docker container using a Dockerfile.

2. Using Dockerfile to Create a Container with Composer Installed

To install PHP Composer within a Docker container, we can leverage a Dockerfile to automate the process. In particular, this approach ensures consistency in the installation of Composer every time the container is built.

For this task, let’s explore how to create a Dockerfile that sets up a PHP environment with Composer.

2.1. Setting Up the Dockerfile

First, we start by creating a Dockerfile, which serves as a blueprint for the Docker image:

FROM php:7.4-fpm-alpine

# Update the package lists and install git
RUN apk update && apk add --no-cache git

# Install Composer globally
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add Composer to the PATH
ENV PATH="$PATH:/usr/local/bin"

This file contains the following information:

  • Base Image: the php:7.4-fpm-alpine image is the base image. It’s lightweight and provides a minimal PHP environment based on Alpine Linux
  • Update and Install Dependencies: the apk update && apk add –no-cache git command updates the package manager and installs git
  • Install Composer: the curl command downloads and installs Composer in the /usr/local/bin/ directory, making it accessible globally within the container
  • Configure PATH: the ENV PATH=”$PATH:/usr/local/bin” command adds the Composer installation directory to the system PATH. In particular, it ensures Composer can be run in any directory within the container

Now that we have the Dockerfile properly set up, let’s proceed to the next step.

2.2. Building the Docker Image

The next step is to build the Docker image. We can build the image with the use of dockerthe  build command:

$ docker build -t my-php-app .
...
OK: 26 MiB in 36 packages
Removing intermediate container dd550beb1f3f
 ---> cd683f16f218
Step 3/4 : RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
 ---> Running in 9f080f07960a
All settings are correct for using Composer
Downloading...

Composer (version 2.7.7) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Removing intermediate container 9f080f07960a
 ---> 783629578b14
Step 4/4 : ENV PATH="$PATH:/usr/local/bin"
 ---> Running in c9e9c77c2d18
Removing intermediate container c9e9c77c2d18
 ---> 1c55ed83e58a
Successfully built 1c55ed83e58a
Successfully tagged my-php-app:latest

This command tells Docker to build an image named my-php-app using the instructions in the Dockerfile located in the current directory (.). Furthermore, the build is successful.

2.3. Verifying the Installation

Additionally, after building the image, we can create and start a container to verify that Composer is installed correctly:

$ docker run -it my-php-app sh
/var/www/html #

Next, inside the container, let’s verify Composer:

# composer --version
Composer version 2.7.7 2024-06-10 22:11:12
PHP version 7.4.33 (/usr/local/bin/php)
Run the "diagnose" command to get a more detailed diagnostics output.

The output confirms that we’ve successfully installed Composer, and it is now ready for use within the container. Furthermore, the command composer –version displays the version of Composer installed, along with the PHP version it is using.

At this point, we’ve fully configured the Docker container to use PHP Composer. We can now proceed to utilize this environment for developing and managing PHP applications with ease, benefiting from the consistency and reproducibility provided by Docker.

3. Using Docker Compose to Create a Container with Composer Installed

Additionally, we can utilize Docker Compose to manage and deploy containers with PHP Composer installed. Docker Compose simplifies the process by allowing us to define and run multi-container Docker applications using a single configuration file. Furthermore, this approach is useful for setting up a development environment where we want to separate the application logic from dependency management.

3.1. Setting up the Project Directory

First, we begin by organizing the project files within a dedicated directory. This structure ensures that all necessary components are easily accessible and well-organized. Let’s create a project directory and navigate into it:

$ mkdir my-php-project
$ cd my-php-project

Within the directory, we create three essential files:

  • docker-compose.yml
  • index.php
  • .dockerignore

Furthermore, here is the project structure:

my-php-project/
│
├── docker-compose.yml
├── index.php
└── .dockerignore

Let’s proceed with creating the docker-compose.yaml file.

3.2. Writing the Docker Compose File

Next, we define the services required to run our PHP application with Composer using a docker-compose.yaml file. This file serves as the blueprint for the multi-container application:

version: '3.8'

services:
  app:
    image: php:8.1-cli
    container_name: php-composer-app
    working_dir: /app
    volumes:
      - .:/app
    ports:
      - "8080:80"
    command: sh -c "php -S 0.0.0.0:80 -t /app"
    depends_on:
      - composer

  composer:
    image: composer:latest
    container_name: composer-installer
    working_dir: /app
    volumes:
      - .:/app
    command: ["composer", "install"]

Each section of the docker-compose.yml file serves a specific purpose.

Firstly, we have the app service:

  • Uses the official php:8.1-cli image, providing the latest PHP environment
  • Maps the current directory (.) to the /app directory inside the container, ensuring our PHP files are accessible
  • Exposes port 80 of the container, mapped to port 8080 on our host, allowing us to access the application via http://localhost:8080
  • Runs the PHP built-in server using the command php -S 0.0.0.0:80 -t /app, which serves our PHP files
  • Depends on the composer service to ensure dependencies are installed before the application starts

Finally, we have the composer service:

  • Utilizes the official composer image, which comes pre-configured with PHP Composer
  • Mounts the current directory to /app in the container, allowing Composer to access composer.json if it exists
  • Executes the composer install command to install any dependencies defined in composer.json automatically

We now proceed to create the simple PHP file.

3.3. Creating the PHP File

To test the setup, we create a simple index.php file, which will serve as the entry point for our PHP application:

<?php
echo "Hello, World!";

This file outputs “Hello, World!” when accessed via a web browser, confirming that the PHP environment is functioning correctly.

3.4. Creating the .dockerignore File

Additionally, we create a .dockerignore file to specify which files and directories should be excluded from the Docker build context. For this example, we exclude the .git directory. In particular, we ensure the Git repository metadata is not included in the Docker image.

3.5. Running the Container

With the complete setup, we proceed to start the services defined in docker-compose.yml:

$ docker-compose up     
WARN[0000] /home/kali/my-php-project/docker-compose.yaml: `version` is obsolete 
[+] Running 25/2
 ✔ app Pulled                                                                                                                                                                      350.6s 
 ✔ composer Pulled                                                                                                                                                                 361.8s 
[+] Running 3/2
 ✔ Network my-php-project_default  Created                                                                                                                                           0.2s 
 ✔ Container composer-installer    Created                                                                                                                                           0.4s 
 ✔ Container php-composer-app      Created                                                                                                                                           0.0s 
Attaching to composer-installer, php-composer-app
composer-installer  | Composer could not find a composer.json file in /app
composer-installer  | To initialize a project, please create a composer.json file. See https://getcomposer.org/basic-usage
composer-installer exited with code 1
php-composer-app    | [Wed Aug 21 15:42:32 2024] PHP 8.1.29 Development Server (http://0.0.0.0:80) started
php-composer-app    | [Wed Aug 21 15:44:23 2024] 172.18.0.1:50744 Accepted
php-composer-app    | [Wed Aug 21 15:44:25 2024] 172.18.0.1:50744 [200]: GET /
php-composer-app    | [Wed Aug 21 15:44:25 2024] 172.18.0.1:50744 Closing

This command accomplishes several tasks. Firstly, it starts the composer service, which installs the necessary dependencies. Then, it initiates the app service, which serves our PHP application at http://localhost:8080.

3.6. Validating PHP Composer Installation

Let’s confirm that PHP Composer has been successfully installed in the Docker container. We can access the composer service container and check the installed version of Composer:

$ docker-compose run composer sh 
/app #

Once inside the container, we verify the Composer installation:

# composer --version
Composer version 2.7.7 2024-06-10 22:11:12
PHP version 8.3.10 (/usr/local/bin/php)
Run the "diagnose" command to get a more detailed diagnostics output.

This shows the installed Composer version, confirming the setup is complete and functional.

4. Conclusion

In this article, we’ve successfully demonstrated how to install and configure PHP Composer within a Docker container using both a Dockerfile and Docker Compose. These approaches ensure a consistent and reliable environment for managing PHP dependencies across different systems. By following these steps, we can seamlessly integrate Composer into Dockerized PHP projects, enhancing both development and deployment workflows.