Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grep output split for docker application

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;}'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading