This example will explore building a simple container that has one specific function. I’ll then extend this concept to a bit to add additional containers in an effort to build a basic calculator that can add, subtract, divide, and multiply. The current rendition will execute from the command line. Given enough time, we can REST enable these calls for some more interesting concepts.
Assumptions:
- Docker is installed
Let’s get started.
Create a directory to maintain your Dockerfiles
Danny: > pwd /Users/maynard.d.bryant.jr/DockerStuff/maint Danny: > mkdir -p Docker_Calc
Next, we will create 4 Dockerfiles. One for each of our calculator operations
Danny: > cat Dockerfile FROM oraclelinux:7-slim ADD adder.py . Danny: > cat Dockerfile_m FROM oraclelinux:7-slim ADD mult.py . Danny: > cat Dockerfile_d FROM oraclelinux:7-slim ADD div.py . Danny: > cat Dockerfile_s FROM oraclelinux:7-slim ADD subtr.py . Danny: >
You can call these files anything you want. I chose the following names
Danny: > ls D* Dockerfile Dockerfile_d Dockerfile_m Dockerfile_s Danny: >
For the code that will perform the arithmetic operations, I chose PYTHON. Again, you can use any language you want, but PYTHON is included in the Oracle Linux Slim distro I am using. If you choose to use a different language you will need to add that in the Dockerfiles. You can check this blog post out for an example of how to include NodeJS.
The code is quite simple, so let’s go ahead and create the files
Danny: > cat adder.py import sys a, b = sys.argv[1:3] summ = int(a) + int(b) print "sum is", summ Danny: > cat div.py from __future__ import division import sys a, b = sys.argv[1:3] div = float(a) / float(b) print "answer is", div Danny: > cat mult.py import sys a, b = sys.argv[1:3] mult = int(a) * int(b) print "product is", mult Danny: > cat subtr.py import sys a, b = sys.argv[1:3] difff = int(a) - int(b) print "difference is", difff Danny: >
Once we have created these file your directory should look like this
Danny: > ls Dockerfile Dockerfile_m adder.py mult.py Dockerfile_d Dockerfile_s div.py subtr.py Danny: >
Finally, use the docker build command to create the containers
Danny: > docker build -f Dockerfile_s -t subtr . Sending build context to Docker daemon 9.216kB Step 1/2 : FROM oraclelinux:7-slim ---> 08a01cc7be97 Step 2/2 : ADD subtr.py . ---> Using cache ---> 5f94762942b6 Successfully built 5f94762942b6 Successfully tagged subtr:latest Danny: > docker build -f Dockerfile_m -t mult . Sending build context to Docker daemon 9.216kB Step 1/2 : FROM oraclelinux:7-slim ---> 08a01cc7be97 Step 2/2 : ADD mult.py . ---> Using cache ---> 3977f121b17a Successfully built 3977f121b17a Successfully tagged mult:latest Danny: > docker build -f Dockerfile_d -t div . Sending build context to Docker daemon 9.216kB Step 1/2 : FROM oraclelinux:7-slim ---> 08a01cc7be97 Step 2/2 : ADD div.py . ---> Using cache ---> 9f0259568b19 Successfully built 9f0259568b19 Successfully tagged div:latest Danny: > docker build -t adder . Sending build context to Docker daemon 9.216kB Step 1/2 : FROM oraclelinux:7-slim ---> 08a01cc7be97 Step 2/2 : ADD adder.py . ---> Using cache ---> 3dd057174165 Successfully built 3dd057174165 Successfully tagged adder:latest Danny: >
Assuming no errors, issue a docker images command, and you will something similar to what I have below.
Danny: > docker images REPOSITORY TAG IMAGE ID CREATED SIZE div latest 9f0259568b19 3 days ago 114MB mult latest 3977f121b17a 3 days ago 114MB subtr latest 5f94762942b6 4 days ago 114MB adder latest 3dd057174165 4 days ago 114MB
What we now have are four containers each providing a specific service: add, subtract, divide, and multiply. Looking at the PYTHON code, each one expects 2 arguments. Let’s try them out and see what happens.
Right now we will need to execute the docker run command. Let’s do that now.
Danny: > docker run -ti adder python adder.py 1 2 sum is 3 Danny: > docker run -ti div python div.py 2 3 answer is 0.666666666667 Danny: > docker run -ti mult python mult.py 5 6 product is 30 Danny: > docker run -ti subtr python subtr.py 2 3 difference is -1 Danny: >
Not quite the most elegant way, but it works. I took the extra step to create an alias for each of the operations so now I can do this:
alias add='docker run -ti adder python adder.py' alias div='docker run -ti div python div.py' alias mult='docker run -ti mult python mult.py' alias subtr='docker run -ti subtr python subtr.py' Danny: > add 1 2 sum is 3 Danny: > div 2 3 answer is 0.666666666667 Danny: > mult 5 6 product is 30 Danny: > subtr 2 3 difference is -1 Danny: >
Now you can build any front end application and use the containers perform the calculations. Obviously next would be to add some error checking and data validation in the PYTHON code to prevent invalid data from being introduced as arguments.
I’ll revisit this post at a later time to expand on this concept.
1 Comment