Thursday, February 6, 2025

Dockerfile keywords (Also known as Instructions)


Double-click on the image to zoom-out ...Larger.

To return to Home page: Refresh Page or Take ESC Button on Keyboard.

Dockerfile keywords

Dockerfiles use specific keywords (also called instructions) to define how a container image is built. Here are the most important ones:

Basic Instructions

  • FROM – Specifies the base image (e.g., FROM ubuntu:20.04).
  • RUN – Executes commands during the build (e.g., RUN apt-get update).
  • CMD – Specifies the default command to run in a container (e.g., CMD ["nginx", "-g", "daemon off;"]).
  • ENTRYPOINT – Defines the main executable of the container (e.g., ENTRYPOINT ["python", "app.py"]).
  • WORKDIR – Sets the working directory inside the container (e.g., WORKDIR /app).
  • COPY – Copies files from the host to the image (e.g., COPY . /app).
  • ADD – Similar to COPY, but can also extract tar archives and handle URLs.
  • EXPOSE – Declares the port the container will listen on (e.g., EXPOSE 80).

Optimization & Efficiency

  • LABEL – Adds metadata to an image (e.g., LABEL maintainer="twtechs671@gmail.com").
  • ENV – Sets environment variables (e.g., ENV NODE_ENV=production).
  • ARG – Defines build-time variables (e.g., ARG VERSION=1.0).
  • VOLUME – Declares a volume for persistent storage (e.g., VOLUME /twtech-data).

User & Execution Control

  • USER – Specifies the user to run the container as (e.g., USER twtech-root).
  • ONBUILD – Triggers commands when the image is used as a base image.

Best Practices

  • Use multi-stage builds to reduce image size (FROM builder AS base).
  • Minimize layers by chaining commands (RUN apt-get update && apt-get install -y curl).
  • Use .dockerignore to exclude unnecessary files.

No comments:

Post a Comment

AWS DynamoDB | Integration With S3 Bucket.

  AWS DynamoDB ↔ S3 integration , View: What DynamoDB ↔ S3 integration is,   How to use DynamoDB ↔ S3 integration,   Why uses DynamoDB ↔  S3...