0

I'm relatively new to docker and trying to set up a simple PHP project with docker-compose.

I use the php:8.1-fpm container and the composer:2.3.9 container images to start from.

My docker-compose.yml looks like:

version: '3.8'

services:

  my-php-fpm:
    container_name: my-php-fpm
    build:
      context: .docker/php-fpm
    volumes:
      - .:/var/www
    depends_on:
      - my-composer

  my-composer:
    container_name: my-composer
    build:
      context: .docker/composer
    volumes:
      - .:/app

The composer/Dockerfile:

FROM composer:2.3.9

CMD ["composer", "install"]

The php-fpm/Dockerfile

FROM php:8.1-fpm

RUN apt-get update && \
    apt-get install -y --no-install-recommends libssl-dev zlib1g-dev curl git unzip \
    netcat libxml2-dev libpq-dev libzip-dev openssh-client && \
    pecl install apcu && \
    pecl install xdebug && \
    docker-php-ext-enable xdebug && \
    docker-php-ext-install -j$(nproc) bcmath intl opcache zip && \
    docker-php-ext-enable apcu sodium && \
    apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

CMD ["php-fpm"]

EXPOSE 9000

When building, the composer container downloads a composer and right after buildig it executes it CMD ["composer", "install"] but this fails because some extensions are missing.

Now I want to execute the composer command of the composer container from the php-fpm container, because the php-fpm container has some more extensions installed and I don't want to install all these extensions in both containers.

Is that somehow possible?

  • A container can never directly invoke a command in another; similarly, a Dockerfile can't run something that only exists in another image. The linked question has several recipes for installing Composer as part of a PHP-based image, which would let you `RUN composer install` as part of your image build sequence. – David Maze Jul 26 '22 at 09:38
  • Thanks, I'll have a look at it. – Code.Working Jul 26 '22 at 09:50

0 Answers0