All Modules Why Containers Dockerfile Compose DevOps Lab عربي

Containerizing the App

Package the vault so it runs identically anywhere — killing "works on my machine" for good.

Module 8 · Docker & Compose, bridging into the DevOps Lab.

Intermediate Containers Includes Lab ~50 min

What You'll Learn

  • What a container is and why it ends "works on my machine"
  • Write a Dockerfile for the API and the web app
  • Keep images lean with .dockerignore and small base images
  • Run the whole stack together with Docker Compose
  • Hands-on lab: containerize both apps and run the vault entirely in Docker

Prerequisites: Module 7, Docker Desktop installed. This module bridges into the DevOps Lab: Docker and Compose modules.

Why Containers

Failure mode #6: "works on my machine." Your app runs for you because your laptop has the right Node version, the right packages, the right environment. A container packages the app with all of that — a sealed box that runs the same on your machine, a teammate's, a CI runner, and a production server.

TermPlain meaning
ImageA blueprint: your app + its runtime + dependencies, frozen.
ContainerA running instance of an image — an isolated mini-machine.
DockerfileThe recipe that builds the image, step by step.

The pipeline unlock

Once the vault is a container, every later step gets easy: CI builds the same image, and deploy just runs it. Containerizing is the hinge between "my project" and "a shippable product."

A Dockerfile per App

Each app in your monorepo gets its own Dockerfile. Here's the shape for the NestJS API — the assistant will fill in the specifics:

# api/Dockerfile FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build EXPOSE 3000 CMD ["npm", "run", "start:prod"]

A .dockerignore (like .gitignore) keeps node_modules and secrets out of the image, making builds faster and safer.

Keep images lean & safe

Use a small base (-alpine), install only production deps in the final image, and never bake .env secrets in. Ask the assistant for a multi-stage build if the image is big — and review it, just like any other diff.

Run It All With Compose

The vault is two apps (plus, later, a database). Docker Compose describes them in one file and starts them together with a single command — networked so web can talk to api.

# docker-compose.yml services: api: build: ./api ports: ["3000:3000"] env_file: ./api/.env web: build: ./web ports: ["5173:5173"] depends_on: [api]
docker compose up --build # start the whole stack docker compose down # stop it

This is exactly the DevOps Lab pattern

The DevOps Lab Compose module walks the same idea in depth (app + database + monitoring). We reuse it here so you're not learning Compose twice.

Where This Goes Next

A containerized app is deployable anywhere that runs containers — a VM, Kubernetes, a managed host. That's the foundation for the last three modules: CI builds the image, deploy runs it, monitoring watches it. Everything downstream assumes this box exists.

Practical Lab: Containerize the Vault

You'll add a Dockerfile to each app and a Compose file, then run the whole snippet vault in Docker — no local Node needed.

What you need

Your snippet-vault repo from Module 7 and Docker Desktop running.

1

Branch and write Dockerfiles

git checkout -b feature/docker

Ask the assistant for a Dockerfile and .dockerignore for api/ and for web/, using small alpine base images. Review each diff — check no secrets are copied in.

2

Write the Compose file

Create docker-compose.yml that builds api and web, maps their ports, loads api/.env, and lets web reach api. One command to start everything.
3

Build and run

docker compose up --build

Open the app in the browser. Create, list, search, and delete a snippet — all running inside containers.

4

Prove the "any machine" promise

Stop your locally-run Node processes entirely. The app should still work through Compose — because everything it needs is in the containers, not your machine.

5

Merge and reflect

git add . git commit -m "Containerize api and web; add docker-compose" git checkout main && git merge feature/docker

In REFLECTION.md: what was in your environment that the container now provides for you? Commit it.

What to hand in

Your snippet-vault repo. Self-check:

  • A Dockerfile + .dockerignore for both api/ and web/
  • docker compose up runs the whole app; it works in the browser
  • No secrets baked into images
  • REFLECTION.md notes what the container replaced

Mini Glossary

TermPlain meaning
ImageA frozen blueprint of an app and everything it needs to run.
ContainerA running, isolated instance of an image.
DockerfileThe step-by-step recipe to build an image.
ComposeA tool to define and run multiple containers together.
.dockerignoreFiles to exclude from the image build (like node_modules).

Recap & What's Next

You now have

The whole snippet vault packaged into containers and running with one Compose command — identical everywhere. "Works on my machine" is dead.

Next up: Module 9 — CI/CD Pipeline. We put the tests, gates, and image build on autopilot with GitHub Actions, so every push is checked and built automatically.

Containerizing the App

Objectives Why Containers Dockerfile Compose DevOps Lab Practical Lab Glossary Recap