Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all docker-compose up services started successfully?

How can I check if the services of docker-compose up started successfully?

Context: I'm adding CI to my project and as part of it, I'd like to have my CI build the docker image and execute a docker-compose command to see if all the services can be brought up successfully before moving on to the next phase of the CI.

I'm thinking that I should do something to the effect of:

$ docker build -t myimage .
$ docker-compose up -d

# attempt count the images with a state of "Exit" and if it's more than 1, return 1
# what happens if an image name contains the substring "Exit" but is running healthily?
$ if [[ $(docker-compose -f docker-compose-local.yml ps | grep "Exit" | wc -l) -ne 0 ]]; then return 1; fi

I suspect that looking for the state of the container through the ps subcommand isn't great and that there's a better solution. I've looked but haven't found one.

What are the possible states of a docker container? The docker docks don't mention the possible states.

like image 288
Paymahn Moghadasian Avatar asked Feb 26 '18 17:02

Paymahn Moghadasian


1 Answers

You might be able to try running with a custom HEALTHCHECK command, which could write a status to a temporary file or something, which the CI process can check for, and then terminate the containers afterwards.

HEALTHCHECK will also cause the health status of each container to be stored in the respective health_status field in the output of docker inspect, which might be easier for you than docker ps.

More generally, I have worked on some applications where we created a type of test that we called a "smoke test" -- very similar to a full acceptance test, except that it could still use artificial or fixture data of some types. But the actual requests made to the web / container applications were real.

Then what you do is have a script which starts up your set of containers, followed by executing a test suite against whatever application(s) are backed by those containers (e.g. database queries, http requests, whatever).

Often you can re-use a unit testing framework from your language of choice for this. In my case, we used pytest to write some Python-based smoke tests ... launch all the containers, then hit them with a variety of artificial requests for which the correct response was known. If the Python test process exited successfully, then the overall test passed and it would bring down the containers.

I like this approach because then the definition of "check if all docker-compose up services started successfully" just becomes some automated test like any other, and it is checked into source code somewhere. If the applications change, you change the tests accordingly, or else they cause failing builds.

Whereas, if you used a simplistic HEALTHCHECK command, it's feasible you could get false information. Maybe the containers appear to start and run, but if a certain data resource, or remote connection, etc., didn't succeed at start-up, you won't know unless your HEALTHCHECK explicitly involves verifying it.

Really it rarely matters to know just that the containers are up and running. Instead, what usually matters is some suite of "real" tests that hit the running containers and prove that everything needed to operate in a real use-case scenario is actually working correctly.

like image 122
ely Avatar answered Oct 11 '22 21:10

ely