Skip to content

Latest commit

 

History

History
163 lines (130 loc) · 11.3 KB

README.md

File metadata and controls

163 lines (130 loc) · 11.3 KB

Containerize a Legacy App

Level: Beginner
Estimated time: 1 week
Skills: Docker, Docker Compose, container security basics, environment-based configuration, GitHub Actions, image scanning
AWS services: Amazon ECR (optional)

Background

ShopNest has a small internal API called orders-report. It is a Python Flask app that reads order data from PostgreSQL and returns daily sales summaries to the finance team. One developer wrote it three years ago, and it only runs on that developer's laptop.

Last month that developer went on leave and the finance team could not get their reports for a week. A second developer tried to run the app and lost two days to problems like these:

  1. The app needs a specific Python version that is not written down anywhere.
  2. Some dependencies are installed globally on the original laptop and missing from requirements.txt.
  3. The database host, user and password are hard-coded in config.py.
  4. PostgreSQL must be installed locally with a particular version and a manually created database.
  5. Nobody knows which operating system packages the app needs.

The engineering lead wants the app containerized so any developer can run it with one command, and so it can later move to a container platform. The security team asked that images be scanned before they are published.

Your task

Package orders-report as a secure, small container image and provide a Docker Compose setup that runs the app with PostgreSQL locally. Build a GitHub Actions pipeline that builds the image, scans it with Trivy, fails on critical vulnerabilities and pushes passing images to a registry tagged with the git commit SHA.

You can write your own simple Flask (or Node.js) API with two or three endpoints that read and write to PostgreSQL. The app itself is not graded. The packaging is.

Before you start

  • Docker Desktop or Docker Engine with Compose v2 installed locally. No AWS account needed unless you choose ECR.
  • A GitHub account (GitHub Container Registry is free for public repositories).
  • Comfortable with: running a Flask or Express app locally, environment variables, basic SQL.

Suggested milestones

  • Day 1: small app with /health and two endpoints, config from environment variables, works locally.
  • Day 2: multi-stage Dockerfile, non-root user, HEALTHCHECK, .dockerignore; record both image sizes.
  • Day 3: compose.yaml with PostgreSQL, named volume, health checks and .env.example.
  • Day 4 to 5: GitHub Actions build, Trivy scan, SHA-tagged push on main; capture a failing scan.
  • Day 6 to 7: evidence, README, clean-machine test, then open change-request.md.

Milestones are a pacing guide, not extra requirements. Record the date you reach each one; it makes a better story for your README and your interviews.

Current state

  • A Flask app with a requirements.txt that is incomplete.
  • Configuration, including the database password, hard-coded in source code.
  • A local PostgreSQL install on one laptop with no setup script.
  • No Dockerfile, no Compose file, no CI pipeline.
  • No documentation on how to run the app.

Target architecture

Containerize a Legacy App architecture

Developers run the app and database together with Docker Compose. Configuration comes from environment variables. On every push, GitHub Actions builds the image, scans it with Trivy, and pushes it to a registry only if no critical vulnerabilities are found. Each image is tagged with the commit SHA it was built from.

Requirements

Dockerfile

  • Uses a multi-stage build: dependencies are installed or built in one stage, and only what is needed at runtime is copied into the final stage.
  • Uses a small base image (for example a slim or alpine variant, or a distroless image). Document your choice and the final image size.
  • Pins the base image to a specific version tag, not latest.
  • Runs the app as a non-root user.
  • Runs the app with a production server (for example Gunicorn for Flask), not the Flask development server.
  • Defines a HEALTHCHECK that calls a health endpoint on the app.
  • Contains no secrets in ENV, ARG, copied files or any image layer.

Build context

  • A .dockerignore file excludes at least: .git, .env files, virtual environments, caches, test output and local editor files.

Application configuration

  • All configuration (database host, port, name, user, password, log level) is read from environment variables.
  • Hard-coded credentials are removed from the source code.
  • The app exposes a /health endpoint that returns 200 when the app is running and can reach the database.

Docker Compose

  • A compose.yaml file runs the app and PostgreSQL together.
  • PostgreSQL uses a pinned official image version and stores data in a named volume.
  • PostgreSQL has a health check, and the app only starts after the database is healthy.
  • Values come from a .env file that is git-ignored. A .env.example with placeholder values is committed.
  • Only the app port is published to the host. The database is not exposed to the host unless you document why.
  • A new developer can run the app with docker compose up after copying .env.example to .env.

CI/CD

  • A GitHub Actions workflow runs on pull requests and on pushes to main.
  • The workflow builds the image and scans it with Trivy.
  • The workflow fails if Trivy finds any CRITICAL vulnerability.
  • On main only, a passing image is pushed to a registry and tagged with the full git commit SHA.
  • Registry choice:
    • GitHub Container Registry: authenticate with the built-in GITHUB_TOKEN and grant only the packages: write permission the job needs.
    • Amazon ECR (optional): authenticate with GitHub OIDC and an IAM role limited to pushing to one repository. No AWS access keys in GitHub secrets. Enable scan on push and image tag immutability.
  • Third-party actions are pinned to a full commit SHA.

Acceptance criteria

  • docker compose up on a clean machine starts both containers, and the app responds on its published port.
  • docker compose ps shows both containers as healthy.
  • docker run --rm <image> id (or equivalent) shows a non-root user.
  • docker history --no-trunc <image> and docker inspect <image> show no passwords or secrets.
  • The final image size is documented and is smaller than a single-stage build of the same app (show both sizes).
  • Data in PostgreSQL survives docker compose down followed by docker compose up.
  • Stopping the database makes /health return a non-200 response.
  • A pull request that introduces a base image or dependency with a known critical vulnerability fails the Trivy step.
  • A merge to main pushes an image tagged with the commit SHA, visible in the registry.
  • The repository contains no .env file and no credentials.

Constraints

  • Do not commit secrets. Use .env locally and repository or environment secrets in GitHub Actions only where truly needed.
  • Do not run the container as root.
  • Do not push images tagged only as latest.
  • If you use AWS, no long-lived access keys. Use GitHub OIDC.
  • Do not disable or skip the Trivy scan to get a green pipeline. Fix the finding or document an accepted risk in a .trivyignore file with a reason.

Out of scope

  • Deploying the container to Amazon ECS, Amazon EKS or any server.
  • Kubernetes manifests or Helm charts.
  • A managed database such as Amazon RDS.
  • Database migration tooling beyond a simple init script.
  • Load testing and performance tuning.

Mid-sprint change request

In a real job, requirements change while you are building. This project has one change request. It is stored in change-request.md in this folder.

Do not read it until you have finished the main requirements (the last milestone above says when). Opening it early removes the point of the exercise, which is adapting a finished design to a new requirement.

Deliverables

  1. A GitHub repository with the app code, Dockerfile, .dockerignore, compose.yaml, .env.example and the GitHub Actions workflow.
  2. An architecture diagram that matches what you built.
  3. A project README with prerequisites, how to run locally, how configuration works, and how the pipeline works.
  4. Evidence for every acceptance criterion in an evidence/ folder, including a failed pipeline run caused by a critical vulnerability.
  5. A short demo video (3 to 5 minutes) showing a fresh clone running with one command and a pipeline run pushing a SHA-tagged image.

Definition of done

  • Pull request reviewed by at least one peer or mentor, or a completed self-review committed to the repository.
  • All acceptance criteria checked and evidenced.
  • Change request implemented and evidenced.
  • A second person (or a clean virtual machine) has run the app from the README alone.
  • Any cloud resources (for example an ECR repository) removed or kept with a lifecycle policy, and this is documented.

Cost and cleanup

  • Running Docker and Docker Compose locally has no cloud cost.
  • GitHub Actions usage on GitHub-hosted runners is free for public repositories. Private repositories use your account's included minutes.
  • Amazon ECR (if used) is billed for image storage per GB-month and for data transfer out. Add a lifecycle policy that expires old untagged images, and delete the repository with terraform destroy or the AWS CLI when you finish.
  • Locally, run docker compose down -v to remove containers and the database volume when you are done, and prune unused images.

Where to read

Official documentation only. If a tool behaves differently from what this brief says, the documentation wins; open an issue so the brief can be corrected.

Resume bullet

Containerized a laptop-only Flask and PostgreSQL service with a multi-stage, non-root Docker image and Docker Compose, cutting local setup from 2 days to under 10 minutes and reducing image size by 60%; added a GitHub Actions pipeline with Trivy scanning that blocks critical vulnerabilities and publishes SHA-tagged images.

Replace the numbers with your own real results.

Interview questions to prepare

  1. What does a multi-stage build give you, and what did you leave out of the final stage?
  2. Why should a container not run as root, and how did you set that up?
  3. How do you keep secrets out of a container image, and how would you check an image for leaked secrets?
  4. Why tag images with the git commit SHA instead of latest?
  5. What does the Compose health check and depends_on condition solve, and what does it not solve?
  6. Your Trivy scan fails on a critical vulnerability in the base image. What are your options?

Part of Real DevOps Projects. Build it, push it to your own GitHub, and add it to your resume. Use the evidence template, the demo video guide and the self-review checklist.