Docker Community Forums

Share and learn in the Docker community.

Docker-compose and multistage dockerfiles (multiple services)

Hi!
I have some problem/question

For example, we have simple multistage Dockerfile which contains different services

ARG PHP_VERSION=7.3

FROM php:7.3-fpm-alpine AS three
CMD ["php-fpm"]

FROM php:7.4-fpm-alpine AS four
CMD ["php-fpm"]

And docker-compose.yml
version: ‘3.7’

services:
    php4:
        # Use a static website hosting service in production
        # See https://facebook.github.io/create-react-app/docs/deployment
        image: php4
        build:
            context: ./
            target: 'four'

When I execute docker-compose build it must build only php4. But in reality

docker-compose build
Building php4
Step 1/5 : ARG PHP_VERSION=7.3
Step 2/5 : FROM php:7.3-fpm-alpine as three
7.3-fpm-alpine: Pulling from library/php
c9b1b535fdd9: Already exists
c1c0a1817bec: Pull complete
cdd5b3ea1fc3: Pull complete
db87396003bd: Pull complete
f09a455666b3: Pull complete
e4a989a7fc7b: Pull complete
b0b308a8c0d7: Pull complete
352857664b09: Pull complete
299d966decb7: Pull complete
f50ac619e43d: Pull complete
c9b1b535fdd9: Already exists
c1c0a1817bec: Already exists
cdd5b3ea1fc3: Already exists
db87396003bd: Already exists
—> Running in e120ab3b8c03
Removing intermediate container e120ab3b8c03
—> 5d5fc2655aed

Step 4/5 : FROM php:7.4-fpm-alpine AS four
7.4-fpm-alpine: Pulling from library/php
e54241183f8c: Pull complete
5a9f10e025a9: Pull complete
1661a47af75d: Pull complete
80a3db629727: Pull complete
1f51304a39ad: Pull complete
53730f2af539: Pull complete
Digest: sha256:a748390f2d9f006a0bed261f751656ea49c8f040f664038fe7a47bab44f61212
Status: Downloaded newer image for php:7.4-fpm-alpine
—> 014a46b60a9d
Step 5/5 : CMD [“php-fpm”]
—> Running in a46ca0117500
Removing intermediate container a46ca0117500
—> ee128203c407

Successfully built ee128203c407
Successfully tagged php4:latest

at the beggining steps are taken to build a service which is not in docker-compose.yml.
Executingdocker build --target four gives me the same result.
Is there any way to exclude execution of steps 2 and 3 when I build image from multistage dockerfile? Or is it by design or my wrong usage?