Automating Docker on a Gitlab CI Server

I’ve currently been setting docker up to run containers for a Gitlab CI server. The basic premise is that the CI server will vagrant up a box, run ansible and finally run phpunit.

This has worked well, however getting the docker containers to destroy afterwards hasn’t been as clean cut.

From using a few other sources, I’m using this script via cron which runs every five minutes to stop and remove any containers that have been running for more than an hour (this is working for our current commit frequency but will need addresses as more things are moved to this CI server).

root@ci-runner:~# cat 
for i in $(docker ps -a | grep "hour ago" | cut -f1 -d" "); do docker stop $i && docker rm $i; done

This is working quite well so far, I’d be interested in how you’re doing this if you’re a docker user too.