What is FizzBuzz?
A popular interview question is the “FizzBuzz test”, its based on a children’s game where you count from 1 to 100 and for multiples of 3 say “Fizz”, multiples of 5 say “Buzz”, if both say “FizzBuzz” else say the number, example 8.
Translated into to the code domain the question you could ask is:
“Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.”
Whats this got to do with Docker?
Well the hello world app from Docker is great but this is a little more fun and I like to put things into containers. FizzBuzz seems like a good candidate and is simpler to understand than the Docker Image for SMS.
The ‘hello world’ example from Docker also starts its life from an image they call ‘SCRATCH’ – this is great but not really useful in real world applications (Well ones I am capable of building anyway)
My ‘FizzBuzz’ image starts its life from ‘microsoft/aspnetcore:2.0’ so its far larger but comes with the .net goodness I know and love.
FizzBuzz is also a great way to demonstrate how easy Docker is to use and the powerful features it has! I hope it inspires developers looking to try their hand at Docker and gives a starting point.
Perhaps instead of asking a candidate to do the sudo code for you, ask them how they will code and create a docker image 🙂
I used a C# Console Application targeting .Net Core 2 so the resulting container is platform agnostic.
Variables are used for the lowerBound, upperBound, fizzAt, buzzAt and outPut values as things need to be maintainable. This demonstrates that things can change, just look at our recent VAT increase!
These are read from injected environmental variables but do have hard coded defaults set, this is fine for this example however for production applications anything hard coded is frowned upon.
var lowerBound = Environment.GetEnvironmentVariable("lowerBound") == null ? 1 : Convert.ToInt32(Environment.GetEnvironmentVariable("lowerBound"));
Docker builds images from Dockerfiles, I used cloud.docker.com to create a repository which builds the resulting image to hub.docker.com. This can then be pulled from store.docker.com with the following commmand:
docker pull carlpaton/fizzbuzz
The docker file runs the following steps:
1. Creates a builder image from microsoft/dotnet:2.0-sdk
2. Copys the solution file
3. Copys the project file and restores dependencys (same like your IDE does from Nuget)
4. Copys your class files
5. Builds & Publishes your code (same like your IDE’s compiler does)
6. Finally from microsoft/aspnetcore:2.0 builds the image and copys in the compiled code from the ‘builder’ image
The variables based on how you want the resulting container to behave need to be injected when you spin up, these need to be in a file thats relative to your current director.
lowerBound=1 upperBound=100 fizzAt=3 buzzAt=4
You then inject with –env-file=env_file_name.env
To pull the container and spin up the image you will need to simply run the following shell script.
Note that you can pass the environmental variables in the command, or even better you can use Docker Compose.
Example shell script:
#!/bin/bash docker pull carlpaton/fizzbuzz:v1.0.0 sudo docker container kill fizzbuzz sudo docker rm fizzbuzz sudo docker run --env-file=env_file_name.env --name fizzbuzz carlpaton/fizzbuzz:v1.0.0 sudo docker start fizzbuzz sudo docker ps --all
I believe in sharing things, all the code for the above is free MIT.
References & Perquisites
Before running the above you will need to setup Docker.