Project 01 - Node.js REST API + MySQL

A production-style REST API built with Node.js + Express backed by a MySQL database, fully containerized with Docker Compose.

What You Will Learn

  • Writing a multi-stage Dockerfile for Node.js
  • Running Node.js and MySQL together with Docker Compose
  • Using named volumes to persist database data
  • Connecting containers using a custom network (by name, not IP)
  • Health checks to ensure MySQL is ready before the API starts
  • Passing secrets via environment variables

Architecture

graph LR Browser[" Browser / Postman"] -->|port 3000| API subgraph DOCKER["Docker Network: app-network"] API[" api\nNode.js + Express\nport 3000"] DB[(" mysql\nMySQL 8.0\nport 3306")] API -->|"DB_HOST=mysql"| DB end DB --- VOL[(" mysql-data\npersistent volume")]

Project Structure

01. Node.js API + MySQL/
 app/
    index.js          <- Express app
    package.json
    package-lock.json
 docker-compose.yml
 Dockerfile
 .env.example
 README.md

How to Run

# 1. Clone / navigate to project folder
cd "Docker Projects/01. Node.js API + MySQL"

# 2. Copy environment file
copy .env.example .env

# 3. Start all services
docker compose up -d

# 4. Check services are running
docker compose ps

# 5. Test the API
curl http://localhost:3000/health
curl http://localhost:3000/api/users

# 6. View logs
docker compose logs -f api

# 7. Stop everything
docker compose down

# 8. Stop and remove database data
docker compose down -v

API Endpoints

MethodEndpointDescription
GET/healthHealth check - returns API + DB status
GET/api/usersGet all users
POST/api/usersCreate a new user
GET/api/users/:idGet user by ID
DELETE/api/users/:idDelete a user

Key Concepts Demonstrated

ConceptWhere
Multi-stage DockerfileDockerfile
Named volume for DBdocker-compose.yml -> volumes
Custom networkdocker-compose.yml -> networks
Health check on MySQLdocker-compose.yml -> healthcheck
depends_on with conditiondocker-compose.yml -> depends_on
Env vars from .env file.env.example + docker-compose.yml

FAQ

Should I memorize every Docker command?+

No. Memorize the core workflow first: build, run, list, inspect, logs, exec, stop, remove, and clean up. Then learn specialized commands when you need them.

Is Docker only for developers?+

No. Docker is useful for system administrators, infrastructure engineers, DevOps engineers, cloud engineers, support engineers, and learners who want repeatable labs.

What should I do after reading this guide?+

Run the examples, write down what each command changes, rebuild the workflow with Docker Compose, and then add one CI/CD step that builds the image automatically.

Need help applying Docker in a real project?

Work directly with Muhammad Irfan Aslam for Docker, Linux, DevOps, CI/CD, cloud deployment, or infrastructure troubleshooting support.

Hire Me for Support