Containerizing Services
Think of constructing a car: you’ve designed the blueprint (setup), but now you need to assemble the engine, body, and wheels. In the Capstone Project, this means building and containerizing services — writing Dockerfiles, building images, and ensuring each service runs independently yet works together.
Containerizing Foundations
1. What Does “Containerizing” Mean?
- Definition: Packaging an application and its dependencies into a Docker image.
- Goal: Ensure consistency across environments (dev, test, prod).
- Workflow:
- Write Dockerfile.
- Build image (
docker build). - Run container (
docker run). - Test functionality.
2. Services in the Capstone Project
- Frontend Service: Web UI (React/Angular/Vue).
- Backend Service: API logic (Node.js/Flask/Spring Boot).
- Database Service: Persistent storage (Postgres/MySQL/MongoDB).
- Optional Services: Cache (Redis), message broker (RabbitMQ).
3. Best Practices for Containerizing
- Use multi‑stage builds to reduce image size.
- Pin versions for reproducibility.
- Expose only necessary ports.
- Use environment variables for configuration.
- Keep Dockerfiles clean and minimal.
Things to Remember
- Each service gets its own Dockerfile.
- Building images ensures portability.
- Containers isolate services but can communicate via Docker Compose.
Hands‑On Lab
Step 1: Frontend Dockerfile (React Example)
# Build stage
FROM node:16 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Runtime stage
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Step 2: Backend Dockerfile (Node.js Example)
FROM node:16
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD ["node", "server.js"]
Step 3: Database Dockerfile (Postgres Example)
FROM postgres:13
ENV POSTGRES_USER=admin
ENV POSTGRES_PASSWORD=secret
ENV POSTGRES_DB=capstone
EXPOSE 5432
Step 4: Build Images
docker build -t capstone-frontend ./frontend
docker build -t capstone-backend ./backend
docker build -t capstone-db ./database
Step 5: Run Containers Individually
docker run -p 3000:80 capstone-frontend
docker run -p 5000:5000 capstone-backend
docker run -p 5432:5432 capstone-db
Step 6: Orchestrate with Compose
version: '3.8'
services:
frontend:
build: ./frontend
ports:
- "3000:80"
backend:
build: ./backend
ports:
- "5000:5000"
environment:
- DB_HOST=database
- DB_USER=admin
- DB_PASSWORD=secret
depends_on:
- database
database:
build: ./database
ports:
- "5432:5432"
Run all services together:
docker-compose up --build
Practice Exercise
- Write Dockerfiles for your chosen frontend, backend, and database.
- Build images for each service.
- Run containers individually to test functionality.
- Create a
docker-compose.ymlto orchestrate services. - Run
docker-compose upand verify services communicate correctly. - Reflect on how containerization ensures consistency across environments.
Visual Learning Model
Building & Containerizing Services
├── Frontend → Dockerfile + image
├── Backend → Dockerfile + image
├── Database → Dockerfile + image
├── Compose → orchestrate services
└── Run → docker-compose up
The Hackers Notebook
Building and containerizing services transforms the Capstone Project from setup into a working application. Each service is packaged into a Docker image, tested individually, and orchestrated together with Docker Compose. This ensures consistency, portability, and scalability across environments.
