How to use composer with docker-compose?

How to use composer with docker-compose?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Use Composer with Docker Compose

Running a PHP stack inside a containerized environment often leads to a specific friction point: managing dependencies. You might be setting up a Symfony application alongside services like Elasticsearch and Redis, but when you attempt to install packages using composer, the command fails silently or errors out. This usually happens because the docker-compose.yml configuration is missing the necessary entrypoint logic to invoke Composer within the PHP-FPM context.

In your current setup, you have separate services for Nginx, PHP-FPM, and a dedicated composer service using the tutum/nginx, php:fpm, and composer/composer:php7 images. The critical issue lies in how the composer service is defined. By setting the command to simply install, you are instructing the container to run that script immediately upon startup and then exit. Once the process exits, the container stops. Even if dependencies were installed, there is no mechanism to mount your project's vendor directory or persist the state of the installation across container restarts effectively without a specific volume strategy.

The most robust approach to using Composer with Docker Compose involves leveraging the PHP-FPM service rather than spinning up a separate Composer container. This avoids the complexity of managing disparate images and keeps your stack cohesive. You should mount your application directory into the PHP-FPM container at the standard web root (typically /var/www/html or /app, depending on the base image) and configure the PHP entrypoint to run composer install.

Here is how you can structure the PHP-FPM service to handle dependency installation:

phpfpm:
  build:
    context: .
    dockerfile: Dockerfile.install
  volumes:
    - ./app:/var/www/html
  # Ensure the container starts and runs the composer command
  command: bash -c "composer install --no-interaction && tail -f /dev/null"

In this pattern, you are using a Dockerfile to install PHP extensions required by your project (such as those for Elasticsearch or Redis) before entering the container. The command override is crucial; it ensures that when Docker starts the service, it executes Composer immediately. Once dependencies are resolved and installed into the vendor folder inside the container, the process pauses (tail -f /dev/null) to keep the container running so you can attach a shell for development.

If you prefer using pre-built images like php:fpm, you must ensure that the composer/composer package is installed via apt-get or pecl within your Dockerfile. Simply mapping the volume from your host machine to the container does not automatically transfer the vendor directory unless you explicitly mount it.

A common pitfall in your original configuration was mounting the app volume but failing to configure the entrypoint correctly. The composer/composer:php7 image is designed for running Composer scripts, not for hosting web applications. Using it as a bridge to install dependencies on a separate PHP-FPM service introduces unnecessary network latency and context switching. Instead, unify the environment. Install Composer inside your PHP base image or use the official php-fpm image which often includes Composer tools if configured correctly via docker run --entrypoint composer.

For production environments, consider caching the Composer dependencies to speed up builds. You can add a named volume specifically for the vendor directory in your docker-compose.yml:

volumes:
  - app_vendors:/var/www/html/vendor
  - ./app:/var/www/html

services:
  phpfpm:
    volumes:
      - app_vendors:/var/www/html/vendor

This separation ensures that even if you rebuild the PHP container, your installed packages remain intact. This is particularly important for Symfony projects where autoload optimization requires a fresh generation after installation. Remember to run composer dump-autoload as part of your entrypoint script to ensure the autoloader is up to date with any new classes.

By consolidating your stack into a single PHP-FPM service that handles both web serving and dependency management, you eliminate the need for multiple containers to coordinate state. This approach aligns with modern practices where development tools are baked into the runtime image rather than managed as transient ephemeral services.

Our network