Microservices PoC: Docker Calculator build with docker-compose

Danny Bryant
Aug 26, 2017 · 6 min read
Docker-Compose
Docker-Compose

Hopefully you have been following my Docker exploits allowing me to share as I go along. Recently I posted a part two on my Microservices PoC describing a simple calculator. The solution currently instructs to you to execute four (4) docker build commands to create the individual containers for the calculator function. When I did this, I felt there had to be a simpler way. To my delight, docker-compose was the answer. In order to use Docker Compose on my Ubuntu system, I needed to download and install it. To keep it simple, I’ll include the steps here:

Install Docker Compose

Execute the following at your command prompt to install the most current version at the time of this post:

dbryant@asus: sudo curl -L https://github.com/docker/compose/releases/download/1.15.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

dbryant@asus: sudo chmod +x /usr/local/bin/docker-compose

dbryant@asus: docker-compose --version
docker-compose version 1.15.0, build e12f3b9

With docker-compose installed, we can now use the docker-compose.yml file to combine several steps into one. In this case the four docker build commands along with the subsequent docker run commands will be handled with one docker-compose.yml file.

Create docker-compose.yml file

In your favorite editor, vi perhaps, create the docker-compose.yml file in the directory with your current “Dockerfile” files.

dbryant@asus: ls
add2.js docker-compose.yml Dockerfile_d Dockerfile_s
divide2.js Dockerfile Dockerfile_m multiply2.js

dbryant@asus: cat docker-compose.yml
add:
build: .
dockerfile: Dockerfile
ports:
- "3000:3000"
subtract:
build: .
dockerfile: Dockerfile_s
ports:
- "3001:3001"
multiply:
build: .
dockerfile: Dockerfile_m
ports:
- "3002:3002"
divide:
build: .
dockerfile: Dockerfile_d
ports:
- "3003:3003"

I have found the docker-compose.yml file to extremely finicky with spacing, tabs, and line feeds so it make take a few times to get the file structure correct.

Once done, we can now use the docker-compose up command to perform the build and run of the four images. In essence, the docker-compose.yml file uses the Dockerfiles identified to build the images and in this situation, exposes the math functions on the various ports 3000, 3001, 3002, 3003.

Run docker-compose up

dbryant@asus:~/development/github.com/Docker/Docker_Calc$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dbryant@asus:~/development/github.com/Docker/Docker_Calc$
dbryant@asus:~/development/github.com/Docker/Docker_Calc$ docker-compose up
Building multiply
Step 1/8 : FROM oraclelinux:7-slim
7-slim: Pulling from library/oraclelinux
3152c71f8d80: Already exists
Digest: sha256:e464042b724d41350fb3ac2c2f84bd9d28d98302c9ebe66048a5367682e5fad2
Status: Downloaded newer image for oraclelinux:7-slim
---> c0feb50f7527
Step 2/8 : ADD node-v6* /opt/
---> e3bc3a113c3d
Removing intermediate container f4f355ae55b5
Step 3/8 : ENV PATH /opt/node-v6.11.1-linux-x64/bin:$PATH
---> Running in 3e2731e6ca82
---> 6cc0e7aaf81b
Removing intermediate container 3e2731e6ca82
Step 4/8 : ENV NODE_PATH /opt/node-v6.11.1-linux-x64/lib/node_modules/
---> Running in 7029d5a7d4ed
---> b370b425a9e7
Removing intermediate container 7029d5a7d4ed
Step 5/8 : RUN npm install -g express
---> Running in 5c58fa043b20
/opt/node-v6.11.1-linux-x64/lib
`-- express@4.15.4
+-- accepts@1.3.4
| +-- mime-types@2.1.16
| | `-- mime-db@1.29.0
| `-- negotiator@0.6.1
+-- array-flatten@1.1.1
+-- content-disposition@0.5.2
+-- content-type@1.0.2
+-- cookie@0.3.1
+-- cookie-signature@1.0.6
+-- debug@2.6.8
| `-- ms@2.0.0
+-- depd@1.1.1
+-- encodeurl@1.0.1
+-- escape-html@1.0.3
+-- etag@1.8.0
+-- finalhandler@1.0.4
| `-- unpipe@1.0.0
+-- fresh@0.5.0
+-- merge-descriptors@1.0.1
+-- methods@1.1.2
+-- on-finished@2.3.0
| `-- ee-first@1.1.1
+-- parseurl@1.3.1
+-- path-to-regexp@0.1.7
+-- proxy-addr@1.1.5
| +-- forwarded@0.1.0
| `-- ipaddr.js@1.4.0
+-- qs@6.5.0
+-- range-parser@1.2.0
+-- send@0.15.4
| +-- destroy@1.0.4
| +-- http-errors@1.6.2
| | `-- inherits@2.0.3
| `-- mime@1.3.4
+-- serve-static@1.12.4
+-- setprototypeof@1.0.3
+-- statuses@1.3.1
+-- type-is@1.6.15
| `-- media-typer@0.3.0
+-- utils-merge@1.0.0
`-- vary@1.1.1

---> dbc77ff00d97
Removing intermediate container 5c58fa043b20
Step 6/8 : ADD multiply2.js .
---> 2426ecb359c2
Removing intermediate container 136f55d5a34d
Step 7/8 : EXPOSE 3002
---> Running in f1088f80a7a6
---> c51ebc6c96f4
Removing intermediate container f1088f80a7a6
Step 8/8 : CMD node multiply2.js
---> Running in ab9ba3505470
---> a9bd02502eb9
Removing intermediate container ab9ba3505470
Successfully built a9bd02502eb9
Successfully tagged dockercalc_multiply:latest
WARNING: Image for service multiply was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building add
Step 1/7 : FROM oraclelinux:7-slim
---> c0feb50f7527
Step 2/7 : ADD node-v6* /opt/
---> Using cache
---> e3bc3a113c3d
Step 3/7 : ENV PATH /opt/node-v6.11.1-linux-x64/bin:$PATH
---> Using cache
---> 6cc0e7aaf81b
Step 4/7 : ENV NODE_PATH /opt/node-v6.11.1-linux-x64/lib/node_modules/
---> Using cache
---> b370b425a9e7
Step 5/7 : RUN npm install -g express
---> Using cache
---> dbc77ff00d97
Step 6/7 : ADD add2.js .
---> 4918e0593ae4
Removing intermediate container f4a9e564b16a
Step 7/7 : CMD node add2.js
---> Running in ffcd4de5bd01
---> e8af879c151a
Removing intermediate container ffcd4de5bd01
Successfully built e8af879c151a
Successfully tagged dockercalc_add:latest
WARNING: Image for service add was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building subtract
Step 1/8 : FROM oraclelinux:7-slim
---> c0feb50f7527
Step 2/8 : ADD node-v6* /opt/
---> Using cache
---> e3bc3a113c3d
Step 3/8 : ENV PATH /opt/node-v6.11.1-linux-x64/bin:$PATH
---> Using cache
---> 6cc0e7aaf81b
Step 4/8 : ENV NODE_PATH /opt/node-v6.11.1-linux-x64/lib/node_modules/
---> Using cache
---> b370b425a9e7
Step 5/8 : RUN npm install -g express
---> Using cache
---> dbc77ff00d97
Step 6/8 : ADD subtract2.js .
---> b87fd184c682
Removing intermediate container 230a1bd82ed5
Step 7/8 : EXPOSE 3001
---> Running in a3ce90e09053
---> 4251062554a8
Removing intermediate container a3ce90e09053
Step 8/8 : CMD node subtract2.js
---> Running in 992df846b962
---> 2e16507c16a6
Removing intermediate container 992df846b962
Successfully built 2e16507c16a6
Successfully tagged dockercalc_subtract:latest
WARNING: Image for service subtract was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building divide
Step 1/8 : FROM oraclelinux:7-slim
---> c0feb50f7527
Step 2/8 : ADD node-v6* /opt/
---> Using cache
---> e3bc3a113c3d
Step 3/8 : ENV PATH /opt/node-v6.11.1-linux-x64/bin:$PATH
---> Using cache
---> 6cc0e7aaf81b
Step 4/8 : ENV NODE_PATH /opt/node-v6.11.1-linux-x64/lib/node_modules/
---> Using cache
---> b370b425a9e7
Step 5/8 : RUN npm install -g express
---> Using cache
---> dbc77ff00d97
Step 6/8 : ADD divide2.js .
---> a614ae005246
Removing intermediate container 129ab6d193ed
Step 7/8 : EXPOSE 3003
---> Running in 407631099de2
---> 96c365ac8705
Removing intermediate container 407631099de2
Step 8/8 : CMD node divide2.js
---> Running in e77f1c4a0276
---> 7c528b7728f1
Removing intermediate container e77f1c4a0276
Successfully built 7c528b7728f1
Successfully tagged dockercalc_divide:latest
WARNING: Image for service divide was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating dockercalc_divide_1 ...
Creating dockercalc_multiply_1 ...
Creating dockercalc_add_1 ...
Creating dockercalc_subtract_1 ...
Creating dockercalc_divide_1
Creating dockercalc_add_1
Creating dockercalc_subtract_1
Creating dockercalc_multiply_1 ... done
Attaching to dockercalc_divide_1, dockercalc_add_1, dockercalc_subtract_1, dockercalc_multiply_1
divide_1 | Division service listening on port 3003!
add_1 | Addition service listening on port 3000!
subtract_1 | Subtraction service listening on port 3001!
multiply_1 | Multiplication service listening on port 3002!

At the end of this process, you can see that all four “microservices” are up and running. We can verify a few things.

dbryant@asus:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockercalc_divide latest 7c528b7728f1 11 minutes ago 163MB
dockercalc_subtract latest 2e16507c16a6 11 minutes ago 163MB
dockercalc_add latest e8af879c151a 11 minutes ago 163MB
dockercalc_multiply latest a9bd02502eb9 11 minutes ago 163MB
oraclelinux 7-slim c0feb50f7527 2 weeks ago 118MB
dbryant@asus:~$


dbryant@asus:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f8e1a031ed70 dockercalc_subtract "node subtract2.js" 12 minutes ago Up 12 minutes 0.0.0.0:3001->3001/tcp dockercalc_subtract_1
47e8bb757446 dockercalc_multiply "node multiply2.js" 12 minutes ago Up 12 minutes 0.0.0.0:3002->3002/tcp dockercalc_multiply_1
8fb7d791bdbd dockercalc_add "node add2.js" 12 minutes ago Up 12 minutes 0.0.0.0:3000->3000/tcp dockercalc_add_1
1d46aa08eb13 dockercalc_divide "node divide2.js" 12 minutes ago Up 12 minutes 0.0.0.0:3003->3003/tcp dockercalc_divide_1


dbryant@asus:~/development/github.com/Docker/Docker_Calc$ docker-compose images
Container Repository Tag Image Id Size
----------------------------------------------------------------------------
dockercalc_add_1 dockercalc_add latest e8af879c151a 156 MB
dockercalc_divide_1 dockercalc_divide latest 7c528b7728f1 156 MB
dockercalc_multiply_1 dockercalc_multiply latest a9bd02502eb9 156 MB
dockercalc_subtract_1 dockercalc_subtract latest 2e16507c16a6 156 MB

We can even test this with the curl command. The %20 represents a space between the two operands.

dbryant@asus:~$ curl http://localhost:3000/add/?id=4%205 Your answer: 9 
dbryant@asus:~$ curl http://localhost:3001/subtract/?id=4%205 The answer is: -1
dbryant@asus:~$ curl http://localhost:3002/multiply/?id=4%205 The answer is: 20
dbryant@asus:~$ curl http://localhost:3003/divide/?id=4%205 The answer is: 0.8

Once you are ready to stop these, you simply press <ctrl-c>:

^CGracefully stopping... (press Ctrl+C again to force)
Killing dockercalc_subtract_1 ... done
Killing dockercalc_multiply_1 ... done
Killing dockercalc_add_1 ... done
Killing dockercalc_divide_1 ... done

There we have it.

Enjoy!

dbaontap
dbaontap

Originally published at dbaonTap.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store