Day 2/40 - How To Dockerize a Project - CKA Full Course 2025
Overview
This video is Day 2 of the 40-day CKA preparation course. It transitions from Docker theory (Day 1) to hands-on practice by walking through how to containerize a real-world application. The instructor demonstrates writing a Dockerfile, building an image, running a container, and troubleshooting common Dockerization pitfalls.
Source Details
- Channel: Tech Tutorials with Piyush
- Playlist: 40 Days of Kubernetes (CKA Full Course)
- Companion Repository: piyushsachdeva/CKA-2024
- Challenge:
#40daysofKubernetes
Key Takeaways
1. What Does “Dockerize” Mean?
- Dockerizing is the process of packaging an application and all its dependencies into a Docker image so it can run consistently anywhere.
- It eliminates “works on my machine” problems by ensuring the runtime environment is identical across dev, staging, and production.
2. The Dockerfile Anatomy
A Dockerfile is a text document containing instructions to build a Docker image. Key instructions covered:
| Instruction | Purpose | Example |
|---|---|---|
FROM | Base image to start from | FROM node:18-alpine |
WORKDIR | Set working directory inside container | WORKDIR /app |
COPY | Copy files from host to container | COPY package*.json ./ |
RUN | Execute commands during build | RUN npm install |
EXPOSE | Document which port the container listens on | EXPOSE 3000 |
CMD | Default command to run when container starts | CMD ["node", "server.js"] |
3. Dockerize Workflow (Step-by-Step)
- Create a
Dockerfilein the project root. - Choose a base image (prefer official, slim, or Alpine variants for smaller size).
- Set
WORKDIRto keep the file system organized. - Copy dependency files first (leveraging Docker layer caching).
- Install dependencies with
RUN. - Copy application source code.
- Expose the application port.
- Define the startup command with
CMD.
4. Layer Caching & Build Optimization
- Docker builds images in layers — each instruction creates a cached layer.
- If a layer changes, all subsequent layers must be rebuilt.
- Optimization trick: Copy
package.json/requirements.txtbefore the full source code so dependency installation is cached and only rebuilds when dependencies change.
5. Build & Run Commands
| Command | Purpose |
|---|---|
docker build -t myapp:1.0 . | Build image with tag myapp:1.0 from current directory |
docker run -d -p 8080:3000 myapp:1.0 | Run container in detached mode, map host port 8080 to container port 3000 |
docker run -it --rm myapp:1.0 /bin/sh | Run interactively with shell access, auto-remove on exit |
docker logs <container_id> | View application logs |
6. Common Dockerization Pitfalls
- Forgetting
.dockerignore: Large files (node_modules, .git) bloat the build context and slow builds. - Using
latesttag: Non-reproducible builds; always pin to specific versions (node:18-alpine, notnode:latest). - Running as root: Security risk; use
USERinstruction to run as non-root. - Hardcoding config: Use environment variables (
ENV) for configuration that changes per environment. - Not handling signals: Containers should gracefully handle SIGTERM; use
execin entrypoint scripts.
7. Multi-Stage Builds (Preview)
- Mentioned as a best practice for production images.
- Build dependencies in one stage, copy only the compiled artifact to a smaller runtime stage.
- Significantly reduces final image size and attack surface.
Cross-References
Ingested: 2026-05-21