开发模式采用docker stack depoly部署时 无法方便登录container
脚本功能
- 查看 service
- 登录service
- 执行容器内命令
#!/usr/bin/env bash
# 设置期望的参数长度
expected_length=1
# 检查参数个数
if [ $# -lt $expected_length ]; then
echo "格式:[service name] [cmd]"
exit 1
fi
NAME=$1
if [ "$NAME" == "ls" ]; then
docker service ls|awk '{print $2}'
exit 0
fi
CMD=${2:-'sh'}
SERVICE_ID=`docker service ps $NAME|awk '{print $1}'|grep -v ID`
docker exec -it `docker ps|grep $SERVICE_ID|awk '{print $1}'` $CMD ${@:3}