Hi, all
I am trying to implement automated test running from Makefile target. As my test dependent on running docker container, I need to check that container is up and running during whole test execution, and restart it, if it’s down. I am trying do it with bash script, running in background mode.
At glance code looks like this:
run-tests:
./check_container.sh &
docker-compose run --rm tests;
#Need to finish check_container.sh right here, after tests execution
RESULT=$$?;
docker-compose logs test-container;
docker-compose kill;
docker-compose rm -fv;
exit $$RESULT
Tests has vary execution time (from 20min to 2hrs), so I don’t know before, how much time it will take. So, I try to poll it within script longer, than the longest test suite. Script looks like:
#!/bin/bash
time=0
while [ $time -le 5000 ]; do
num=$(docker ps | grep selenium--standalone-chrome -c)
if [[ "$num" -eq 0 ]]; then
echo 'selenium--standalone-chrome container is down!';
echo 'try to recreate'
docker-compose up -d selenium
elif [[ "$num" -eq 1 ]]; then
echo 'selenium--standalone-chrome container is up and running'
else
docker ps | grep selenium--standalone-chrome
echo 'more than one selenium--standalone-chrome containers is up and running'
fi
time=$(($time + 1))
sleep 30
done
So, I am looking for how to exit script exactly after test running is finished, its meant, after command docker-compose run --rm tests
is finished?
P.S. It is also ok, if background process can be finished on Makefile target finish
2
Answers
Docker (and Compose) can restart containers automatically when they exit. If your
docker-compose.yml
file has:Then Docker will do everything your shell script does. If it also has
then the
docker-compose run tests
line will also cause theselenium
container to be started, and you don’t need a script to start it at all.When you launch a command in the background, the special parameter
$!
contains its process ID. You can save this in a variable and later kill(1) it.In plain shell-script syntax, without Make-related escaping: