Docker 构建在另一个目标下执行指令(多阶段)
Docker build performs instructions under another target (multistage)
我有虚拟 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'
当我想在目标 test
下构建图像时,我执行命令:
docker build -t myimage --target=test .
但是我注意到目标开发下的指令也被执行了:
....
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
这是预期的吗?如果不是怎么回事?
--target
选项并不意味着“从这个目标开始”或“只构建这个目标”;意思是“stop at this target”。
因此,如果您指定 --target test
,Docker 将 运行 test
阶段及其之前的每个阶段。
我有虚拟 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'
当我想在目标 test
下构建图像时,我执行命令:
docker build -t myimage --target=test .
但是我注意到目标开发下的指令也被执行了:
....
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
这是预期的吗?如果不是怎么回事?
--target
选项并不意味着“从这个目标开始”或“只构建这个目标”;意思是“stop at this target”。
因此,如果您指定 --target test
,Docker 将 运行 test
阶段及其之前的每个阶段。