Important instructions used in dockerfile

Dockerfile is a text file containing a series of instructions that define how to build a Docker image. These instructions are executed sequentially when you run the docker build command, creating layers that form the final image. Understanding key Dockerfile instructions is essential for creating efficient, maintainable, and secure container images.

In this article, we'll explore the most important Dockerfile instructions that every developer should master to build optimized Docker images.

FROM

The FROM instruction specifies the base image for your Docker image and must be the first instruction in any Dockerfile (except for ARG instructions that can precede FROM).

FROM <image_name>:<tag>

Examples of FROM instructions:

FROM ubuntu:20.04
FROM python:3.9-slim
FROM node:16-alpine
FROM scratch

The scratch image is a special empty image used for building minimal images from scratch.

RUN

The RUN instruction executes commands in a new layer on top of the current image and commits the results. Each RUN instruction creates a new layer, so combining multiple commands into a single RUN instruction reduces the number of layers.

# Inefficient - creates multiple layers
RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install -y curl

# Efficient - single layer
RUN apt-get update && \
    apt-get install -y vim curl && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

CMD vs ENTRYPOINT

Both CMD and ENTRYPOINT define the default command to execute when the container starts, but they behave differently:

Instruction Behavior Override
CMD Provides default command/arguments Completely replaced by docker run arguments
ENTRYPOINT Sets fixed command that always executes Arguments appended to the entrypoint command
# CMD example
CMD ["echo", "Hello World"]

# ENTRYPOINT example
ENTRYPOINT ["echo", "Welcome to"]
CMD ["TutorialsPoint"]

WORKDIR

The WORKDIR instruction sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions. If the directory doesn't exist, it will be created automatically.

WORKDIR /usr/src/app
COPY . .
RUN npm install

COPY vs ADD

Both instructions copy files and directories from the build context to the image, but ADD has additional features:

Instruction Basic Copy Auto-extract Archives Remote URLs Recommendation
COPY ? ? ? Preferred for simple copying
ADD ? ? ? Use only when extra features needed
# COPY - preferred for local files
COPY package.json /usr/src/app/

# ADD - for archives or remote URLs
ADD https://example.com/file.tar.gz /tmp/
ADD archive.tar.gz /usr/src/app/

EXPOSE

The EXPOSE instruction documents which ports the container listens on at runtime. It doesn't actually publish the port?that requires the -p flag with docker run.

EXPOSE 3000
EXPOSE 8080/tcp
EXPOSE 53/udp

ENV and ARG

Both instructions handle variables, but serve different purposes:

# ARG - build-time variables
ARG NODE_VERSION=16
FROM node:${NODE_VERSION}

# ENV - runtime environment variables
ENV NODE_ENV=production
ENV PORT=3000

VOLUME

The VOLUME instruction creates a mount point and marks it as holding externally mounted volumes from the host or other containers.

VOLUME ["/data", "/logs"]
# or
VOLUME /var/lib/mysql

LABEL

The LABEL instruction adds metadata to an image using key-value pairs. This is useful for documentation, licensing, and automation.

LABEL version="1.0"
LABEL description="This is a sample web application"
LABEL maintainer="developer@example.com"

Best Practices Summary

  • Use .dockerignore to exclude unnecessary files from the build context

  • Leverage multi-stage builds to reduce final image size

  • Chain RUN commands to minimize layers

  • Use specific tags instead of latest for base images

  • Prefer COPY over ADD unless you need ADD's extra features

  • Clean up package manager caches in the same RUN instruction

Conclusion

Mastering these essential Dockerfile instructions is crucial for building efficient, secure, and maintainable Docker images. Proper use of FROM, RUN, CMD/ENTRYPOINT, COPY/ADD, and other instructions helps create optimized containers that follow Docker best practices and reduce deployment complexity.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements