अपना पहला Dockerfile लिखें (शुरुआती)
Read this guide in: English · मराठी · हिंदी
Why Dockerfile चाहिए? क्योंकि developers और operations के बीच असली problem है। Developers के पास index.html (या app) laptop पर ठीक दिखता है। Operations — server side वाला employee — पूछता है EC2 पर वही कैसे चलाएँ। Files copy काफ़ी नहीं; server को runtime bits भी चाहिए। Dockerfile shared recipe है: docker build → image, docker run → container।
संक्षिप्त उत्तर
- Developer — app files own करता है।
- Operations — server-side person जिसे EC2 पर वही site चलाना है।
- Problem — बिना recipe file copy; हाथ से packages; अगले हफ़्ते drift।
- Dockerfile — FROM → COPY → EXPOSE → CMD; फिर build और run।
- Pocket: file → Dockerfile → image → container.
Language: English · मराठी · हिंदी
Problem? Suppose we are…
Suppose we are building Instagram (like updates)
Tiny status page mobile app से मिलना चाहिए। Developer HTML email करता है। Operations EC2 पर हाथ से nginx। अगले हफ़्ते steps भूल जाते हैं।
Suppose we are working at Netflix and we are giving updates
हर microservice known image tag के रूप में ship होना चाहिए। Dockerfile न हो तो “laptop build” production नहीं।
Suppose you are working in Infosys on a client project
Client operations कहता है: “Dockerfile और tag दो — files की zip नहीं लेंगे।” वही shared contract।
Need Docker (install on EC2)।
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.
Dockerfile कैसे जवाब देता है
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
HTML बदला? नया tag: docker build -t myweb:2 .
Ravindra Bagale's Tip
Files बदलकर पुराना container चलाना = पुरानी zip copy। Edit → build → नया tag। याद रखो।
Common errors
| Pain | Fix |
|---|---|
| Dockerfile नहीं मिला | सही folder में cd |
| COPY failed | Files Dockerfile के पास |
| Page update नहीं | Rebuild + नया tag |
| Port busy | -p 8081:80 |
| Huge context | .dockerignore |
Learn it properly
Samajla ka? Why Dockerfile = developer और server-side की shared recipe; Instagram / Netflix / Infosys “suppose”; हर build tag करो। आज myweb:1।
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.