When a script starts a container that does a job and then exits, I want to know how it ended without parsing docker ps. docker wait does only that.
The command blocks until the containers you name have stopped, then prints one exit code per container.
docker run -d --name nightly-job example/job:1.0
docker wait nightly-job
The second line just sits there while the job runs. When the container stops it prints something like 0 and returns. Because the exit code is also the output, it drops straight into a script:
code=$(docker wait nightly-job)
if [ "$code" -ne 0 ]; then
docker logs --tail 50 nightly-job
fi
If the container is already stopped, the command returns immediately with its exit code, so there is no race when the job finishes before you get to the wait line. You can give it several names and get one code for each.
It waits for as long as the container runs, so for a job that might hang, put a limit around it, for example timeout 600 docker wait nightly-job, or your script sits there forever.
Further reading: docker container wait