📧 chinmayeepavi@gmail.com Support

Dockerfile Reference

Instruction Purpose
FROM Sets the base image for subsequent instructions. Must be first.
RUN Executes a command in a new layer on top of the current image and commits the result. Build-time.
CMD Default command executed when a container starts. Only the last CMD takes effect.
ENTRYPOINT Configures the container to run as an executable.
COPY Copies files or directories from the build context into the image.
ADD As COPY, but additionally expands local tar archives and accepts remote URLs.
ENV Sets an environment variable, available at build time and run time.
ARG Defines a build-time variable, passed via --build-arg. Not available at run time.
EXPOSE Documents the port the container listens on.
VOLUME Creates a mount point marked as holding externally mounted data.
WORKDIR Sets the working directory for following RUN, CMD, ENTRYPOINT, COPY, and ADD.
USER Sets the username or UID used to run the image.
LABEL Adds metadata to the image.
ONBUILD Registers an instruction to run later, when this image is used as a base.
HEALTHCHECK Defines how the daemon tests that the container is still working.

MAINTAINER appears in older material but is deprecated. Use a label instead: LABEL maintainer="you@example.com".

EXPOSE does not publish a port

This trips people up. EXPOSE is documentation — it records the port the application listens on. It does not make the port reachable from the host. Publishing requires -p at run time, or -P to map all exposed ports to random host ports.

COPY vs ADD

Both copy files from the build context into the image.

  • COPY copies files and directories, and nothing more.
  • ADD also auto-extracts local tar archives and can fetch remote URLs.

Prefer COPY. Its behaviour is predictable, whereas ADD can surprise you by unpacking an archive you intended to copy intact. Use ADD deliberately when you want tar extraction; for remote files prefer an explicit RUN curl, so the download and its cleanup occupy one layer.

RUN vs CMD vs ENTRYPOINT

These three are routinely confused, and the distinction is about when and how they execute.

RUN is a build step. Its result is committed into the image as a layer. A Dockerfile may have many RUN instructions, each layering on the last.

CMD is the default command run when a container launches from the image. A Dockerfile has only one effective CMD, and it is overridden by any command supplied on the docker run line:

docker run myimage              # runs CMD
docker run myimage /bin/bash    # overrides CMD entirely

ENTRYPOINT also defines what runs at startup, but is not casually overridden — arguments given to docker run are appended to it as parameters rather than replacing it. It has two forms:

ENTRYPOINT ["executable", "param1", "param2"]   # exec form, preferred
ENTRYPOINT command param1 param2                # shell form

The exec form is preferred because it runs the executable as PID 1 directly, so it receives signals such as SIGTERM and shuts down cleanly. The shell form wraps the command in /bin/sh -c, which swallows signals and leads to containers that take ten seconds to stop.

The idiomatic combination is ENTRYPOINT for the fixed executable and CMD for the default arguments a user might wish to replace.