What is a Dockerfile?
A Dockerfile is a plain text file with a list of instructions that tells Docker how to build an image.
Think of it like a recipe 📝:
- It says:
- “Start from this base system”
- “Install this software”
- “Copy these files”
- “Run this command”
Then Docker reads that recipe and builds a Docker image (a snapshot of your app + environment).
You can then run containers from that image
Simple Example: Dockerfile to run a website
Step 1: Dockerfile
# Use official Nginx image
FROM nginx:latest
# Remove default nginx page
RUN rm -rf /usr/share/nginx/html/*
# Copy your custom website files
COPY ./website/ /usr/share/nginx/html/
# Expose port 80
EXPOSE 80
# Run Nginx in foreground
CMD ["nginx", "-g", "daemon off;"]
This tells Docker:
- Start from Nginx.
- Copy my website files.
- Listen on port 80.
Key Dockerfile Commands:
| Command | Meaning |
|---|---|
FROM | Start from this base image |
COPY | Copy files into the image |
RUN | Run a command inside the image |
EXPOSE | Tell Docker which port is used |
CMD | Command to run when container starts |
In simple words:
- Dockerfile = “Recipe”
- Docker image = “Dish prepared using recipe”
- Docker container = “Running the dish (app) on table” 🍽️
Step 2: index.html (with title “Website Post”)
index.html (with title “Post Title”)
Save this inside the website folder:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Post Title</title>
</head>
<body>
<h1>Welcome to Post Title Website!</h1>
<p>This site is running in a Docker container!</p>
</body>
</html
Step 3: Folder structure
.
├── Dockerfile
└── website
└── index.html
Step 4: Build & Run
# Build the Docker image
docker build -t website-post .
# Run the container
docker run -d -p 8080:80 website-post
Now open http://localhost:8080 and you will see “Website Post” as the tab title and heading!