Unit Testing with a Docker Container

Installing Docker

First thing’s first – you will need to get Docker.  This will probably be the hardest part of this exercise!


  • If you are on Windows 10 and are willing to use Window’s HyperVisor for any VMs you need, you can try my installation script (you may want to remove some programs you don’t want from it).
  • If you are Windows 7/10 (and don’t want to use Docker for Windows since you need VirtualBox) or on Mac, try Docker Toolbox.
  • If you are on Linux, you may be able to download a package if you are on a late enough version of Docker.  I would also recommend downloading docker-compose.

Once you have Docker installed, make sure you can run the hello-world image to verify it is properly installed.

You should see…

Getting Code from GitHub and Running the Test

This code will be from an exercise in a previous post, where we installed Python manually.  I encourage you to read that post if you are not familiar with unit testing.

Now let’s get my code from GitHub.  Clone/download it onto your computer from the following GitHub site: docker-and-unit-test

Once you have that, go into the docker directory and run the following:




This is where magic happens.  Ok, it’s not magic… but it’s really, really cool and hands-off for the users of the Docker image (you in this case).




You should see something similar to the following:

This builds a Docker image from that docker-compose.yml file.  Let’s briefly look at this file.

It has a ‘service’ called ‘nosetests’.  A docker-compose file can be used to launch multiple “services” (different Docker containers) by running that one docker-compose up command.

build: docker tells Docker that it will build a new image based on the “docker” directory (which has a Dockerfile with instructions in it).  The Dockerfile states to start from the python:3.6 container and pip install a few packages in it.

The volumes command tells Docker that the current directory will be mapped to the /usr/src/testdir directory inside the container that is launched.

The command is run inside Docker.  In this case, it changes to the directory that has the code in it (the one that we mapped with the volume command), then it runs nosetests on it.

Finally, we tell it not to create a custom network but to use the existing Docker bridge network.

The really cool thing about this is even if you don’t have any of the necessary Docker images, they will get downloaded with the docker-compose up command.  The first time you run the compose command, it will take a lot longer since it is downloading a lot of images.  The next time though, it will run very quickly… in a matter of milliseconds/seconds.


Congratulations on running your first useful Docker container!  You did not have to download Python or any of its dependencies.  If you already have Docker, you only have to download code from Github and run docker-compose up on it.  Want to run the same environment on a continuous integration server?  Follow the same steps you just took for your own laptop!  Want to share this test environment with a co-worker?  It’s as simple as git clone and docker-compose up!

The best part is that this same command can be used to run a multitude of different environments with completely different purposes.  That, ladies and gentlemen, is the power of Docker.

Please follow and like us:

Leave a Reply

2 Comment threads
2 Thread replies
Most reacted comment
Hottest comment thread
3 Comment authors
tscottShoaib AkhtarGean Recent comment authors
newest oldest most voted
Notify of

Thanks, Tim. Hopefully we can get docker going here as soon as we move to Windows 10. Should happen soon.

Shoaib Akhtar
Shoaib Akhtar

Thanks tim for this blog. Your github repo does not contain .yml file that’s why docker-compose up command is not running