Docker Container ImagesDockerfile

Article & Tutorial

Dockerfile Instructions Explained: FROM, RUN, COPY, CMD, ENTRYPOINT and More

Use FROM, WORKDIR, COPY, RUN, EXPOSE, ENV, CMD, ENTRYPOINT, ARG, USER, and HEALTHCHECK to build secure, efficient container images.

Dockerfile instructions for building container images
Dockerfile instructions for building container images
Contents

Dockerfile Instructions Explained

A Dockerfile is the build recipe for a Docker image. This guide explains the most important Dockerfile instructions with examples, syntax, common mistakes, and best practices.

Quick Dockerfile example

FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --omit=dev COPY . . EXPOSE 3000 CMD ["node", "server.js"]

FROM

FROM selects the base image. Every Dockerfile normally starts with a base image.

FROM nginx:alpine FROM node:20-alpine FROM python:3.12-slim

WORKDIR

WORKDIR sets the working directory inside the image.

WORKDIR /app

COPY and ADD

COPY copies files from your build context into the image. Prefer COPY for normal file copying.

COPY package*.json ./ COPY src/ ./src/

RUN

RUN executes commands during image build. It is used for installing packages and preparing the image.

RUN npm ci --omit=dev RUN apt-get update && apt-get install -y curl

EXPOSE

EXPOSE documents which port the containerized app listens on. It does not publish the port by itself.

EXPOSE 3000

CMD

CMD defines the default command when a container starts.

CMD ["node", "server.js"]

ENTRYPOINT

ENTRYPOINT defines the main executable. It is often used when the container behaves like a command-line tool.

ENTRYPOINT ["nginx", "-g", "daemon off;"]

ENV and ARG

ARG is available at build time. ENV is available in the image and container environment.

ARG APP_VERSION=dev ENV NODE_ENV=production

USER

USER runs the container process as a non-root user when configured correctly.

USER node

HEALTHCHECK

HEALTHCHECK tells Docker how to test if a container is healthy.

HEALTHCHECK --interval=30s --timeout=5s CMD curl -f || exit 1

.dockerignore

A .dockerignore file prevents unnecessary files from being sent to the Docker build context.

node_modules .git .env coverage Dockerfile*

Common mistakes

  • Copying the entire project before installing dependencies, causing slow rebuilds.
  • Committing secrets into images.
  • Using very large base images without reason.
  • Running everything as root.
  • Forgetting .dockerignore .

Best practices

  • Use small official base images.
  • Pin important versions where possible.
  • Copy dependency files first for better build cache.
  • Use non-root users for app containers where practical.
  • Keep one main process per container.
  • Use multi-stage builds for compiled apps.

Related Melochip guides

  • Docker Learning Path
  • Docker Images vs Containers Explained
  • GitLab Docker Build Pipeline

Official references

  • Dockerfile reference

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.