Issue
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
Solution
FWIW, I often use the following few commands:
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"
The keyword would be something unique about the name of your image.
Answered By - Pierre D Answer Checked By - Mary Flores (WPSolving Volunteer)