shell Docker应用程序的Grep输出拆分[重复]

dm7nw8vv  于 2022-11-16  发布在  Shell
关注(0)|答案(2)|浏览(159)

此问题在此处已有答案

Shell command to Get container id from "docker ps"(6个答案)
Why does bash script work depend on the size of the terminal window?(3个答案)
25天前关闭。
我学习了服务托管的Docker。我想以某种方式拆分所需的行以获得所需的令牌。如果令牌对应于CONTAINER_ID,我会将该行拆分为空格并获得第一个令牌。我甚至不知道启动此shell管道,但它似乎对每个SRE开发人员都有用。:-)
我试着下了命令:
docker ps -a | grep -w <image-name> | head -n1 | awk '{print $1;}'
以下输出对应于命令run docker ps -a

CONTAINER ID   IMAGE                         COMMAND                  CREATED         STATUS         PORTS                                       NAMES
0e7e52667c2e   sappio-1                      "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   hardcore_panini
d9cd1a9d2c37   eclipse-mosquitto:2-openssl   "/docker-entrypoint.…"   2 weeks ago     Created                                                    cedalo_platform_mosquitto_1
96f65f3e8bd8   postgres:14.5                 "docker-entrypoint.s…"   3 weeks ago     Up 4 hours                                                 tests_db
bdb51a386349   nginxproxy/nginx-proxy        "/app/docker-entrypo…"   5 months ago    Created                                                    dreamy_bouman
j7dteeu8

j7dteeu81#

FWIW中,我经常使用以下几个命令:

img=$(docker image ls | grep some_keyword | head -1 | awk '{print $3}') && echo "image is $img"

docker run -it $img

cont=$(docker ps | tail +2 | head -1 | awk '{print $1}') && echo "container is $cont"

关键字可以是关于你的图像名称的一些独特的东西。

y1aodyip

y1aodyip2#

假设条件:

  • 需要搜索IMAGE或部分IMAGE
  • 如果/当找到IMAGE匹配时,则显示相应的CONTAINER ID并退出(即,仅对第一个匹配感兴趣)

使用docker.ps.out模拟docker/ps输出,并添加几行(伪)代码:

$ cat docker.ps.out
CONTAINER ID   IMAGE                         COMMAND                  CREATED         STATUS         PORTS                                       NAMES
0e7e52667c2e   sappio-1                      "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   hardcore_panini
d9cd1a9d2c37   eclipse-mosquitto:2-openssl   "/docker-entrypoint.…"   2 weeks ago     Created                                                    cedalo_platform_mosquitto_1
96f65f3e8bd8   postgres:14.5                 "docker-entrypoint.s…"   3 weeks ago     Up 4 hours                                                 tests_db
bdb51a386349   nginxproxy/nginx-proxy        "/app/docker-entrypo…"   5 months ago    Created                                                    dreamy_bouman
001234abcdef   postgres:14.5                 "docker-entrypoint.s…"   3 weeks ago     Up 4 hours                                                 tests_db
9999000aabbc   postgres:14.5                 "docker-entrypoint.s…"   3 weeks ago     Up 4 hours                                                 tests_db

一个awk创意:

cat docker.ps.out | awk -v img="post" '
FNR>1 && index($2,img) {print $1; exit}'    # skip header record and if img found in 2nd field then print 1st field and exit the script

对于-v img="post"

96f65f3e8bd8

对于-v img="proxy"

bdb51a386349

对于-v img="XXX"

-- no output == no match

OP的当前代码使用grep -w来指示需要对IMAGE字段进行精确匹配,因此对awk脚本进行了一个小的更改:

cat docker.ps.out | awk -v img="post" '
FNR>1 && $2==img {print $1; exit}'    # skip header record and if 2nd field == img then print 1st field and exit the script

对于img="postgres:14.5"

96f65f3e8bd8

对于img=postgres:17.2"

-- no output == no match

相关问题