Home > Net >  Why containers are called services in docker compose?
Why containers are called services in docker compose?

Time:10-22

I have this docker-compose.yml file. It is creating containers but it is called service in docker compose. What is the difference between container and service and what are the benefits of making it service?

version: "3.9"
services:
  web:
    build: .
    ports:
      - "1026:5000"
    depends_on: 
      - redis
    volumes:
      - vol1:/code
  redis:
    image: "redis:alpine"
    container_name: db
volumes:
  vol1:

CodePudding user response:

A service is a specification for running containers. A service persists while, at any point in time, there might be 0, 1, or many containers that are service tasks, or replicas.

With compose, you can scale the number of replicas of a service using the scale keyword. there are now many containers, but still one service to manage them. If containers shut down, the service restart policy can have rules about restarting the service. This will cause new containers to be created. Each stopped container can be examined for its own crash - or success - logs. But again, the service is the single contact point that can retrieve the list of containers, their status and so on.

  • Related