Search code examples
dockerdockerfiledocker-multi-stage-build

Docker build performs instructions under another target (multistage)


I have dummy Dockerfile:

FROM python:3.8-alpine3.13 AS python-base

RUN echo http://mirror.yandex.ru/mirrors/alpine/v3.13/main > /etc/apk/repositories; \
    echo http://mirror.yandex.ru/mirrors/alpine/v3.13/community >> /etc/apk/repositories

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONFAULTHANDLER=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONHASHSEED=random

FROM python-base as builder-base

WORKDIR /app
RUN apk update && apk add --no-cache \
    gcc musl-dev postgresql-dev openldap-dev gettext-dev \
    libffi-dev openssl-dev python3-dev jpeg-dev zlib-dev musl-locales \
    musl-locales-lang postgresql-libs libjpeg graphviz-dev ttf-freefont
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY pythonapline .
RUN python manage.py migrate
RUN python manage.py compilemessages
EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

FROM builder-base as development
RUN pip install fastapi
RUN echo 'DEVELOPMENT'

FROM builder-base as test
RUN echo 'TEST'

When I want to build an image under target test I perform the command:

docker build -t myimage --target=test .

But I noticed that instructions under target development are also performed:

....
Step 16/18 : RUN echo 'DEVELOPMENT'
 ---> Running in 4cfa2ed80350
DEVELOPMENT
Removing intermediate container 4cfa2ed80350
 ---> 935d770dfe6d
Step 17/18 : FROM builder-base AS test
 ---> 442c02445aae
Step 18/18 : RUN echo 'TEST'
 ---> Running in 13432a53bec0
TEST
Removing intermediate container 13432a53bec0
 ---> 96e80f6d9603
Successfully built 96e80f6d9603

Is that expected? If no what's going on?


Solution

  • The --target option doesn't mean "start with this target" or "only build this target"; it means "stop at this target".

    So if you specify --target test, Docker will run the test stage and every stage that precedes it.