gohar shah
gohar shah

Reputation: 175

"Retrieving docker inspect" requires at least 1 argument in bash script

I am trying to check container is up and running in the bash script. I am able to retrieve name of the dockder container but I am facing issue in docker inspect

CONTAINER=sudo docker ps -qf "name=pipeline"
echo $CONTAINER

RUNNING=$(docker inspect -f {{.State.Running}} $CONTAINER 2> /dev/null)
STARTED=$(docker inspect -f {{.State.StartedAt}} $CONTAINER)
NETWORK=$(docker inspect -f {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} $CONTAINER)
echo "OK - $CONTAINER is running. IP: $NETWORK, StartedAt: $STARTED"

Also tried with the below format

 docker inspect --format='{{.State.StartedAt}}' $CONTAINER) with sudo and without sudo

Running bash script command

 bash ./testing.sh

Error

"docker inspect" requires at least 1 argument.
 See 'docker inspect --help'.
 Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]
 Return low-level information on Docker objects
 Template parsing error: template: :1: unexpected unclosed action in range
 OK -  is running. IP: , StartedAt:

Thank you for your help

Upvotes: 0

Views: 2186

Answers (1)

David Maze
David Maze

Reputation: 159382

If you know the container's name, you can just use it in commands like docker inspect; you don't need the initial lookup.

RUNNING=$(docker inspect -f '{{.State.Running}}' pipeline 2> /dev/null)

Always quote shell variables that you expect to be used as parameters. If the variable $CONTAINER comes back empty, but when you use it it's quoted, there will be a parameter there; if it's not quoted, it just gets dropped from the command. (But you'll get a different error if you try to docker inspect "" with an empty container name.)

STARTED=$(docker inspect -f '{{.State.StartedAt}}' "$CONTAINER")

The container-private IP address isn't useful in many environments (on MacOS or Windows hosts, if Docker is inside a VM, calling from a different host). I would not recommend looking it up at all.

If you're just trying to get this diagnostic state from Docker, and not trying to act on it within the script, you can just write everything in the -f template option. Combining this all together, I might replace everything in the script in the question with the single command

docker inspect \
  -f 'Container {{.Id}} is {{.State.Status}}, started at {{.State.StartedAt}}' \
  pipeline

Don't write sudo in scripts, but run the script with sudo permissions if you need that to run docker commands.

Upvotes: 1

Related Questions