Using multi-stage build is definitely one part of the answer here.
docker-compose v3.4 target being the second and last.
Here is a example to have 2 containers (1 normal & 1 w/ xdebug installed) living together :
Dockerfile
FROM php:7-fpm AS php_base
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
apt-get install -y git libicu-dev libmagickwand-dev libmcrypt-dev libcurl3-dev jpegoptim
RUN pecl install imagick && \
docker-php-ext-enable imagick
RUN docker-php-ext-install intl
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install opcache
RUN docker-php-ext-install mcrypt
RUN docker-php-ext-install curl
RUN docker-php-ext-install zip
FROM php_base AS php_test
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
docker-compose.yml
version: '3.4'
services:
php:
build:
context: ./
target: php_base
php_test:
build:
context: ./
target: php_test
# ...