背景:
阅读新闻

What is the difference between Docker Service and Docker Container?

  作者: 今日评论: [字体: ]

In short: Docker service is used mostly when you configured the master node with Docker swarm so that docker containers will run in a distributed environment and it can be easily managed.

Docker run: The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.

That is, docker run is equivalent to the API /containers/create then /containers/(id)/start

source: https://docs.docker.com/engine/reference/commandline/run/#parent-command

Docker service: Docker service will be the image for a microservice within the context of some larger application. Examples of services might include an HTTP server, a database, or any other type of executable program that you wish to run in a distributed environment.

When you create a service, you specify which container image to use and which commands to execute inside running containers. You also define options for the service including:

  • the port where the swarm will make the service available outside the swarm
  • an overlay network for the service to connect to other services in the swarm
  • CPU and memory limits and reservations
  • a rolling update policy
  • the number of replicas of the image to run in the swarm

source: https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/#services-tasks-and-containers


Docker run will start a single container.

With docker service you manage a group of containers (from the same image). You can scale them (start multiple containers) or update them.


  • docker run is used to create a standalone container
  • docker service create is used to create instances (called tasks) of that service running in a cluster (called swarm) of computers (called nodes). Those tasks are containers of cource, but not standalone containers. In a sense a service acts as a template when instantiating tasks.

For example

docker service create --name MY_SERVICE_NAME --replicas 3 IMAGE:TAG

creates 3 tasks of the MY_SERVICE_NAME service, which is based on the IMAGE:TAG image.

More information can be found here



You can use docker in two way.

Standalone mode


When you are using the standalone mode you have installed docker daemon in only one machine. Here you have the ability to create/destroy/run a single container or multiple containers in that single machine.

So when you run docker run; the docker-cli creates an API query to the dockerd daemon to run the specified container.

So what you do with the docker run command only affects the single node/machine/host where you are running the command. If you add a volume or network with the container then those resources would only be available in the single node where you are running the docker run command.

Swarm mode (or cluster mode)


When you want or need to utilize the advantages of cluster computing like high availability, fault tolerance, horizontal scalability then you can use the swarm mode. With swarm mode, you can have multiple node/machine/host in your cluster and you can distribute your workload throughout the cluster. You can even initiate swarm mode in a single node cluster and you can add more node later.

Example


You can recreate the scenario for free here. Suppose at this moment we have only one node called node-01.dc.local, where we have initiated following commands,

####### Initiating swarm mode ########

$ docker swarm init --advertise-addr eth0

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-21mxdqipe5lvzyiunpbrjk1mnzaxrlksnu0scw7l5xvri4rtjn-590dyij6z342uyxthletg7fu6 192.168.0.8:2377


####### create a standalone container  #######
[node1] (local) root@192.168.0.8 ~
$ docker run -d --name app1 nginx

####### creating a service #######
[node1] (local) root@192.168.0.8 ~
$ docker service create --name app2 nginx

After a while, when you feel that you need to scale your workload you have added another machine named node-02.dc.local. And you want to scale and distribute your service to the newly created node. So we have run the following command on the node-02.dc.local node,

####### Join the second machine/node/host in the cluster #######

[node2] (local) root@192.168.0.7 ~
$ docker swarm join --token SWMTKN-1-21mxdqipe5lvzyiunpbrjk1mnzaxrlksnu0scw7l5xvri4rtjn-590dyij6z342uyxthletg7fu6 192.168.0.8:2377
This node joined a swarm as a worker.

Now from the first node I have run the followings to scale up the service.

####### Listing services #######
[node1] (local) root@192.168.0.8 ~
$ docker service ls
ID             NAME      MODE         REPLICAS   IMAGE          PORTS
syn9jo2t4jcn   app2      replicated   1/1        nginx:latest   

####### Scalling app2 from single container to 10 more container #######
[node1] (local) root@192.168.0.8 ~
$ docker service update --replicas 10 app2
app2
overall progress: 10 out of 10 tasks 
1/10: running   [==================================================>] 
2/10: running   [==================================================>] 
3/10: running   [==================================================>] 
4/10: running   [==================================================>] 
5/10: running   [==================================================>] 
6/10: running   [==================================================>] 
7/10: running   [==================================================>] 
8/10: running   [==================================================>] 
9/10: running   [==================================================>] 
10/10: running   [==================================================>] 
verify: Service converged 
[node1] (local) root@192.168.0.8 ~

####### Verifying that app2's workload is distributed to both of the ndoes #######
$ docker service ps app2
ID             NAME      IMAGE          NODE      DESIRED STATE   CURRENT STATE            ERROR     PORTS
z12bzz5sop6i   app2.1    nginx:latest   node1     Running         Running 15 minutes ago             
8a78pqxg38cb   app2.2    nginx:latest   node2     Running         Running 15 seconds ago             
rcc0l0x09li0   app2.3    nginx:latest   node2     Running         Running 15 seconds ago             
os19nddrn05m   app2.4    nginx:latest   node1     Running         Running 22 seconds ago             
d30cyg5vznhz   app2.5    nginx:latest   node1     Running         Running 22 seconds ago             
o7sb1v63pny6   app2.6    nginx:latest   node2     Running         Running 15 seconds ago             
iblxdrleaxry   app2.7    nginx:latest   node1     Running         Running 22 seconds ago             
7kg6esguyt4h   app2.8    nginx:latest   node2     Running         Running 15 seconds ago             
k2fbxhh4wwym   app2.9    nginx:latest   node1     Running         Running 22 seconds ago             
2dncdz2fypgz   app2.10   nginx:latest   node2     Running         Running 15 seconds ago  

But if you need to scale your app1 you can't because you have created the container with standalone mode.

来源:https://stackoverflow.com/questions/43408493/what-is-the-difference-between-docker-service-and-docke
录入日期:[2021/03/20 16:04:00]
收藏 推荐 打印 | 录入:mikebai | 阅读:
文章评论      
正在加载评论列表...
评论关闭