TechLead
Lesson 18 of 18
5 min read
Docker & DevOps

DevOps Best Practices & Workflows

Learn essential DevOps practices including Infrastructure as Code, GitOps, and team collaboration workflows

What is DevOps?

DevOps is a set of practices that combines software development (Dev) and IT operations (Ops) to shorten the development lifecycle and deliver high-quality software continuously. Docker is a cornerstone of modern DevOps workflows.

DevOps Principles

  • Automation: Automate everything — builds, tests, deployments, infrastructure
  • Continuous Integration: Merge and test code frequently
  • Continuous Delivery: Always be ready to deploy to production
  • Infrastructure as Code: Define infrastructure in version-controlled files
  • Monitoring: Observe everything and respond to issues proactively
  • Collaboration: Break down silos between dev and ops teams

Infrastructure as Code (IaC)

Define your infrastructure using code files that can be version controlled, reviewed, and automated — instead of manually configuring servers.

Docker Compose as IaC

# Your entire infrastructure described in code
services:
  app:
    build: .
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure
      resources:
        limits:
          memory: 512M

  db:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine

  nginx:
    image: nginx:alpine
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf

volumes:
  pgdata:

GitOps Workflow

GitOps uses Git as the single source of truth for both application code and infrastructure. Changes are made through pull requests, reviewed, and automatically applied.

GitOps Flow

  1. Developer creates a feature branch and pushes code
  2. CI pipeline builds Docker image and runs tests
  3. Developer opens a pull request
  4. Team reviews code and approves
  5. Merge to main triggers CD pipeline
  6. New image is built, tagged, and pushed to registry
  7. Deployment is automatically updated with the new image

Branching Strategy

# Feature branches
git checkout -b feature/add-user-auth

# Make changes, commit, push
git add .
git commit -m "feat: add user authentication"
git push origin feature/add-user-auth

# Create PR → Review → Merge

# CI/CD automatically:
# 1. Builds Docker image
# 2. Runs tests
# 3. Pushes to registry
# 4. Deploys to staging (on PR merge to develop)
# 5. Deploys to production (on release tag)

Environment Promotion

# Same image, different configs per environment
# .github/workflows/deploy.yml
jobs:
  deploy-staging:
    if: github.ref == 'refs/heads/develop'
    steps:
      - name: Deploy to Staging
        run: |
          docker compose -f compose.yml -f compose.staging.yml up -d

  deploy-production:
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: Deploy to Production
        run: |
          docker compose -f compose.yml -f compose.prod.yml up -d

Docker Development Workflow

# 1. Clone the project
git clone https://github.com/team/project.git
cd project

# 2. Start development environment (everything in one command!)
docker compose up -d

# 3. Make changes (live reload with bind mounts)
# Edit files in your IDE...

# 4. Run tests
docker compose exec api npm test

# 5. Check logs
docker compose logs -f api

# 6. Tear down
docker compose down

Makefile for Common Tasks

# Makefile — simplify Docker commands
.PHONY: dev build test deploy clean

dev:
	docker compose up -d
	docker compose logs -f

build:
	docker compose build --no-cache

test:
	docker compose run --rm api npm test

lint:
	docker compose run --rm api npm run lint

deploy:
	docker compose -f compose.yml -f compose.prod.yml up -d

clean:
	docker compose down -v
	docker system prune -f

logs:
	docker compose logs -f

shell:
	docker compose exec api sh

db-shell:
	docker compose exec db psql -U admin myapp

backup:
	docker compose exec db pg_dump -U admin myapp > backup-$$(date +%Y%m%d).sql

DevOps Checklist

  • ✅ All infrastructure defined in code (Docker Compose, K8s manifests)
  • ✅ CI/CD pipeline automates build, test, and deploy
  • ✅ Feature branches with PR reviews before merging
  • ✅ Separate environments: dev → staging → production
  • ✅ Same Docker image promoted across environments
  • ✅ Monitoring and alerting for production services
  • ✅ One-command setup for new developers

Continue Learning