Docker Compose Advanced | Multi-Environment, Health Checks, Secrets | Sinhala Guide

Docker Compose Advanced | Multi-Environment, Health Checks, Secrets | Sinhala Guide

ආයුබෝවන් යාළුවෝ හැමෝටම!

අද අපි කතා කරන්න යන්නේ Docker Compose වල ඊළඟ මට්ටමට, ඒ කියන්නේ advanced features ගැන. ඔයා දැනටමත් Docker සහ Docker Compose වල basics ටිකක් දන්නවා නම්, මේ article එක ඔයාට ඔයාගේ applications තවත් robust, secure, සහ production-ready කරගන්න ගොඩක් උදව් වෙයි. අපි බලමු, කොහොමද Docker Compose එකෙන් multi-environment configurations, health checks, secrets management, dependency management, production deployments වගේ සංකීර්ණ දේවල් පහසුවෙන් handle කරන්නේ කියලා.

ඉතින්, සූදානම්ද? එහෙනම් අපි පටන් ගමු!

1. Multi-Environment Configurations (වෙනස් පරිසරයන් සඳහා සැකසීම්)

අපි software develop කරද්දී, සාමාන්‍යයෙන් Development, Staging, Production වගේ වෙනස් environments ගොඩක් තියෙනවා. මේ හැම environment එකකටම අපේ application එකට වෙනස් configuration (සැකසීම්) අවශ්‍ය වෙනවා. උදාහරණයක් විදියට, Development environment එකට debug logs අවශ්‍ය වෙන්න පුළුවන්, Production environment එකට data volumes, resource limits, සහ replica count එක වැඩි වෙන්න පුළුවන්. Docker Compose මේ සඳහා අපිට docker-compose.override.yml වගේ files භාවිතා කරන්න අවස්ථාව දෙනවා.

Override Files භාවිතා කරන ආකාරය

මේ ක්‍රමයේදී, ඔයා මුලින්ම ඔයාගේ application එකේ base configuration එක docker-compose.yml file එකේ ලියනවා. ඊට පස්සේ, environment එක අනුව වෙනස් වෙන configurations, docker-compose.dev.yml, docker-compose.prod.yml වගේ වෙනත් files වල ලියනවා. Docker Compose ඒවා merge කරලා final configuration එක හදාගන්නවා.

උදාහරණයක් බලමු:

docker-compose.yml (Base Configuration)

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    environment:
      APP_ENV: development # Default to development
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf

docker-compose.prod.yml (Production Overrides)

version: '3.8'
services:
  web:
    environment:
      APP_ENV: production # Override for production
    deploy:
      replicas: 3 # Scale to 3 instances in production
      restart_policy:
        condition: on-failure # Restart if service fails
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"

Production environment එකට deploy කරන්න, මේ විදියට execute කරන්න පුළුවන්:

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

-f flag එක භාවිතා කරලා ඔයාට අවශ්‍ය files specify කරන්න පුළුවන්. මේක ඇත්තටම අපේ configurations manage කරගන්න පට්ටම පහසු ක්‍රමයක්.

2. Health Checks (සෞඛ්‍ය තත්ත්වය පරීක්ෂා කිරීම)

ඔයාගේ Docker service එක running කියලා පෙන්නුවට, ඒක ඇත්තටම ready වෙලා නැති වෙන්න පුළුවන්. උදාහරණයක් විදියට, database service එක start වෙලා තිබුණට, ඒකේ ports open වෙලා තිබුණට, database එක ඇතුළේ tables populate වෙලා නැති වෙන්න පුළුවන්, නැත්නම් web server එක start වෙලා තිබුණට, application server එක තාම requests handle කරන්න ready නැති වෙන්න පුළුවන්.

මේ වගේ අවස්ථාවලට healthcheck කියන option එක ගොඩක් වැදගත් වෙනවා. මේකෙන් Docker engine එකට පුළුවන් service එකක සෞඛ්‍ය තත්ත්වය නිවැරදිව තේරුම් ගන්න.

උදාහරණයක්: Node.js web application එකක් සඳහා health check එකක්

version: '3.8'
services:
  webapp:
    image: my-nodejs-app:latest
    ports:
      - "3000:3000"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"] # Check /health endpoint
      interval: 30s # Check every 30 seconds
      timeout: 10s # Fail if check takes longer than 10 seconds
      retries: 3 # Retry 3 times before marking as unhealthy
      start_period: 20s # Give the app 20 seconds to start up before starting checks

මෙහිදී webapp service එකේ /health endpoint එකට curl command එක යවලා check කරනවා. මේකෙන් ඔයාගේ application එක ඇත්තටම requests handle කරන්න ready ද කියලා Docker ට තේරුම් ගන්න පුළුවන්. docker ps වලින් බැලුවම service එකේ status එක (healthy) කියලා පෙන්නයි.

3. Secrets Management (රහස්‍ය තොරතුරු කළමනාකරණය)

ඔයාගේ application එකේ passwords, API keys, database credentials වගේ sensitive data environment variables විදියට දාන එක security wise එච්චර හොඳ නෑ. Docker Compose, Docker Swarm සහ Kubernetes වල වගේම secrets manage කරන්න පුළුවන්කම දෙනවා. මේකෙන් sensitive data, files විදියට secure විදියට services වලට mount කරන්න පුළුවන්.

උදාහරණයක්: Database password එකක් secret විදියට භාවිතා කිරීම

  1. මුලින්ම ඔයාගේ secret එක file එකක දාගන්න. (උදා: db_password.txt)
echo "mysecretpassword" > db_password.txt
  1. ඊට පස්සේ, docker-compose.yml file එකේ secrets block එක define කරලා, service එකට attach කරන්න.
version: '3.8'
services:
  db:
    image: postgres:13
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password # PostgreSQL reads password from this file
    secrets:
      - db_password # Attach the secret to the service
    volumes:
      - db_data:/var/lib/postgresql/data

secrets:
  db_password:
    file: ./db_password.txt # Path to your secret file

volumes:
  db_data:

මේ විදියට db_password කියන secret එක db service එකට /run/secrets/db_password path එකට mount වෙනවා. PostgreSQL වගේ databases වලට direct secrets files වලින් passwords කියවන්න පුළුවන්. මේක environment variables වලට වඩා ගොඩක් secure.

4. Dependency Management (සේවා අතර රඳා පැවැත්ම කළමනාකරණය)

අපේ applications වලදී එක් service එකක් තවත් service එකක් මත රඳා පවතිනවා. උදාහරණයක් විදියට, web application එකක් database එකක් නැතුව වැඩ කරන්නේ නෑ. Docker Compose වල depends_on කියන option එකෙන් service start order එක manage කරන්න පුළුවන්.

depends_on - Start Order විතරයි!

ගොඩක් අය හිතනවා depends_on කියන්නේ service එක fully ready වෙනකන් බලන් ඉන්නවා කියලා. ඒත් ඇත්තටම depends_on වලින් වෙන්නේ, depend වෙන service එක start වෙනකන් බලාගෙන ඉඳලා, ඒක start උනාම තමන්ගේ service එකත් start කරන එක විතරයි. ඒ කියන්නේ, database container එක start උනාට, database server එක ඇතුලෙන් requests handle කරන්න ready වෙන්න පොඩි වෙලාවක් යනවා.

උදාහරණයක්:

version: '3.8'
services:
  webapp:
    image: my-app:latest
    depends_on:
      db:
        condition: service_started # This just means the DB container has started
  db:
    image: postgres:13

මේ අවස්ථාවේදී webapp එක start වෙන්නේ db container එක started කියන status එකට ආවමයි. ඒත් db එක තාම queries accept කරන්න ready නැති වෙන්න පුළුවන්. මේකට හොඳම විසඳුම තමයි application code එක ඇතුලෙන් හෝ entrypoint script එකක් හරහා dependency readiness check කරන එක.

Dependency Readiness Checks (සැබෑ රඳා පැවැත්ම පරීක්ෂා කිරීම)

Production environments වලදී, wait-for-it.sh වගේ scripts නැත්නම් Dockerize වගේ tools භාවිතා කරලා, service එක ඇත්තටම ready වෙනකන් බලාගෙන ඉන්න එක තමයි හොඳම ක්‍රමය. මේවා service එකේ port එකට connect වෙලා බලනවා නැත්නම් health endpoint එක check කරනවා.

wait-for-it.sh උදාහරණය (webapp service entrypoint එකේ):

#!/bin/sh
# wait-for-it.sh script එක container එක ඇතුලට copy කරලා තියෙනවා කියමු.

/app/wait-for-it.sh db:5432 --timeout=30 -- echo "Database is up - starting webapp"
exec npm start

මේ wait-for-it.sh script එක db service එකේ 5432 port එක ready වෙනකන් තත්පර 30ක් බලන් ඉන්නවා. Database එක ready උනාම විතරයි npm start command එක execute වෙන්නේ. මේක තමයි Production level deployments වලට අනිවාර්යයෙන්ම කරන්න ඕන දෙයක්.

5. Production Deployments (නිෂ්පාදන පරිසරයට යෙදවීම)

Development environment එකේ වැඩ කරනවා වගේම, Production environment එකට Docker Compose භාවිතා කරද්දී පොඩ්ඩක් මේ ගැන අවධානය යොමු කළොත් ගොඩක් දේවල් පහසු කරගන්න පුළුවන්.

ප්‍රධාන කරුණු:

  • Detached Mode (-d): Production වලදී containers run කරද්දී අනිවාර්යයෙන්ම detached mode එකේ (background එකේ) run කරන්න.
  • Scaling Services (සේවා පරිමාණය කිරීම): Docker Compose එකෙන් basic scaling කරන්න පුළුවන්.
  • Note: Complex scaling, rolling updates, load balancing වගේ දේවල් වලට Docker Swarm නැත්නම් Kubernetes වගේ orchestration tools වලට යන එක තමයි හොඳම විසඳුම.
  • Resource Limits: Services වලට CPU සහ Memory limits දාන එක ගොඩක් වැදගත්. මේකෙන් එක් service එකක් මුළු host machine එකේම resources consume කරන එක වළක්වනවා.
  • Restart Policy: Service එකක් fail උනොත් නැත්නම් host machine එක restart උනොත්, container එක automatically restart වෙන්න restart policy එකක් set කරන්න.
  web:
    image: my-app:latest
    restart: always # Always restart the container if it stops
  web:
    image: my-app:latest
    deploy:
      resources:
        limits:
          cpus: '0.5' # Max 50% of a CPU core
          memory: 512M # Max 512MB RAM
docker-compose up -d --scale web=3 db=1 # web services 3ක්, db service 1ක්
docker-compose up -d

6. Common Compose Errors and Troubleshooting (සාමාන්‍ය දෝෂ සහ ඒවා නිරාකරණය කිරීම)

අපි කොච්චර පරිස්සමෙන් වැඩ කලත්, error එකක් එන එක සාමාන්‍ය දෙයක්. Docker Compose එක්ක වැඩ කරද්දී එන පොදු errors සහ ඒවා troubleshoot කරන විදිය ගැන පොඩ්ඩක් දැනගෙන ඉන්න එක ගොඩක් වටිනවා.

Image Pull Errors (Image pull කිරීමේ දෝෂ):Docker Hub එකෙන් හෝ private registry එකකින් image එකක් pull කරන්න බැරි වෙන එක.

# විසඳුම:
# Image name එක නිවැරදිද කියලා බලන්න (e.g., myuser/myimage:latest).
# Private registry එකක් නම්, Docker login වෙලාද කියලා බලන්න.
# Internet connection එක check කරන්න.

Volume Mount Issues (Volume mount ගැටළු):Local files හෝ directories containers ඇතුලට mount කරද්දී permissions errors නැත්නම් path errors එන්න පුළුවන්.

# විසඳුම:
# Host machine එකේ file/directory permissions check කරන්න.
# docker-compose.yml එකේ volume path එක නිවැරදිද කියලා බලන්න. 
# උදා: - ./data:/var/lib/mysql

Incorrect Network Configurations (වැරදි ජාල සැකසීම්):Services එකිනෙකාට connect වෙන්න බැරි අවස්ථා වලට මේක හේතුවක් වෙන්න පුළුවන්. Docker Compose default network එකක් හදනවා, ඒක ඇතුලේ services වලට service names වලින් communicate වෙන්න පුළුවන්.

# විසඳුම:
# ඔයාගේ docker-compose.yml එකේ network definitions check කරන්න.
# Services ටික එකම network එකකද තියෙන්නේ කියලා බලන්න.
# Services connect වෙන්න හදන hostname එක (service name) නිවැරදිද කියලා බලන්න.

"Service xxx exited with code 1" (සේවාවක් code 1කින් අවසන් වීම):මේකෙන් කියවෙන්නේ ඔයාගේ service එක start වෙලා, ඒත් මොකක් හරි හේතුවක් නිසා close උනා කියන එකයි. Code 1 කියන්නේ සාමාන්‍යයෙන් application-level error එකක්.

# විසඳුම:
# Service එකේ logs බලන්න. මේක තමයි පළවෙනි පියවර.
docker-compose logs <service_name>

# උදා: docker-compose logs webapp
# Log එකේ මොකක් හරි error message එකක් තියෙන්න ඕන. 

"Port already in use" (Port එකක් දැනටමත් භාවිතයේ තිබීම):මේක ගොඩක් වෙලාවට එන error එකක්. ඔයාගේ host machine එකේ Docker container එකක් හෝ වෙනත් process එකක් දැනටමත් ඔයා map කරන්න හදන port එක භාවිතා කරනවා කියන එක මේකෙන් කියවෙනවා.

# විසඳුම:
# දැනටමත් run වෙන process මොනවද කියලා බලන්න
sudo lsof -i :8000 # 8000 port එක example එකක් විදියට

# නැත්නම්, ඔයාගේ docker-compose.yml එකේ port mapping එක වෙනස් කරන්න
# උදා: "8080:80" වෙනුවට "9000:80" 

ඕනෑම error එකකදී docker-compose logs <service_name> සහ docker-compose ps භාවිතා කරන එක තමයි මුලින්ම කරන්න ඕන දේ. ගොඩක් වෙලාවට logs වලින්ම ප්‍රශ්නය තේරුම් ගන්න පුළුවන්.

අවසන් වශයෙන් (Conclusion)

ඉතින් යාළුවනේ, මේ Docker Compose Advanced Techniques Sinhala Guide එකෙන් ඔයාලට Docker Compose වල basics වලින් එහා ගියපු ගොඩක් වැදගත් දේවල් ගැන ඉගෙන ගන්න ලැබුණා කියලා මම හිතනවා. Multi-environment configurations වලින් ඔයාගේ deployments flexible කරගන්නත්, health checks වලින් services වල stability එක වැඩි කරගන්නත්, secrets management වලින් sensitive data ආරක්ෂා කරගන්නත්, dependency management වලින් services start up sequence එක නිවැරදිව manage කරගන්නත්, production deployments වලදී අවධානය යොමු කළ යුතු කරුණු ගැනත් අපි කතා කළා.

මේ වගේ advanced features නිවැරදිව භාවිතා කිරීමෙන්, ඔයාට පුළුවන් robust, scalable, සහ secure applications හදන්න. මේක ඔයාගේ DevOps journey එකේදී අනිවාර්යයෙන්ම දැනගෙන ඉන්න ඕන skills set එකක්.

දැන් මේ දේවල් ඔයාගේ next project එකට apply කරලා බලන්න. ඔයාගේ අත්දැකීම් මොනවාද? ඔයාට මේ ගැන තවත් දැනගන්න අවශ්‍ය දේවල් තියෙනවා නම්, comment section එකේ share කරන්න! අපි හැමෝම එකතු වෙලා ඉගෙන ගමු.

සුබ දවසක්!