0

In docker-compose, we can define service attribute image, or we can build an image from a dockerfile. It seems that service is the same with container which is also a running instance of the based image.

So what is the difference between service and container?

ZhaoGang
  • 4,491
  • 1
  • 27
  • 39

2 Answers2

1

Conceptually a service and a container are completely different things.

  • A container is a standardised wrapper around an isolated process.
  • A service is a mechanism to provide access to capabilities (running software) via a formal interface.

Further, in docker-compose, a service can have 0..n instances, where n defaults to 1 and can be overridden. Access over HTTP will be load-balanced by compose and services will be registered in a registry that allows other services to look them up by name for access (dns).

There are other differences, such as the default placement of services on a shared network. And, another difference is that compose is declarative where 'docker run' is imperative. There is a strong preference in the industry for the former.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
  • IMHO, Compose is used in a single host, compared with Swarm which are is to deploy our application in a cluster. Can we have more than 1 service instance in a single host? – ZhaoGang Feb 23 '21 at 07:31
0

Your assumption seems correct. Docker states that they follow the compose-spec for Cloud Native Applications. According to the compose-spec:

Services are defined by a Docker image and set of runtime arguments. All containers within a service are identically created with these arguments.

Functionally, they should be the same. docker-compose files are an easier way to define how a container should be run (compared to providing long commands to the docker CLI). Services are a concept used by docker-compose to abstract some computing resource that can be scaled and replaced independently from other components.

All credit due the compose-spec/spec.md doc. This is the best description I've read of what they meant these to be.

cam
  • 4,409
  • 2
  • 24
  • 34