1

My script.sh:

#/bin/sh
docker run --name foo 

(Just assume that the docker command works and the container name is foo. Can't make the actual command public.)

I have a script that runs a docker container. I want to check that it ran successfully and echo the successful running status on the terminal.

How can I accomplish this using the container name? I know that I have to use something like docker inspect but when I try to add that command, it only gets executed after I ^C my script probably because docker has the execution.

In this answer, the docker is executed in some other script so it doesn't really work for my use case.

Raj
  • 2,150
  • 2
  • 22
  • 43
  • Do you expect the container to exit promptly? As with any other shell command, `$?` will have its exit status and 0 is “success”. – David Maze Oct 16 '19 at 16:40

3 Answers3

1

The linked answer from Jules Olléon works on permanently running services like webservers, application servers, database and similar software. In your example, it seems that you want to run a container on-demand, which is designed to do some work and then exit. Here, the status doesn't help.

When running the container in foreground mode as your example shows, it forwards the applications return code to the calling shell. Since you didn't post any code, I give you a simple example: We create a rc.sh script returning 1 as exit-code (which normally indicates some failure):

#!/bin/sh
echo "Testscript failed, returning exitcode 1"
exit 1

It got copied and executed in this Dockerfile:

FROM alpine:3.7
COPY rc.sh .
ENTRYPOINT [ "sh", "rc.sh" ]

Now we build this image using docker build -t rc-test . and execute a short living container:

$ docker run --rm rc-test
Testscript failed, returning exitcode 1

Bash give us the return code in $?:

$ echo $?
1

So we see that the container failed and could simply check them e.g. inside some bash script with an if-condition to perform some action when it fails:

#!/bin/bash

if ! docker run --rm rc-test; then
    echo "Docker container failed with rc $?"
fi
John Kugelman
  • 307,513
  • 65
  • 473
  • 519
Lion
  • 11,498
  • 13
  • 55
  • 113
  • I had initially tried this and expected it to work but after running the ./script.sh there is no response until I ^C where I see the Running: true. – Raj Oct 16 '19 at 22:15
  • Did you pass additionall parameters or similar? The code from my example was tested with a simple bash script that got execute in the `ENTRYPOINT` of the container. – Lion Oct 22 '19 at 17:27
0

After running your docker run command you can check this way if your docker container is still up and running:

s='^foo$'

status=$(docker ps -qf "name=$s" --format='{{.Status}}')
[[ -n $status ]] && echo "Running: $status" || echo "not running"
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • I had initially tried this and expected it to work but after running the ./script.sh there is no response until I `^C` where I see the `Running: true`. – Raj Oct 16 '19 at 22:14
  • A quicker way to do this: ``[[ "$(docker ps -aq -f status=running -f name=$s)" == "" ]] && echo "not running" || echo "Running"`` – JD Allen Jun 11 '20 at 17:27
0

You just need to execute it with "-d" to execute the container in detached mode. With this, the solutions provided in the other post or the solution provided by @anubhava are both good solutions.

docker run -d -name some_name mycontainer
s='^some_name$'
status=$(docker ps -qf "name=$s" --format='{{.Status}}')
[[ -n $status ]] && echo "Running: $status" || echo "not running"
osmuogar
  • 109
  • 8
  • I had initially tried this and expected it to work but after running the ./script.sh there is no response until I ^C where I see the Running: true. – Raj Oct 16 '19 at 22:15