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 going to show you how can you install it within the Dockerfile and build the Docker image with a PHP composer.
Prerequisites
- Docker
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
: Sets how much memory a PHP Composer can use.composer self-update
: Updates composer to the latest 1 version and get all 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.