Docker Deep Dive 2: Dockerfiles, Multi-Stage Builds & Volumes
පිටුව 14: Docker රහස් 2: Dockerfiles ලිවීම, Multi-Stage Builds, Volumes සහ Networking
Dockerfile ලිවීම (FROM, WORKDIR, COPY, RUN, CMD vs ENTRYPOINT), Image Layers, Multi-Stage Builds (1GB -> 25MB), Persistent Volumes සහ Port Forwarding (-p).
Lab 14 of 20 · Docker Mastery · 5 hands-on activities · bilingual (English + සිංහල)
පිටුව 14: Docker රහස් 2: Dockerfiles ලිවීම, Multi-Stage Builds, Volumes සහ Networking
Dockerfile ලිවීම (FROM, WORKDIR, COPY, RUN, CMD vs ENTRYPOINT), Image Layers, Multi-Stage Builds (1GB -> 25MB), Persistent Volumes සහ Port Forwarding (-p).
💡 සරල සිංහලෙන්: Dockerfile, Layers සහ Volumes සරලව
කේක් හදන Recipe එක සහ කේක් එක (Image vs Container):
- Dockerfile: කේක් එක හදන Recipe පොත. මොන ingredients ද ඕන (FROM), කොහොමද කලවම් කරන්නේ (RUN) කියලා ලියලා තියෙනවා.
- Docker Image: කේක් එක හදන අච්චුව (Mould). මේක වෙනස් කරන්න බෑ (Read-only).
- Docker Container: ඒ අච්චුවෙන් හදපු ඇත්තම කේක් එක! අපිට එකම Image අච්චුවෙන් Containers 100ක් වුණත් තත්පරයෙන් හදන්න පුළුවන්.
- Multi-Stage Builds: කේක් එක හදන්න ගෙනාපු පිටි මෝල්, ලොකු මැෂින් ඔක්කොම පැත්තකට දාලා, අන්තිමට ලස්සන කේක් පෙට්ටිය විතරක් පාරිභෝගිකයාට දෙනවා වගේ. Image size එක 1GB ඉඳන් 25MB වෙනකම් අඩු වෙනවා!
- Volumes: Container එක delete කළත් අපේ Database එකේ දත්ත මැකෙන්නේ නැතුව Hard disk එකේ තියාගන්න දාන Folder එකයි Volume කියන්නේ.
Architectural Foundation & Systems Diagram
Packaging microservices cleanly requires mastering the Dockerfile, understanding image layer caching, and safely mounting persistent storage volumes.
-
📄 Dockerfile Anatomy — •
FROM: Base OS or runtime (e.g.alpine:3.19).
•WORKDIR: Active working folder.
•COPY: Transfer source code.
•RUN: Build/compile instructions.
•CMD: Default execution command. -
📦 Multi-Stage Builds — Separate the build environment (with compilers, JDK, npm) from the runtime environment (minimal Alpine with only the compiled binary). Shrinks image sizes from 1.2 GB down to 25 MB!
-
💾 Volumes & Networks — Containers are ephemeral (data is lost on exit). Use
-v /host/path:/container/pathfor persistent data (like databases), and-p 8080:80to forward host ports into container ports.
Step-by-step practical lab
Step 01: Launch Detached Web Container with Port Forwarding
සිංහලෙන් — Port Forwarding සහිතව පසුබිමේ (Detached) Nginx Web Container එකක් run කරමු.
Run an Nginx container in background detached mode (-d), binding host port 8080 to container port 80 (-p 8080:80).
docker run -d -p 8080:80 nginx-d: Detached mode: Container එක පසුබිමේ (background) ධාවනය කරයි.-p 8080:80: Port forwarding: Host මැෂින් එකේ 8080 වරාය Container එකේ 80 වරායට සම්බන්ධ කරයි.
Expected output
e3f89a21b402847291a24d081f7294bbca841029412958471204859182937401
Container launched successfully in detached mode (-d) with port binding 0.0.0.0:8080->80/tcpSRE Tip: The 64-character alphanumeric string returned is the unique Container ID assigned by the container runtime.
Step 02: Inspect Running Docker Containers Status
සිංහලෙන් — Server එකේ සජීවීව දුවන Docker Containers ලැයිස්තුව පරීක්ෂා කරමු.
List all active running Docker containers with container ID, image name, port bindings, and uptime.
docker psps: ක්රියාත්මක වන containers පෙන්වයි. නැවතුණු සියල්ල බැලීමට -a යොදන්න.
Expected output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3f89a21b402 nginx:alpine "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8080->80/tcp web-frontend
9a81b2c4d5e6 redis:7-alpine "docker-entrypoint.s…" 5 hours ago Up 5 hours 6379/tcp cache-redisSRE Tip: If a container dies immediately upon startup, run 'docker ps -a' and check its Exit Code (e.g. Exit 137 = OOM killed).
Step 03: Query Cached Local Docker Images
සිංහලෙන් — පරිගණකයේ තැන්පත් වී ඇති Docker Images ලැයිස්තුව බලමු.
Query all container image blueprints, repository tags, image IDs, and sizes cached on the local Docker engine.
docker imagesimages: Local disk එකේ ඇති සියලුම container blueprints පෙන්වයි.
Expected output
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine 9a4c1070e632 3 days ago 41.2MB
redis 7-alpine 178ec6b51347 1 week ago 38.6MBSRE Tip: Alpine-based images are ~40MB, whereas Ubuntu/Debian base images are 150MB+. Smaller images reduce vulnerability attack surface!
Hands-on practice activities
Work through these yourself — tick them off as you go.
- Activity 66: Query Cached Local Docker Images (Drill) — Query all container image blueprints, repository tags, image IDs, and sizes.
- Activity 67: List Active Running Containers (Drill) — View active containers, their container IDs, images, status, and mapped port bindings.
- Activity 68: Launch Detached Web Container with Port Forwarding (Drill) — Launch an Nginx container in background detached mode forwarding host port 8080 to container port 80.
- Activity 69: CMD vs ENTRYPOINT Distinction (Checkpoint) — ENTRYPOINT specifies the immutable command binary; CMD specifies default arguments that can be overridden at runtime.
- Activity 70: Multi-Stage Build Benefit Quiz (Quiz) — What is the primary benefit of Docker multi-stage builds? Answer: Dramatically reducing production container image size and attack surface.
Keep going: ← Lab 13 · Lab 15 → · Track your progress in the Self Guide
ExerciseKnowledge check
What is the primary benefit of Docker multi-stage builds? / Docker multi-stage builds භාවිතයේ ප්රධාන වාසිය කුමක්ද?
Finished this one?
Part of Self Guide · Stage 11: SRE Labs II — cloud, containers and delivery. Progress is saved in this browser.


