This project containerizes a microservices-based application locally using Docker Compose, and in production using Docker Swarm, with CI/CD automation via GitHub Actions.
| Concept | Description |
|---|---|
| Docker Image | A lightweight, standalone package of software that includes code & runtime |
| Docker Container | A running instance of an image |
| Dockerfile | Script to build Docker images |
| Docker Volume | Persistent data storage |
| Docker Compose | Tool to define and run multi-container applications |
| Docker Swarm | Native container orchestration for Docker |
| Docker Hub | Cloud-based registry to store and share images |
# Version and Info
docker --version # Check Docker version
# Image Management
docker build -t image_name . # Build image from Dockerfile
docker images # List all images
docker rmi image_name # Remove image
# Container Management
docker run -d -p 8080:80 image_name # Run container in detached mode
docker ps # List running containers
docker ps -a # List all containers
# Logs and Exec
docker logs <container_id> # View container logs
docker exec -it <container_id> bash # Enter a running container shell
# Stop & Remove
docker stop <container_id> # Stop container
docker rm <container_id> # Remove container
# Volume Usage
-v host_path:container_path # Mount volume to persist data
# Docker Compose
docker-compose up -d # Start services in detached mode
docker-compose down # Stop and remove all services
# Docker Swarm
docker swarm init # Initialize swarm
# Networks
docker network ls # List networks
docker network create mynet # Create a user-defined network| Tool/Concept | Real World Usage |
|---|---|
| Dockerfile | Ensures consistent builds across dev, test, and prod |
| Compose | Runs full local stack with all microservices |
| Volumes | Persist PostgreSQL data between container restarts |
| Docker Hub | Share and pull prebuilt images across environments |
| Swarm | Simulate production deployment locally with replicas |
- Frontend: Node.js β UI Service
- Backend: Node.js β API Service
- Auth Service: Python (FastAPI or Django) β Authentication
- Database: PostgreSQL or MySQL
FROM node:18 as builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
FROM node:18-slim
WORKDIR /app
COPY package*.json ./
RUN npm install --omit=dev
COPY --from=builder /app .
ENV PORT=8080
EXPOSE 8080
CMD ["node", "index.js"]docker build -t backend ./backendUse Docker Compose for local development and to define the full stack.
version: '3.7'
services:
backend:
build: ./backend
ports:
- "8080:8080"
networks:
- app-network
environment:
- DB_HOST=db
- DB_USER=root
- DB_PASSWORD=rootpass
- DB_NAME=docker_demo
depends_on:
- db
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "5"
networks:
app-network:
driver: bridge
volumes:
dbdata:docker-compose up -d
docker-compose ps
docker-compose logs -fSimulates distributed orchestration on local or remote environments.
docker swarm initdocker stack deploy -c docker-compose.yml mystack
docker stack services mystack
docker stack ps mystackAutomates build, test, and deployment when code is pushed.
name: Backend Service CI
on:
push:
paths:
- 'backend/**'
- '.github/workflows/backend.yml'
pull_request:
paths:
- 'backend/**'
- '.github/workflows/backend.yml'
workflow_dispatch:
jobs:
backend:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up Docker
run: |
# Uninstall conflicting containerd packages
sudo apt-get remove -y containerd containerd.io
# Update package list
sudo apt-get update
# Install Docker dependencies
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Set up the stable Docker repository
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Engine and Docker Compose
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Verify installation
docker --version
docker-compose --version
- name: Build and start backend service with Docker Compose
run: |
docker-compose -f docker-compose.yml up -d --build backend
- name: Wait for backend to be ready
run: |
echo "Waiting for backend service to be ready..."
sleep 30 # Adjust the sleep time based on expected startup time
- name: Check backend service health
run: |
echo "Testing backend service at http://localhost:8080"
curl -f http://localhost:8080/health || echo "Backend service failed health check!"
- name: Get backend service logs
run: |
echo "Fetching backend service logs"
docker-compose logs backend
- name: Get MySQL DB logs
run: |
echo "Fetching MySQL DB logs"
docker-compose logs db
- name: Check if MySQL service is accessible
run: |
echo "Checking if MySQL DB is accessible"
docker-compose exec db mysqladmin ping -h localhost || echo "MySQL DB is not accessible!"
- name: Run backend tests
run: |
echo "Running backend tests inside backend container"
docker-compose exec -T backend npm test # Ensure tests run inside the backend container
- name: Shut down the Docker containers
run: |
docker-compose downSensitive data like DB passwords or Docker Hub credentials should not be hardcoded.
-
Go to Settings > Secrets and variables > Actions
-
Add secrets like:
DOCKER_USERNAMEDOCKER_PASSWORD
docker ps
curl http://localhost:3000
curl http://localhost:5000
curl http://localhost:8000| Issue | Cause | Solution |
|---|---|---|
| Docker image build fails | Bad Dockerfile or missing files | Check paths, logs, and Dockerignore |
| Service not accessible | Port not exposed properly | Review ports: section in Compose |
| Swarm deployment not working | Stack config error | Validate with docker-compose config |
| GitHub Action fails | Wrong Docker credentials | Recheck repository secrets |
| Container crashing on start | Wrong CMD or missing dependency | Check logs and Dockerfile |
# Docker Builds
cd frontend && docker build -t frontend .
cd backend && docker build -t backend .
cd auth && docker build -t auth .
# Docker Compose (local)
docker-compose up -d
docker-compose logs -f
# Docker Swarm
docker swarm init
docker stack deploy -c docker-compose.yml mystack
docker stack ps mystack
# Docker Image Push (Manual or CI/CD)
docker login -u <user>
docker push <image>
# GitHub Actions (CI/CD)
# - On push to main, the pipeline builds and pushes all images to Docker Hub
# - Docker credentials are securely managed in GitHub Secrets