Dockerfile : Multi-stage Builds & Caching

💡 Dockerfile : Multi-stage Builds & Caching — Executive Summary

Technical architectural analysis, practical configuration manifests, and deployment instructions for Dockerfile : Multi-stage Builds & Caching.

Samples

Syntax

1. Basic command
FROM
  • Create a new image based on the image.
  • Format: FROM :
  • Example: FROM node:16
RUN
  • Specifies the command to be executed during Docker image creation.
  • Shell format: RUN
  • exec format: RUN ["executable file", "parameter 1", "parameter 2"]
  • Example:
docker
RUN apt-get update && apt-get install -y curl
CMD
  • Specifies the command to be executed by default when running the container.
  • Format:
  • Shell format: CMD
  • exec format: CMD ["executable file", "parameter 1", "parameter 2"]
  • Example:
docker
CMD ["node", "app.js"]
ENTRYPOINT
  • Set the execution starting point of the container.
  • Similar to CMD, but with more emphasis on fixed execution.
  • Format: ENTRYPOINT ["Executable file", "Parameter 1"]
  • Example:
docker
ENTRYPOINT ["python3", "script.py"]
COPY
  • Copy files or directories to container.
  • Format: COPY
  • Example:
docker
COPY app.js /usr/src/app/
ADD
  • Can copy files + download and unzip URL.
  • Format: ADD
  • Example:
docker
ADD archive.tar.gz /usr/src/app/
WORKDIR
  • Set the working directory where subsequent commands will be executed.
  • Format: WORKDIR
  • Example:
docker
WORKDIR /usr/src/app
EXPOSE
  • Specify the port on which the container will listen.
  • Format: EXPOSE
  • Example:
docker
EXPOSE 3000
ENV
  • Set environment variables.
  • Format: ENV
  • Example:
docker
ENV NODE_ENV production
ARG
  • Specifies parameters to be passed when building Dockerfile.
  • Format: ARG =
  • Example:
docker
ARG APP_VERSION=1.0
VOLUME
  • Set the volume to be shared between the container and the host.
  • Format: VOLUME [""]
  • Example:
docker
VOLUME ["/data"]
USER
  • Set the container execution user account.
  • Format: USER [:group]
  • Example:
docker
USER 1001
LABEL
  • Set metadata.
  • Format: LABEL =
  • Example:
docker
LABEL maintainer="[email protected]"

2. Advanced Grammar
ONBUILD
  • Set the command triggered on the parent image.
  • Format: ONBUILD
  • Example:
docker
ONBUILD RUN echo "Triggered from base image!"
HEALTHCHECK
  • Check container status.
  • Format: HEALTHCHECK [option] CMD
  • Example:
docker
HEALTHCHECK --interval=30s --timeout=10s \
    CMD curl -f http://localhost/health || exit 1

3. Comment
  • Write a comment in the Dockerfile starting with #.
  • Example:
docker
# 이 Dockerfile은 Node.js 애플리케이션을 위한 것입니다.

4. Execution order of commands
  • Dockerfiles are executed sequentially from top to bottom.
  • Caching is applied according to the order of instructions, so for optimization purposes, it is recommended to write instructions that do not change frequently at the top.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *