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?
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 run
would 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With