docker,

How to install PHP composer within Dockerfile

Dec 04, 2021 · 1 min read · Post a comment
How to install PHP composer within Dockerfile

Dockerizing your PHP application probably would require some external commands within your base docker image. That’s why you will need to have a Dockerfile and put all the additional commands there. A lot of the PHP developers need to have a PHP composer which is a package manager for PHP and provides a lot of required libraries and dependencies. Here I’m gonna show you how can you install it within the Dockerfile and build the docker image with a PHP composer.

Prerequisites

  • Docker installed

Install PHP Composer within Dockerfile

Step 1. For instance, if you are using the official php 7.4 docker image, your first Dockerfile line should look like:

# Dockerfile
FROM php:7.4.1

Step 2. So, now to install composer put the following command:

RUN curl -sS https://getcomposer.org/installer | php -- --check && \
    export COMPOSER_MEMORY_LIMIT=-1 && \
    composer self-update --1 && \
    composer install --no-interaction --optimize-autoloader
  • export COMPOSER_MEMORY_LIMIT=-1 - here you can set how much memory a php composer can use.
  • composer self-update - it will update composer itself to the latest 1 version and get all the dependencies.

Step 3. The whole Dockerfile should look like this:

# Dockerfile
FROM php:7.4.1

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --check && \
    export COMPOSER_MEMORY_LIMIT=-1 && \
    composer self-update --1 && \
    composer install --no-interaction --optimize-autoloader

Step 4. To build the image run:

docker build -t composer .

Conclusion

Here I showed you a basic Dockerfile configuration and how to install composer within the same Dockerfile. If you have any other questions about Dockerfile config please put a comment below. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on telegram.