skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. Assumptions:

    • need to search for an IMAGE or a partial IMAGE
    • if/when an IMAGE match is found then display the corresponding CONTAINER ID and exit (ie, only interested in the first match)

    Using docker.ps.out to simulate the docker/ps output, and adding a few (bogus) lines:

    $ 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
    

    One awk idea:

    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
    

    For -v img="post":

    96f65f3e8bd8
    

    For -v img="proxy":

    bdb51a386349
    

    For -v img="XXX":

    -- no output == no match
    

    OP’s current code uses grep -w to indicate a need for an exact match on the IMAGE field so a small change to our awk script:

    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
    

    For img="postgres:14.5":

     96f65f3e8bd8
    

    For img=postgres:17.2":

    -- no output == no match
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search