Sobes.tech
Middle

What is the difference between Docker Compose and Docker Swarm?

sobes.tech AI

Answer from AI

Docker Compose manages multiple containers as a single service on one machine. It is used for development, testing, and local deployments. Configuration files are described in YAML format.

Docker Swarm is an integrated cluster orchestrator for Docker. It allows running and managing a distributed cluster of multiple Docker machines. It is used for production deployments, providing scaling, fault tolerance, and load balancing.

Main differences:

Characteristic Docker Compose Docker Swarm
Scalability Single machine Multi-machine (cluster)
Purpose Development, testing, local Production, distributed systems
Orchestration No (manages multiple services) Yes (built-in orchestrator)
Fault tolerance No Yes (self-healing services)
Load balancing No Yes (built-in)
Service configuration docker-compose.yml docker stack deploy with YAML file

Example docker-compose.yml for two services (web and db):

# docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: mysecretpassword

Example of deploying a stack in Swarm:

# Deploy services described in the YAML file
docker stack deploy -c my-swarm-stack.yaml myapp