d3ming
d3ming

Reputation: 9080

How to tell if a docker container run with -d has finished running its CMD

I want to make a simple bash script which runs one docker container with -d and then do something else if and only if the container has finished running its CMD. How can I do this while avoiding timing issues since the docker container can take a while to finish starting up?

My only thought was that the Dockerfile for the container will need to create some sort of state on the container itself when it's done and then the bash script can poll until the state file is there. Is there a better / standard way to do something like this?

Essentially I need a way for the host that ran a docker container with -d to be able to tell when it's ready.

Update

Made it work with the tailing logs method, but it seems a bit hacky:

  docker run -d \
      --name sauceconnect \
      sauceconnect

  # Tail logs until 'Sauce Connect is up'
  docker logs -f sauceconnect | while read LINE
  do
    echo "$LINE"
    if [[ "$LINE" == *"Sauce Connect is up"* ]]; then
      pkill -P $$ docker
    fi
  done

Upvotes: 3

Views: 796

Answers (1)

BMW
BMW

Reputation: 45243

You should be fine to check the logs via docker logs -f <containter_name_or_ID>

-f : same as tail -f 

For example, the CMD is finished, and export a log as JOB ABC is successfully started.

your script can detect and run the rest jobs after get it.

Upvotes: 1

Related Questions