创建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 --helpUsage: docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]Create a new service...
例子
[root@localhost ~]# docker service create nginx:latestujf1xtef2e7xqleqrmh75b0mgoverall progress: 1 out of 1 tasks1/1: running [==================================================>]verify: Service converged[root@localhost ~]#[root@localhost ~]# docker service lsID NAME MODE REPLICAS IMAGE PORTSujf1xtef2e7x 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 lsID NAME MODE REPLICAS IMAGE PORTSujf1xtef2e7x mystifying_cartwright replicated 1/1 nginx:latest[root@localhost ~]#[root@localhost ~]# docker service ps ujID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTSnlj4so00wxz3 mystifying_cartwright.1 nginx:latest localhost.localdomain Running Running 3 minutes ago
查看容器信息发现容器id与service id确实不一样
[root@localhost ~]# docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES49e36233b260 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 3ujoverall progress: 3 out of 3 tasks1/3: running [==================================================>]2/3: running [==================================================>]3/3: running [==================================================>]verify: Service converged[root@localhost ~]#[root@localhost ~]# docker service lsID NAME MODE REPLICAS IMAGE PORTSujf1xtef2e7x mystifying_cartwright replicated 3/3 nginx:latest[root@localhost ~]# docker service ps ujID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTSnlj4so00wxz3 mystifying_cartwright.1 nginx:latest localhost.localdomain Running Running 13 minutes agov7l8v247h2qo mystifying_cartwright.2 nginx:latest localhost.localdomain Running Running 45 seconds agoxy85ftdwcpmp mystifying_cartwright.3 nginx:latest localhost.localdomain Running Running 45 seconds ago[root@localhost ~]#[root@localhost ~]# docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES753076d85b2a nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp mystifying_cartwright.2.v7l8v247h2qo3x06tk31syvbx6cf61228c7a3 nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp mystifying_cartwright.3.xy85ftdwcpmpibb41h8kkjof649e36233b260 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 753753[root@localhost ~]#[root@localhost ~]# docker service ps ujID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTSnlj4so00wxz3 mystifying_cartwright.1 nginx:latest localhost.localdomain Running Running 16 minutes agop16tkh74v5om mystifying_cartwright.2 nginx:latest localhost.localdomain Running Running 11 seconds agov7l8v247h2qo \_ 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 ujuj[root@localhost ~]# docker service lsID NAME MODE REPLICAS IMAGE PORTS[root@localhost ~]#
