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
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
7 min read · Sep 19, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Sep 17, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Sep 17, 2026Related articles
Learn Docker registries, image tags, Docker Hub, private registries, login, push, pull, naming rules, CI/CD us...
2 min read · Sep 17, 2026
Learn Docker environment variables and secrets, including docker run -e, env files, Compose environment, secre...
2 min read · Sep 17, 2026
Prepare for Docker interviews with practical questions and real-time scenarios covering images, containers, Do...
2 min read · Sep 17, 2026
Learn Docker security best practices for images, containers, users, secrets, registries, scanning, least privi...
2 min read · Sep 17, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.