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.
答案 0 :(得分:4)
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.
答案 1 :(得分:0)
如何检查所有 docker-compose up 服务是否启动成功?
您可以通过比较所有正在运行的服务和所有服务来检查 docker-compose 中的所有服务是否正在运行。请注意,这可能并不意味着您的服务正在运行 - 仅表示 docker 容器正在运行。下面的 shell 脚本只是比较了运行和所有服务的计数:
running="$(docker-compose ps --services --filter "status=running")"
services="$(docker-compose ps --services)"
if [ "$running" != "$services" ]; then
echo "Following services are not running:"
# Bash specific
comm -13 <(sort <<<"$running") <(sort <<<"$services")
else
echo "All services are running"
fi
似乎 docker 中的 negating filter is not possible,所以它必须是这样。