Dockerfile
Learn how Dockerfiles define container images, including base images, layers, copy steps, commands, ports, and build hygiene.
Last reviewed: June 2026
🔍 What Is It?
A Dockerfile is the build recipe for a Docker image.
Common instructions:
- →
FROMchooses the base image - →
WORKDIRsets the working directory - →
COPYadds files from the build context - →
RUNexecutes build-time commands - →
ENVdefines environment defaults - →
EXPOSEdocuments the port - →
CMDdefines the default runtime command
🎯 Why Do We Need It?
The Dockerfile makes runtime setup repeatable and reviewable.
Instead of installing packages manually on a server, the team can version the runtime alongside application code. Code reviewers can see exactly what goes into the image.
🏗️ What Happens in Real Projects?
A production Dockerfile usually evolves beyond the first tutorial version.
Teams add .dockerignore, non-root users, dependency caching, multi-stage builds, health checks, pinned base images, and minimal runtime images. CI then uses the Dockerfile as the single build contract.
⚙️ How It Works
Docker sends a build context to the builder, reads the Dockerfile instructions, and creates image layers.
Layer order matters. Put rarely changing dependency files before frequently changing source files so Docker can reuse cached layers.
For example, copy package.json first, install dependencies, then copy source code. A source-only change should not reinstall all dependencies.
💻 Example
Production-Friendly Dockerfile — A Node service Dockerfile with cache-aware ordering and a non-root runtime user.
🤔 Common Confusions
❓ What is the difference between `RUN` and `CMD`?
`RUN` executes during image build and creates a layer. `CMD` defines what runs when a container starts.
❓ Does `EXPOSE` publish the port?
No. `EXPOSE` documents the intended port. You still publish ports with `-p` or platform configuration.
❓ Why does Docker rebuild too much?
Layer cache was invalidated. Reorder instructions so stable dependency files are copied before frequently changing source files.
🚨 Common Production Issues
**Secrets copied into image** - `.env`, keys, or credentials enter the build context and image layers.
**Root runtime** - containers run as root even when the app does not need elevated privileges.
**Poor cache order** - every code change reinstalls dependencies and slows CI.
**Unpinned base image** - builds change unexpectedly when base tags move.
✅ Best Practices
Use `.dockerignore` in every project
Order layers for build cache efficiency
Run applications as a non-root user
Keep runtime images small
Avoid secrets in `ARG`, `ENV`, or copied files
Prefer exec-form `CMD` for signal handling
🏛️ Architect's Note
A Dockerfile is application infrastructure code. Treat it with the same review discipline as production code.
The best Dockerfiles are boring: predictable base image, clear dependency installation, no secrets, non-root runtime, small final image, and fast rebuilds.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Docker Technology Guide
Return to the Docker learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Python Developer Career Path
A practical route through Python fundamentals, automation, backend basics, testing, projects, interview preparation, and career growth.
Sources
Sources / References
Use these Docker references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Docker learning path
Browse the full Docker topic collection and learning order on SplashRide.
Docker Interview Prep
Practice Docker interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Python Developer Career Path
A practical route through Python fundamentals, automation, backend basics, testing, projects, interview preparation, and career growth.
Docker Images
Learn Docker images as layered, immutable artifacts that define the filesystem and startup defaults for containers.
Multi-Stage Builds
Use Docker multi-stage builds to compile applications in one stage and ship a smaller, cleaner runtime image.