After learning Docker Images and Containers, the next step is understanding Dockerfiles. Dockerfiles are one of the most important concepts in Docker because they automate image creation.

Without Dockerfiles, creating Docker Images manually would be difficult and inconsistent. Dockerfiles allow developers to define application environments using code.
What is a Dockerfile?
A Dockerfile is a text file containing instructions used by Docker to build an image.
Each instruction tells Docker how to construct the final image.
Source Code ↓ Dockerfile ↓ Docker Build ↓ Docker Image ↓ Docker Container
Dockerfiles make image creation repeatable, consistent and version controlled.
Why Do We Need Dockerfiles?
Suppose we want to deploy a Spring Boot application.
Without Dockerfiles, every image would require manual configuration.
Install Java Copy Application Configure Runtime Start Application
With a Dockerfile, Docker automatically performs all these steps.
Simple Dockerfile Example
FROM eclipse-temurin:21 WORKDIR /app COPY target/app.jar app.jar EXPOSE 8080 CMD ["java","-jar","app.jar"]
This Dockerfile creates an image capable of running a Spring Boot application.
Docker Build Process
Docker reads the Dockerfile line by line.
Dockerfile ↓ Instruction 1 ↓ Instruction 2 ↓ Instruction 3 ↓ Docker Image
Each instruction generates a new layer inside the image.
Understanding Common Dockerfile Instructions
FROM
The FROM instruction specifies the base image.
FROM ubuntu:24.04
Every Dockerfile must begin with a FROM statement.
Examples:
FROM ubuntu FROM nginx FROM mysql FROM eclipse-temurin:21
Think of FROM as the foundation of a building.
WORKDIR
WORKDIR sets the working directory inside the container.
WORKDIR /app
Any subsequent commands execute from this directory.
Equivalent Linux command:
cd /app
COPY
COPY copies files from the host machine into the Docker image.
COPY target/app.jar app.jar
The application JAR file becomes available inside the container.
RUN
RUN executes commands during image creation.
RUN apt-get update
Common examples:
RUN apt-get update RUN apt-get install -y curl RUN mkdir logs
RUN executes when the image is being built.
EXPOSE
EXPOSE documents which port the application will use.
EXPOSE 8080
Examples:
EXPOSE 80 EXPOSE 443 EXPOSE 8080 EXPOSE 3306
EXPOSE does not publish the port automatically.
CMD
CMD specifies the command executed when the container starts.
CMD ["java","-jar","app.jar"]
Only one CMD instruction should typically exist.
The container executes this command when launched.
Spring Boot Dockerfile Example
FROM eclipse-temurin:21 WORKDIR /app COPY target/app.jar app.jar EXPOSE 8080 CMD ["java","-jar","app.jar"]
Explanation:
- Use Java 21 base image
- Create /app directory
- Copy Spring Boot JAR
- Expose port 8080
- Start the application
Building an Image
After creating the Dockerfile:
docker build -t springboot-app .
Docker reads the Dockerfile and creates an image.
Running the Container
docker run springboot-app
Docker creates a container from the image and starts the application.
Layered Architecture of Docker Images
Every Dockerfile instruction creates a layer.
FROM ↓ Layer 1 COPY ↓ Layer 2 RUN ↓ Layer 3 CMD ↓ Final Image
Docker caches layers to improve build performance.
Docker Build Workflow
Source Code + Dockerfile ↓ docker build ↓ Docker Image ↓ docker run ↓ Container Running
Real World Example
Consider a company deploying a Spring Boot microservice.
Every environment:
Development Testing Production
Uses exactly the same Dockerfile.
This guarantees consistency and reduces deployment errors.
Most Common Dockerfile Instructions
| Instruction | Purpose |
|---|---|
| FROM | Base Image |
| WORKDIR | Working Directory |
| COPY | Copy Files |
| RUN | Execute Build Commands |
| EXPOSE | Document Port |
| CMD | Start Application |
Common Interview Questions
What is a Dockerfile?
A Dockerfile is a text file containing instructions used to build Docker Images.
Why do we use Dockerfiles?
Dockerfiles automate image creation and ensure consistency across environments.
What does FROM do?
FROM defines the base image for the Docker build.
What is the difference between RUN and CMD?
RUN executes during image creation, while CMD executes when the container starts.
Which instruction starts the application?
CMD starts the application when the container launches.
Best Practices
- Use lightweight base images.
- Minimize the number of image layers.
- Use specific image versions.
- Keep Dockerfiles simple and readable.
- Avoid storing secrets in Dockerfiles.
Frequently Asked Questions (FAQ)
Can one Dockerfile create multiple containers?
Yes. A single image built from a Dockerfile can create multiple containers.
Can Dockerfiles be version controlled?
Yes. Dockerfiles are plain text files and are commonly stored in Git repositories.
Is Dockerfile mandatory?
No. But Dockerfiles are the standard and recommended approach for building images.
Conclusion
Dockerfiles are the foundation of reproducible Docker images. By defining image creation using code, Dockerfiles provide consistency, automation and portability across environments. Understanding Dockerfile instructions such as FROM, COPY, RUN, WORKDIR, EXPOSE and CMD is essential for modern application deployment and containerized development.
Keywords
Dockerfile, Dockerfile Tutorial, Dockerfile Example, Docker Build, Docker Image Creation, Spring Boot Dockerfile, Docker Commands, Docker Interview Questions, Docker RUN vs CMD, Docker EXPOSE, Containerization, DevOps Tutorial, Docker for Beginners, Microservices Deployment, Cloud Native Applications
Reviewed by 13thGhost
on
1:02 AM
Rating: