Slides from Alexei Ledenev's talk on testing strategies for Docker containers. Abstract: Being able to build a Docker container is only part of its development story. You need to be able to test your newly created containers. Ideally, we’d all like our container testing to be repeatable and portable. But there’s more than one way to achieve this so how do you know which method to choose? During this talk, we will demonstrate several approaches for testing Docker containers, discussing benefits and drawbacks with each approach.
12. # build demo app & test container
$ docker build -t demo:apptest -f docker/Dockerfile.build.apptest .
# run tests
$ docker run -it --rm demo:apptest script/test.sh
# run app
$ docker run -d -p 10000:10000 demo:apptest demo
https://asciinema.org/a/100216
13. Benefits and Drawbacks
Simple CI flow
Application portability
Portable DEV
environment
Portable TEST
environment
• Bigger Docker image size
• Need to rebuild the whole
image on code or test
change
• Polluted Docker image
• Need to manage test
results
16. # build test aware Docker image
$ docker build -t demo:testaware -f docker/Dockerfile.build.testaware .
# run tests
$ docker run -it --rm demo:testaware
# get app layers from image
$ APP_LAYER=$(docker history demo:testaware |
grep -e ‘LABEL SPLIT=T’ |
awk ‘{print $1}’)
# tag APP_LAYER and push it
$ docker tag $APP_LAYER demo:app
# run demo app
$ docker run -it --rm -p 10000:10000 --name demo demo:app
https://asciinema.org/a/100260
17. Benefits and Drawbacks
Application portability
Portable TEST and
DEV environments
2 separate images
One Dockerfile
• Need to rebuild the whole
image on code or test
change
• Need some shell “magic” to
create a clean APP image
(non-Docker way)
18. – Me :-)
“After trying multiple approaches, the
following is the one that I can
recommend.”
19. Docker Automation Flow
Docker automation CI/CD flow consists from multiple steps, like: build, scan, test, run,
deploy and others.
Each step requires different tools, runtime, packages, data and configuration files
Use dedicated Docker container for every step
For example:
Builder Container - use it to build your app. Docker container with compilers, linters,
package managers and other dev tool.
Test Container(s) - user it to test your app. Docker container with testing tools, test
scripts, runtimes and helper files and required packages.
App Container - the one you push/deploy. Docker container with application binary,
runtime and required packages only.
22. # build Build Container image
$ docker build -t demo:builder -f docker/Dockerfile.builder .
# use Builder container to build the app
$ docker run -it --rm
-v $(pwd)/.dist:/go/src/app/.dist
-e CGO_ENABLED=0 demo/builder go build -v -o .dist/demo
# build App Container image
$ docker build -t demo:dist -f docker/Dockerfile.dist .
# build Test Container image
$ docker build -t demo:test -f docker/Dockerfile.test .
# create common network for app and test
$ docker network create mynet
# run demo app
$ docker run -d --name demo --network mynet demo:dist
# run tests
$ docker run -it --rm --name tests --network mynet demo:test
newman run -e test/demo.env.json
test/demo-rest-api-tests.postman.json
23. Benefits and Drawbacks
• Application portability
• Portable DEV
environment
• Build ANYWHERE
• Portable TEST
environment
• Test ANYWHERE
• Small APP image
• Fast builds
• Need an approach/tool to
automate and orchestrate
Docker build flow
• Bash, Ansible, …
• dobi, habitus, …
• Docker CI/CD service, like
codefresh.io
25. ● Create your Codefresh account at www.codefresh.io
● Additional info on Containers & Docker Driven Development -
www.codefresh.io/blog & www.codefresh.io/container-academy/
● Twitter - @codefresh
● Questions for sales - sales@codefresh.io
● Create your Cloud 66 account at www.cloud66.com/
● Twitter - @cloud66
Next Steps