Write Your First Dockerfile (Beginners)
Read this guide in: English · मराठी · हिंदी
Why do we need a Dockerfile? Because there is a real problem between developers and operations. Developers have an index.html (or app) that looks fine on the laptop. Operations — the employee on the server side — asks how to run the same thing on EC2. Copying files is not enough; the server needs the runtime bits too. A Dockerfile is the shared recipe: docker build creates an image, docker run starts a container.
Friends, watch one file become a running site. Names come after the fight.
Quick answer
Hold this vertical picture:
- Developer — owns the app files.
- Operations — server-side person who must run the same site on EC2.
- The problem — file copy without a recipe; packages installed by hand; drift next week.
- Dockerfile — FROM → COPY → EXPOSE → CMD; then build and run.
- Pocket line: file → Dockerfile → image → container.
Language: English · मराठी · हिंदी
Why do we need a Dockerfile?
- Developers need a repeatable “how this app runs.”
- Operations on the server side needs the same description — not tribal knowledge.
- Without a recipe, every host becomes a special snowflake.
Need Docker working (install Docker on EC2).
What does operations have to do with the Dockerfile?
- Operations still SSHs, checks health, opens ports carefully.
- They run a tagged image, not a mystery folder.
- Developers update the Dockerfile; both trust
myweb:1as “what we shipped.”
What is the problem? Suppose we are…
Suppose we are building Instagram (like updates)
Suppose a tiny static status page must match what the mobile app expects. Developer emails HTML. Operations installs nginx by hand on EC2. Next week the steps are forgotten. Users see a blank page.
Suppose we are working at Netflix and we are giving updates
Suppose every microservice must ship as a known image tag. Without a Dockerfile, “build on my laptop” is not what production runs.
Suppose you are working in Infosys on a client project
Suppose the client operations team says: “Give us a Dockerfile and a tag — we will not accept a zip of files.” That is the shared contract.
Dockerfile instructions → docker build creates a read-only image → docker run starts a container.
Terminal-style teaching diagram: Dockerfile lines, docker build -t myweb:1, then docker images.
How a Dockerfile answers that same fight
mkdir myweb && cd myweb
cat > index.html <<'EOF'
<!doctype html>
<html><body><h1>DevOps lab page</h1><p>Same bits on laptop and server</p></body></html>
EOF
cat > Dockerfile <<'EOF'
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
EOF
docker build -t myweb:1 .
docker run --rm -d -p 8080:80 --name myweb myweb:1
curl -s http://localhost:8080 | head
docker logs myweb
docker stop myweb
What each Dockerfile line means:
- FROM — start from a known base image.
- COPY — put your files into the image.
- EXPOSE — documents port 80 (still map with
-pat run). - CMD — process when the container starts.
Changed the HTML? Rebuild a new tag:
docker build -t myweb:2 .
docker run --rm -p 8080:80 myweb:2
Ravindra Bagale's Tip
Students change files and re-run an old container without rebuilding — same as operations copying an old zip. Edit → docker build → run the new tag. Lakshat theva.
Common errors — story pain then fix
| Pain from the stories | Likely cause | Fix |
|---|---|---|
| build cannot find Dockerfile | Wrong directory | cd to folder with Dockerfile |
| COPY failed | File outside context / wrong name | Keep files next to Dockerfile |
| Page not updating | Old image/tag | Rebuild + new tag |
| Port already allocated | 8080 busy | -p 8081:80 |
| Huge context upload | Junk folders sent | Add .dockerignore |
Learn it properly
Samajla ka? Why Dockerfile = shared recipe for developer and server-side operations; Instagram / Netflix / Infosys “suppose”; tag every build. Practise myweb:1 tonight.
Frequently asked questions
What is a Dockerfile?
A text file of instructions that docker build follows to create an image.
What is FROM?
FROM sets the base image you start from, for example a small Linux or language runtime.
COPY vs ADD?
Prefer COPY for local files. ADD has extra magic most beginners do not need.
Why does build context matter?
docker build sends the folder (.) to the daemon. Keep the context small and use .dockerignore.
What does EXPOSE do?
It documents a port. You still map ports with -p when you run.
CMD vs ENTRYPOINT?
CMD is the default command; ENTRYPOINT is harder to override. Beginners usually start with CMD.