Welcome to the Linux Foundation Forum!

Lab 6.2 - Container Operations with Docker - alternatives

Options

Lab 6.2 is really nice - so many options to try out. But also so many syntax details to memorize.

A particularly mind-boggling example is this:
sudo docker container inspect --format=’{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ web-on-failure

Not a chance in the world I'll memorize that (I wonder who came up with this json syntax - must be a lonely fellow).

Turns out it's not so hard to parse the json file:
sudo docker container inspect web-on-failure | grep \"IPAddress\": | cut -d '"' -f4 | uniq

And here a one-liner for the exercise:

heiko@ubuntu:~# curl $(sudo docker container inspect web-on-failure | grep \"IPAddress\": | cut -d '"' -f4 | uniq)
Welcome to Container Fundamentals

The grep & cut works also with other parameters:

sudo docker container inspect web-on-failure | grep \"Gateway\": | cut -d '"' -f4 | uniq
sudo docker container inspect web-on-failure | grep \"Runtime\": | cut -d '"' -f4 | uniq

Guess what's the runtime for the docker containers?

Unfortunately it fails when the value isn't quoted (""). For some incongruous reason many values are quoted, others are not. Example:

"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,

Hope this is useful.

Comments

  • chrispokorni
    chrispokorni Posts: 2,165
    Options

    Hello @heiko_s,

    Thank you for providing the grep/cut alternatives.

    The JSON syntax assumes you are familiar with navigating the fields of the JSON file, while grep/cut requires you to know the position of the desired field. In both cases some some reconnaissance is required prior to extracting the desired value.

    Regards,
    -Chris

  • heiko_s
    heiko_s Posts: 99
    Options

    Thanks for the reply! Reconnaissance is definitely needed. But a simple grep gets me what I'm looking for, the rest is for scripting/automation.

    Have been doing some other exercises using Podman and realised what was written in the lessons - no networking out of the box in unprivileged mode. The pod doesn't even get an IP. Looking forward to the container networking chapter.

Categories

Upcoming Training