What is a Dockerfile?
A Dockerfile is a text file that contains a set of instructions used to build a Docker Image automatically. It defines the application environment, dependencies, runtime, configurations, and startup commands required to run an application inside a Docker Container.
In modern DevOps, cloud-native architecture, CI/CD pipelines, Kubernetes deployments, and microservices-based systems, Dockerfiles are one of the most important components for containerization and automated deployments.
Real-Time Analogy
Think of a Dockerfile like a cooking recipe.
Recipe (Dockerfile)
|
v
Prepared Food (Docker Image)
|
v
Served Dish (Running Container)
The Dockerfile tells Docker Engine exactly how to build the application image.
Why Dockerfile is Important
Before Dockerfiles, developers manually configured servers:
- Install Java
- Install dependencies
- Copy application files
- Configure environment variables
- Start services manually
This caused:
- Environment inconsistencies
- Deployment failures
- Manual configuration errors
- Slow infrastructure setup
Dockerfile solved these problems by automating image creation.
โInfrastructure becomes code.โ
How Dockerfile Fits in Docker Architecture
Application Source Code
|
v
Dockerfile
|
v
docker build command
|
v
Docker Image
|
v
Docker Container
|
v
Running Application
Basic Dockerfile Example
FROM eclipse-temurin:17-jdk
WORKDIR /app
COPY target/payment-service.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
This Dockerfile builds a Spring Boot application image.
How Dockerfile Works Internally
Docker Engine reads the Dockerfile line by line and creates image layers.
Internal Build Process
Step 1:
Read Dockerfile
Step 2:
Pull base image
Step 3:
Execute instructions one by one
Step 4:
Create image layers
Step 5:
Generate final Docker Image
Dockerfile Architecture
+------------------------------------------------------+
| Dockerfile |
+------------------------------------------------------+
| FROM |
| WORKDIR |
| COPY |
| RUN |
| ENV |
| EXPOSE |
| ENTRYPOINT |
+------------------------------------------------------+
|
v
+------------------------------------------------------+
| Docker Engine |
+------------------------------------------------------+
|
v
+------------------------------------------------------+
| Docker Image |
+------------------------------------------------------+
|
v
+------------------------------------------------------+
| Docker Container |
+------------------------------------------------------+
Main Dockerfile Instructions
| Instruction | Purpose |
|---|---|
| FROM | Base image |
| WORKDIR | Set working directory |
| COPY | Copy files into image |
| RUN | Execute commands during build |
| CMD | Default startup command |
| ENTRYPOINT | Main executable command |
| ENV | Environment variables |
| EXPOSE | Declare container port |
| VOLUME | Persistent storage |
1. FROM Instruction
FROM specifies the base image.
Example
FROM ubuntu:22.04
OR
FROM eclipse-temurin:17-jdk
Every Dockerfile usually starts with FROM.
Why Important?
- Provides operating system layer
- Provides runtime environment
- Provides dependencies
2. WORKDIR Instruction
WORKDIR sets the working directory inside the container.
Example
WORKDIR /app
All future commands execute inside this directory.
3. COPY Instruction
COPY copies files from host machine into Docker Image.
Example
COPY target/payment-service.jar app.jar
This copies Spring Boot JAR file into image.
4. RUN Instruction
RUN executes commands during image build time.
Example
RUN apt-get update && apt-get install -y curl
This installs packages inside image.
5. ENV Instruction
ENV sets environment variables.
Example
ENV SPRING_PROFILES_ACTIVE=prod
Environment variables help configure applications dynamically.
6. EXPOSE Instruction
EXPOSE documents which port the container uses.
Example
EXPOSE 8080
This indicates the application runs on port 8080.
7. CMD Instruction
CMD defines the default command executed when container starts.
Example
CMD ["java", "-jar", "app.jar"]
8. ENTRYPOINT Instruction
ENTRYPOINT defines the main executable command.
Example
ENTRYPOINT ["java", "-jar", "app.jar"]
ENTRYPOINT is commonly used in production systems.
Complete Production Dockerfile Example
Spring Boot Microservice
FROM maven:3.9.6-eclipse-temurin-17 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests
FROM eclipse-temurin:17-jdk-jammy
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
What Happens Internally During docker build?
Command
docker build -t payment-service:1.0 .
Internal Flow
Step 1:
Docker Engine reads Dockerfile
Step 2:
Pull base image
Step 3:
Execute each instruction
Step 4:
Create image layers
Step 5:
Cache reusable layers
Step 6:
Generate final image
Docker Image Layers
Every Dockerfile instruction creates a new image layer.
Base OS Layer
|
Java Runtime Layer
|
Dependency Layer
|
Application Layer
Benefits
- Faster builds
- Efficient storage
- Layer caching
- Faster deployments
Real-Time Production Example
Consider a global online learning platform serving users from USA, UK, and India.
Microservices:
API Gateway
Course Service
Interview Service
Payment Service
Notification Service
Assessment Service
Each service has its own Dockerfile.
course-service/Dockerfile
payment-service/Dockerfile
interview-service/Dockerfile
CI/CD Flow
Developer Pushes Code
|
v
CI/CD Pipeline
|
v
Dockerfile Builds Image
|
v
Push Image to Registry
|
v
Kubernetes Deploys Containers
Multi-Stage Docker Builds
Multi-stage builds reduce final image size significantly.
Without Multi-Stage Build
Final Image Includes:
- Maven
- Build tools
- Source code
- Temporary files
- Application JAR
With Multi-Stage Build
Final Image Includes Only:
- Runtime
- Application JAR
This improves:
- Security
- Performance
- Storage efficiency
Dockerfile Best Practices
- Use lightweight base images
- Use multi-stage builds
- Minimize image layers
- Avoid unnecessary packages
- Use specific image versions
- Do not run containers as root
- Use .dockerignore
- Keep images small
Use .dockerignore File
.dockerignore prevents unnecessary files from entering Docker build context.
Example
target/
.git/
.idea/
node_modules/
logs/
This improves build speed significantly.
Dockerfile Security Best Practices
- Use official base images
- Use minimal OS distributions
- Scan images for vulnerabilities
- Avoid hardcoded passwords
- Use non-root users
- Keep dependencies updated
Production Scaling Scenario
During Black Friday in USA or Diwali sales in India:
Normal Traffic:
2 payment containers
Heavy Traffic:
20 payment containers
Dockerfiles allow Kubernetes to quickly create additional containers using prebuilt Docker Images.
Dockerfile vs Docker Compose
| Feature | Dockerfile | Docker Compose |
|---|---|---|
| Purpose | Build Docker Image | Run Multiple Containers |
| Focus | Single Image Creation | Application Orchestration |
| File Type | Dockerfile | docker-compose.yml |
Dockerfile in Kubernetes
Kubernetes deployments heavily depend on Docker Images built from Dockerfiles.
Dockerfile
|
v
Docker Image
|
v
Container Registry
|
v
Kubernetes Deployment
Common Production Problems Solved by Dockerfile
| Problem | Dockerfile Solution |
|---|---|
| Environment inconsistency | Automated builds |
| Manual server setup | Infrastructure as code |
| Dependency conflicts | Container isolation |
| Deployment failures | Consistent images |
| Slow scaling | Prebuilt images |
Interview Answer (Short Version)
A Dockerfile is a text file containing instructions used to build a Docker Image. It defines the application environment, dependencies, runtime, configurations, and startup commands required for running containers.
Docker Engine reads the Dockerfile instruction by instruction and creates image layers automatically. Dockerfiles are widely used in DevOps, CI/CD pipelines, Kubernetes, and microservices architecture for automated deployments.
Important Dockerfile Commands
docker build -t app:1.0 .
docker images
docker run app:1.0
docker push app:1.0
Useful Internal Links
- Docker Interview Questions
- DevOps Interview Questions
- Microservices Interview Questions
- Kubernetes Interview Questions
- AWS Interview Questions
- Explore Career Development Courses
Final Conclusion
Dockerfile is one of the most important components in Docker and modern DevOps. It enables automated, repeatable, scalable, and production-ready container image creation.
Dockerfiles help organizations standardize deployments, simplify CI/CD pipelines, improve scalability, and support cloud-native architectures used in modern enterprise systems worldwide.