Multi-Stage Build
Multi-Stage Build allows you to create more efficient and smaller Images using multiple build stages in a single Dockerfile. Each stage can create an intermediate Image and files that are only available within that specific stage. This can be useful when additional tools are needed to create resources or build application libraries, but are not needed to run the application.
# Build stage
ARG PYTHON_VERSION=3.8
FROM python:${PYTHON_VERSION} AS base
WORKDIR /app
# Copy the application code into the container
COPY app-updated.py docker-logo.png requirements.txt ./
# Runtime stage
FROM python:${PYTHON_VERSION}-slim
WORKDIR /app
# Set an environment variable for the runtime
ENV APP_ENV="Development"
# Copy the built application and installed dependencies from the build stage
COPY --from=base /app .
RUN pip install --upgrade pip && \
pip install -r requirements.txt
# Expose port 8080 to the host
EXPOSE 8080
# Define the command to run the application
CMD ["python", "app-updated.py"]
!!! Thanks to Multi-Stage Build in a single Dockerfile, we can use multiple base Images, each of which can have specific tools for creating resources.