I wrote a blog post about unit testing Docker images: [https://blog.phonito.io/unit-testing-docker-images/](https://blog.phonito.io/unit-testing-docker-images/)
Everyone understands the need for testing their applications, but few people go on to test that final step of their build process, their Docker image!
Edit: This is actually functional testing! Can't the title though 🙂
7 thoughts on “TIL: You can unit testing Docker images”
That’s a good idea!
I must note, this is not unit testing. It’s integration testing.
To unit test docker images, you would have a static mock app for testing (just nitpicking here, but I wanted to make sure *unit* testing keeps it’s purpose clear).
I’ve been testing mine via [BATS](https://github.com/sstephenson/bats), which is a bash-based command line testing framework. It’s not ideal, but it works well enough. I’ll have to check out your solution too.
Thanks my dude
Have you listened to Gianluca Arbezzano’s DockerCon 2019 talk, Write Maintainable Integration Tests with Docker (links at bottom)?
He goes through a couple of different approaches and then advocates for using the Docker SDK and the TestContainers project, of which he contributes to the testcontainers-go repo. The other core benefit of TestContainers is canned containers.
I like the suggestion to look at InSpec but I hate the baggage of Chef, as I tried Chef in ~2012 on Windows and the whole experiment was a debugging nightmare, since OpsCode Chef Stable was dependent on a version of Ruby without basic standard library support like standard out for debugging why a Recipe failed. I found this blog post to be a gentle introduction to InSpec:
https://blog.chef.io/docker-container-compliance-with-inspec/ The neat trick is running the latest Docker CIS Benchmark in a one liner. Definitely worth at least trying.
Blog post: https://www.docker.com/blog/maintainable-integration-tests-with-docker/
YouTube (40m): https://youtu.be/RoKlADdiLmU
RemindMe! Six weeks
RemindMe! 2 weeks
This is not unit testing or functional testing. It’s a grab bag of verifications that are completely redundant.
\> Packages are installed
Package managers have their own tests. Docker itself has its own tests. You unit tests would fail if your dependencies were not installed.
\> Configuration is correct
Your application would not pass its tests if it were not.
\> Services/Processes are running
Do you not have a staging environment? Or are you also trying to verify basic things like unix processes?
\> Ports are open
More duplication of linux tests and docker tests.