6

How do I check in a bash script if this

sudo docker images -q nginx

gives me a result string, which means this container is existing

sudo docker images -q nginx
if [ $? != '' ]
then
    echo "existing"
else
    echo "missing
2
  • 5
    The thing to note is that docker images will list images, not containers.
    – zeppelin
    Commented Feb 10, 2017 at 10:35
  • @zeppelin Container image.
    – Kusalananda
    Commented Aug 22, 2022 at 5:46

3 Answers 3

10

I tried docker images -q "{Image Name}", as suggested in the "best answer", but it only returned the ID of the Image, not of the container.  No matter if the container is running or not, it always returns the Image ID.

If you want to know whether or not the container is running, you need to apply the following command:

docker ps -q -f name="{Container Name}"

If the container exists and is running, the container ID is returned.  If it doesn't exist, or exists but is stopped, an empty string comes back. If you want to search through all (running and stopped) containers, use "-a" argument for the "docker ps" command like this:

docker ps -a -q -f name="{Container Name}"

You can also search by other fields, for example by container id:

docker ps -a -q -f id="{Container ID}"

For more details see the official "docker ps" documentation.

9
  • This just repeats a similar command as the user in the question is using. It does not go on to say what the user's issue is due to or how to fix it. Note that the user is asking how to use the command in a bash script. This answer is not addressing that at all.
    – Kusalananda
    Commented Aug 22, 2022 at 5:48
  • Since this is the command line call it should also work in a bash script, shouldn't it ?
    – Thomas
    Commented Aug 22, 2022 at 6:03
  • It does work perfectly in a bash script, and so does the user's docker command in the question too. The actual issue is about using the output of the command to determine whether the container exists or not, and then execute the correct echo statement.
    – Kusalananda
    Commented Aug 22, 2022 at 6:12
  • Yet my comment only refers to the following line :
    – Thomas
    Commented Aug 22, 2022 at 12:38
  • ... docker images -q "{Image Name}"
    – Thomas
    Commented Aug 22, 2022 at 12:38
8

$? isn't a string but the exit status of sudo (in this case). To use that properly, compare it against zero with -gt, or use if (( $? )) (in a shell like bash or ksh93 that does arithmetic evaluation with (( ... ))).

If sudo docker images -q nginx gives you a string if the container image exists and nothing if it doesn't, then you may store that in a variable and see if it's empty or not:

result=$( sudo docker images -q nginx )

if [[ -n "$result" ]]; then
  echo 'Container image exists'
else
  echo 'No such container image'
fi

However, using sudo inside a script is awkward since the tool often requires interactive prompting for passwords, and it's better to use sudo instead to run the script itself (and then use sudo inside the script only if you need to assume some other non-root user's identity).

docker inspect is another command for checking the information about containers:

docker inspect -f '{{.Config.Image}}' nginx

This would give the container image hash for the nginx container. It would also return a proper exit status that you can use to determine whether the container exists at all:

if docker inspect -f '{{.Config.Image}}' nginx >/dev/null 2>&1
then
    echo 'Container image exists'
else
    echo 'Container does not exist'
fi

Or, you may pick out the output string and see whether it's empty or not:

result=$( docker inspect -f '{{.Config.Image}}' nginx 2>/dev/null )

if [[ -n "$result" ]]; then
  echo 'Container image exists'
else
  echo 'No such container image'
fi

I'm discarding the standard error stream by redirecting it to /dev/null since it will complain if the container image does not exist.

You may also use docker inspect to figure out if a container is running or not by inspecting {{.State.Running}}:

result=$( docker inspect -f '{{.State.Running}}' nginx )

if [[ $result == "true" ]]; then
  echo 'Container is running'
else
  echo 'Container is not running'
fi
0
1

Try this

sudo docker inspect --format="{{.State.Running}}" $CONTAINER
# If the $CONTAINER is running or not and exists, $? is 0
# But If the $CONTAINER doesn't exist or was never created,
# $? is 1 on docker, or 125 on podman 
if [ $? -eq 0 ];
then
     echo "existing"
else
     echo "missing"
fi
1
  • 2
    Welcome to the site, and thank you for your contribution. Please consider editing your post to add some explanation, in particular in what way the approach shown in your answer is better/more robust than the existing answers.
    – AdminBee
    Commented Jul 19, 2021 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .