创建service
- 使用
docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
创建
[root@localhost ~]# docker service create
"docker service create" requires at least 1 argument.
See 'docker service create --help'.
Usage: docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new service
[root@localhost ~]# docker service create --help
Usage: docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new service
...
- 例子
[root@localhost ~]# docker service create nginx:latest
ujf1xtef2e7xqleqrmh75b0mg
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
[root@localhost ~]#
[root@localhost ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ujf1xtef2e7x mystifying_cartwright replicated 1/1 nginx:latest
Note:这里输出的id是service的id而非container的id。service等同于container。
查看service信息
-
docker service ls
:查看所有的service基本信息 -
docker service ps [SERVICE_ID]
:查看具体某个service的信息
[root@localhost ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ujf1xtef2e7x mystifying_cartwright replicated 1/1 nginx:latest
[root@localhost ~]#
[root@localhost ~]# docker service ps uj
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
nlj4so00wxz3 mystifying_cartwright.1 nginx:latest localhost.localdomain Running Running 3 minutes ago
- 查看容器信息发现容器id与service id确实不一样
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49e36233b260 nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp mystifying_cartwright.1.nlj4so00wxz3wo07lyktjzx3c
利用replicas复制多个容器
同一个服务启动多个容器
docker service update [SERVICE_ID] --replicas [NUMBER]
[root@localhost ~]# docker service update uj --replicas 3
uj
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
[root@localhost ~]#
[root@localhost ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ujf1xtef2e7x mystifying_cartwright replicated 3/3 nginx:latest
[root@localhost ~]# docker service ps uj
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
nlj4so00wxz3 mystifying_cartwright.1 nginx:latest localhost.localdomain Running Running 13 minutes ago
v7l8v247h2qo mystifying_cartwright.2 nginx:latest localhost.localdomain Running Running 45 seconds ago
xy85ftdwcpmp mystifying_cartwright.3 nginx:latest localhost.localdomain Running Running 45 seconds ago
[root@localhost ~]#
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
753076d85b2a nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp mystifying_cartwright.2.v7l8v247h2qo3x06tk31syvbx
6cf61228c7a3 nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp mystifying_cartwright.3.xy85ftdwcpmpibb41h8kkjof6
49e36233b260 nginx:latest "/docker-entrypoint.…" 13 minutes ago Up 13 minutes 80/tcp mystifying_cartwright.1.nlj4so00wxz3wo07lyktjzx3c
[root@localhost ~]#
swarm维护service的replicas
- 先删除掉service中的某个replica即删除一个容器,会发现swarm会自动开启另一个replica
[root@localhost ~]# docker container rm -f 753
753
[root@localhost ~]#
[root@localhost ~]# docker service ps uj
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
nlj4so00wxz3 mystifying_cartwright.1 nginx:latest localhost.localdomain Running Running 16 minutes ago
p16tkh74v5om mystifying_cartwright.2 nginx:latest localhost.localdomain Running Running 11 seconds ago
v7l8v247h2qo \_ mystifying_cartwright.2 nginx:latest localhost.localdomain Shutdown Failed 17 seconds ago "task: non-zero exit (137)"
xy85ftdwcpmp mystifying_cartwright.3 nginx:latest localhost.localdomain Running Running 4 minutes ago
停止service
docker service rm [SERVICE_ID]
[root@localhost ~]# docker service rm uj
uj
[root@localhost ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
[root@localhost ~]#
网友评论