What is a Dockerfile and what are its key instructions?easy

Type
applied
Topic
image-building
Frequency
very common
Tags
image, building
Answer

A Dockerfile is a text file with instructions to build an image. Key instructions: FROM (base image), RUN (execute commands), COPY (add files), WORKDIR (set working directory), ENV (set environment variables), EXPOSE (document port), CMD/ENTRYPOINT (default command).

Explanation

Each instruction creates a new image layer. Minimize layers by chaining RUN commands with && . Place rarely-changing instructions early (FROM, system installs) and frequently-changing ones late (COPY src code) to maximize cache hits. CMD is overridable at runtime; ENTRYPOINT is not (without --entrypoint). Use ENTRYPOINT for the main executable and CMD for default arguments.

Follow-upWhat is the difference between COPY and ADD in a Dockerfile?