如何在swarm中部署service时,指定容器运行的命令?


默认情况下,在swarm中部署一个service,会根据镜像中启动命令来启动容器,如果要进行测试也好,修改、调试也好,如何指定service中容器的启动命令?

 

方法非常的简单。

 

1、比如先查看某个镜像的启动命令

 

以alpine镜像为例

 

docker inspect d4deec2c521c

 

 

Entrypoint和CMD如下:

            "Cmd": [
                "/bin/sh"
            ],
            "Image": "sha256:b747534ae29d08c0c84cc4326caf04e873c6d02bb67cd9c7644be2b4fa8d2f31",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null

 

 

启动命令就是:/bin/sh

 

2、修改容器的默认启动命令

 

如果要部署这个service,但是容器不用这个命令,怎么搞?

 

docker service create \
  --name test-commd \
  172.20.58.152/baseimage/alpine:latest \
  ping www.baidu.com

 

 

没错,就是在镜像的后面,加上启动的命令即可。

 

查看service的启动命令

 

[root@nccztsjb-node-01 ~]# docker service ps test-commd
ID             NAME           IMAGE                                   NODE               DESIRED STATE   CURRENT STATE            ERROR     PORTS
rz463fgm9hk8   test-commd.1   172.20.58.152/baseimage/alpine:latest   nccztsjb-node-02   Running         Running 21 seconds ago  
 

 

查看service的日志,就是在使用PING命令

 

[root@nccztsjb-node-01 ~]# docker service logs test-commd
test-commd.1.rz463fgm9hk8@nccztsjb-node-02    | PING www.baidu.com (110.242.68.4): 56 data bytes
[root@nccztsjb-node-01 ~]# 

 

 

查看具体的容器的启动命令

 

[root@nccztsjb-node-02 ~]# docker ps --no-trunc

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

6e284d195cb1d34d3ad977ffdfafc7745f7b3d8619378a0cc20340434ce25554 172.20.58.152/baseimage/alpine:latest@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3 "ping www.baidu.com" 3 minutes ago Up 3 minutes test-commd.1.rz463fgm9hk808a1aj2v6i9lo

986a920894ed32769b109e5047fe15ea2775086962931fd94e45241ee30ba115 172.20.58.152/middleware/nginx:1.21.4@sha256:2f14a471f2c2819a3faf88b72f56a0372ff5af4cb42ec45aab00c03ca5c9989f "/docker-entrypoint.sh nginx -g 'daemon off;'" 44 hours ago Up 44 hours 80/tcp nginx.4.9kvrbnvumz04soafmtx2c7p54

[root@nccztsjb-node-02 ~]#

 

 

OK。

 

这样,就完成了对service中容器的运行命令的指定!

posted @ 2022-09-15 10:50  Zhai_David  阅读(151)  评论(0编辑  收藏  举报