I learn about Docker for service hosting. I want to split the desired line somehow to obtain a desired token. In case the token corresponds to CONTAINER_ID
, I would split the line in spaces and obtain the first token. I do not even know to start this shell pipe, but it seems useful for every SRE developer. 🙂
I tried the command below:
docker ps -a | grep -w <image-name> | head -n1 | awk '{print $1;}'
The output below corresponds to command 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
2
Answers
FWIW, I often use the following few commands:
The keyword would be something unique about the name of your image.
Assumptions:
IMAGE
or a partialIMAGE
IMAGE
match is found then display the correspondingCONTAINER ID
and exit (ie, only interested in the first match)Using
docker.ps.out
to simulate thedocker/ps
output, and adding a few (bogus) lines:One
awk
idea:For
-v img="post"
:For
-v img="proxy"
:For
-v img="XXX"
:OP’s current code uses
grep -w
to indicate a need for an exact match on theIMAGE
field so a small change to ourawk
script:For
img="postgres:14.5"
:For
img=postgres:17.2"
: