Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between docker service and docker container

I can create a docker container by command

docker run <<image_name>>

I can create a service by command

docker service create <<image_name>>

What is the difference between these two in behaviour?

When would I need to create a service over container?

like image 765
Shiva Avatar asked Mar 05 '23 19:03

Shiva


1 Answers

docker service command in a docker swarm replaces the docker run. docker run has been built for single host solutions. Its whole idea is to focus on local containers on the system it is talking to. Whereas in a cluster the individual containers are irrelevant. We simply use swarm services to manage the multiple containers in a cluster. Swarm will orchestrate the containers of the services for us.

docker service create is mainly to be used in docker swarm mode. docker run does not have the concept of scaling up/down. With docker service create you can specify the number of replicas to be created using the --replicas command. This will create and manage multiple replicas of a containers in many different nodes. There are several such options for managing multiple containers using docker service create and other commands under docker service ...

One more note: docker services are for container orchestration systems(swarm). It has built in facility for failure recovery. ie. it recreates a container on failure. docker runwould never recreate a container if it fails. When the docker service commands are used we are not directly asking to perform action like "create a single container", rather we are saying to the orchestration system to "put this job in your queue and when you can get to it perform that action on the swarm". This means it has rollback facilities, failure mitigation and lots of intelligence built in.

You need to consider using docker service create when in swarm mode and docker run when not in swarm mode. You can lookup on docker swarms to understand docker services.

like image 126
Riyafa Abdul Hameed Avatar answered Mar 31 '23 12:03

Riyafa Abdul Hameed