我们何时使用docker service create
命令,何时使用docker run
命令?
我们何时使用docker service create
命令,何时使用docker run
命令?
docker run
命令创建一个独立的容器。docker service create
命令在计算机群(称为swarm)中创建该服务的实例(称为task)。这些任务当然是容器,但不是独立容器。从某种意义上讲,服务在实例化任务时充当模板。例如:
docker service create --name MY_SERVICE_NAME --replicas 3 IMAGE:TAG
创建3个任务的MY_SERVICE_NAME
服务,该服务基于IMAGE:TAG
镜像。更多信息可以在这里找到。Docker run 命令将启动一个单独的容器。
使用 Docker 服务管理一组容器(来自同一镜像)。您可以对它们进行扩缩容(启动多个容器)或更新操作。
docker service ls
查看两者都可以),那么你如何获取属于该组的Docker容器ID列表,以便你可以ssh连接到其中任意一个容器?对我来说,这似乎是许多人想做的事情,就像我一样,但答案似乎非常难寻找...(感谢阅读,希望您能分享您的想法)。 - therobyouknow您可能希望阅读 "docker service is the new docker run"。
根据这些幻灯片,"docker service create" 就像是一个 "进化版" 的 docker run。如果您想要将容器部署到Docker Swarm中,则需要创建一个"服务"。
docker run
命令时,docker-cli
会向dockerd
守护程序发送API查询以运行指定的容器。docker run
命令所做的操作只会影响您运行该命令的单个节点/机器/主机。如果您在容器中添加卷或网络,则这些资源只能在您运行docker run
命令的单个节点上可用。
node-01.dc.local
的节点,在这个节点上我们已经执行了以下命令,####### 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
node-02.dc.local
的机器。你希望将服务扩展和分发到新创建的节点上。
因此,我们在node-02.dc.local
节点上运行了以下命令:####### 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.
####### 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
Docker服务就像容器的"蓝图"。例如,您可以将简单的工作程序定义为一个服务,然后将该服务扩展到20个容器以快速处理队列。之后,您可以将该服务缩减回3个容器。此外,通过Swarm,这些容器可以部署到群集的不同节点上。
但是,我也建议阅读文档,就像@Tristan建议的那样。