dockerfile
By Lakshay Jain. Published November 23, 2026. 2 min read.
Topics: Technology.
This story was written with AI help.
What Is a Dockerfile?
A Dockerfile is a plain-text script that defines how to build a Docker image. Each instruction in the file tells Docker what base image to use, which files to copy, which commands to run, and how to configure the final container. By automating these steps, Dockerfiles make it easy to reproduce environments, share them with teammates, and deploy applications consistently across machines.
Core Dockerfile Instructions
The most common instructions you will see in a Dockerfile are:
- FROM : specifies the base image.
- LABEL : adds metadata such as maintainer information.
- ENV : defines environment variables.
- RUN : executes commands during the image build.
- COPY and ADD : copy files from the build context into the image.
- WORKDIR : sets the working directory for subsequent instructions.
- EXPOSE : documents which ports the container will listen on.
- CMD and ENTRYPOINT : define the default command that runs when the container starts.
Writing an Effective Dockerfile
1. Choose a Minimal Base Image
Start with the smallest possible base that still supports your application. Alpine Linux, for example, reduces image size and speeds up transfers. Use official images when possible to benefit from security updates.
2. Order Instructions for Caching
Docker caches each layer after it is built. Place frequently changing commands (like copying source code) toward the end of the file, and keep stable commands (like installing system packages) at the top. This maximizes cache reuse and shortens rebuild times.
3. Keep Images Small
- Remove build-time dependencies after they are no longer needed.
- Combine related RUN commands with && to reduce the number of layers.
- Use --no-install-recommends (Debian/Ubuntu) or similar flags to avoid unnecessary packages.
4. Use .dockerignore
A .dockerignore file works like .gitignore . List files and directories that should not be sent to the Docker daemon during the build, such as node_modules .git and temporary build artifacts. This speeds up the build and prevents accidental inclusion of sensitive data.
5. Define a Clear Entry Point
Separate the container's runtime configuration ( CMD ) from its executable ( ENTRYPOINT ). This allows users to override arguments without replacing the main binary.
Sample Dockerfile for a Node.js Application
# Use an official lightweight Node runtime as the base FROM node:20-alpine # Add metadata LABEL maintainer="dev@example.com" # Set working directory inside the container WORKDIR /app # Install only production dependencies first (cached layer) COPY package*.json ./ RUN npm ci --only=production # Copy the rest of the source code COPY . . # Expose the default port EXPOSE 3000 # Define the command to run the app CMD ["node", "server.js"]
Best Practices Checklist
- Start from an official, minimal base image.
- Pin versions of base images and packages to ensure reproducibility.
- Leverage multi-stage builds to separate build and runtime environments.
- Use .dockerignore to exclude unnecessary files.
- Keep layers small and ordered for optimal caching.
- Document exposed ports and environment variables.
- Prefer ENTRYPOINT + CMD over a single CMD for flexibility.
- Scan built images for vulnerabilities with tools like Trivy or Docker Scout.
Conclusion
A well-crafted Dockerfile turns a complex setup process into a repeatable, version-controlled build. By following the guidelines above, choosing minimal bases, ordering instructions for cache efficiency, keeping images small, and using .dockerignore you can create images that are fast to build, secure, and easy to maintain. Mastering Dockerfiles is a key step toward reliable containerized deployments.