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”.”
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:
- Creates a builder image from microsoft/dotnet:2.0-sdk
- Copys the solution file
- Copys the project file and restores dependencys (same like your IDE does from Nuget)
- Copys your class files
- Builds & Publishes your code (same like your IDE’s compiler does)
- 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.
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:
I believe in sharing things, all the code for the above is free MIT.
Before running the above you will need to setup Docker.