SlideShare a Scribd company logo
How to Dockerize Web Application using Docker Compose
Enhancing the application development process in all its phases—building, scaling, shipping, deploying
and running—plays a vital role in today’s competitive IT industry by shortening the time between writing
code and running it. Moreover, the methodologies of DevOps and such container virtualization tech-
nologies as Docker make adopting microservice architecture style with reduced risk and complexity vir-
tually effortless. It is significantly easier to achieve the state of continuous deployment, monitoring and
delivery using Docker.
Before we look into the steps of dockerizing web applications, let us quickly understand some of
Docker’s fundamentals.
Shipping an application into containers is popularly known as “Dockerizing”. Dockerizing offers several
substantial benefits. Here are some ways Docker container technology proves beneficial in day-to-day
software development and testing:
Containers provide an isolated and secure
application platform for running an applica-
tion. They also isolate applications from one
another and the underlying infrastructure,
while providing an added layer of protec-
tion for the application.
Containers can help eliminate a lot of
issues related to configuration, environment
setup, application setup and more. Most of
these issues tend to occur due to configu-
ration differences in development, testing
and production environment stages, and
cause the deployment to fail.
Containers enhance the overall process of
developing, shipping, testing and deploying
the code in different environments, and
make the process very convenient for
various stakeholders. This convenience
creates self-service development and
testing environments, and enhances overall
productivity of software developers and testproductivity of software developers and test
engineers.
Copyright © 2016 Evoke Technologies. All rights reserved 1
Docker Client
Containers Containers
Docker Host
Docker Daemon
Docker build
Docker pull
Docker run
Docker Registry
Tomcat
Ubuntu
CentOS
The diagram below represents a high level architecture of Docker, showing these key Docker building
blocks:
Docker Client
Docker Images
Docker Daemon
Docker Containers
Docker Registry
Copyright © 2016 Evoke Technologies. All rights reserved 2
------------
---------------
----------
---------------
----------------------------
---------------------
-----------------------------------
..............
...
.........
......
....
Here are some of the common commands one would come across while working with Docker images:
One of the biggest advantages of using Docker containers is the creation of self-service development
and test environments, along with the deployment of software packages. To realize this advantage,
one must first figure out distinct ways in which Docker containers representing multiple application
components (web servers, hosting web application and services, databases and so on) can be
orchestrated.
Two approaches for orchestrating multiple containers to create self-service development and testing
environments are:
In this white paper, we will demonstrate how to use Docker compose to orchestrate multiple contain-
ers for creating self-service development/test environments.
The application use case demonstrated in this paper is a Java-based web application that calls a
microservice to retrieve data from MongoDB. A traditional scenario would use dedicated software
development and testing environments that need to be configured as appropriate whenever new
releases are made. This white paper will demonstrate the following, Dockerized method:
We will use Docker compose to orchestrate these containers. The outline below represents required
steps in dockerizing a RESTful web service built using a Spring Boot application.
Docker containers with configuration management tools such as Ansible/Chef/Puppet
Docker compose tool to orchestrate Docker containers
A Java web application will run within a Docker container. This web application will invoke a Spring
Boot microservice.
The Spring Boot microservice will run within another container. This RESTful service preserves its data
in MongoDB.
The MongoDB will run within another container.
Used to build images using
the instructions from
Dockerfile.
Used to fetch the Docker
image from the Docker regis-
tries. In the example shown in
this white paper, the Docker
client interacts with Docker
registries via the Docker
daemon to pull the existing
CentOS image.CentOS image.
Copyright © 2016 Evoke Technologies. All rights reserved 3
Build a Maven-based Spring Boot employee microservice RESTful application. The service will be
exposed over a certain URI (Uniform Resource Identifier). The service saves the data in MongoDB that
runs in another container. Build a WAR file of the above application and place it in a desired location,
specified in the Dockerfile.
To run the application in a Docker container, create a Dockerfile consisting of a set of instructions to
build the image. Once the Dockerfile is created, it could be used to build an image for this service.
Copyright © 2016 Evoke Technologies. All rights reserved 4
Here is a sample Dockerfile representing the image of this service.
Note these important points about the Dockerfile above:
FROM java:8
MAINTAINER apandiri@evoketechnologies.com
VOLUME /tmp
ADD build/libs/ microservice-employee-service.jar app.jar
EXPOSE 8181
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongodb/micros", "-ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongodb/micros", "-
Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
[“java”,"-Dspring.data.mongodb.uri=mongodb://mongodb/micros" ,“-Djava.security.egd=-
file:/dev/./urandom”,“-jar”,“/app.jar”]: execute our fat-jar (urandom is for Tomcat source of entropy)
"-Dspring.data.mongodb.uri=mongodb://mongodb/micros", this environment variable for connecting
to mongodb service which we are going to create using Docker-Compose and the data base name
is micros.
A variable dev/./urandom added to reduce the tomcat start time /app.jar is our jar file to be run.
Djava.security.egd=file:/dev/./urandom for running Spring Boot application
Dockerfile consists of a set of instructions to build an image.
As represented in the diagram above, the build command is used to build an image A, per the
instruction provided in Dockerfile. Additionally, the image A can be pushed in Docker hub using the
push command.
The pushed image A can be fetched using the Docker pull command.
A container can be created from the run command. The above illustration depicts how the run
command was used to create container A from the image A.
Operations that can be performed on container A include start, stop and restart
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-----------------------------------
-----------------------------------
----------------------------------
- - -
Start, Stop, Restart
Commit
Build
Run
Pull
Push
Copyright © 2016 Evoke Technologies. All rights reserved 5
Docker Host
Repository
Image A
Docker Hub
Docker Daemon
Image A
Container ADocker File
Dockerizing a Spring web application would simply mean starting a Spring web application within a
Docker container. One of the key benefits of this approach is that one could build an image once
and then start the Spring web application container as and when required for development and
training purposes. In cases where the Spring web application is part of a bigger enterprise applica-
tion, it brings a lot of benefits in terms of self-service, on-demand development and testing setup,
thereby enhancing the overall productivity of software developers and test engineers.
In this white paper, we have shown how one can build a maven-based Spring web application that
runs on Tomcat. For this, one would be required to build a WAR file and place it in a desired location
(by default, webapps) under Tomcat installation.
$ docker build –t <username>/
<Repositoryname>:<tagname>.
Specifying the <username>/<Repositoryname>
:<tagname> as employee and “.”to look for
Dockerfile in the current directory.
$ docker images
$ docker run –d –name container_name
image_name
$ docker exec -it container_name /bin/bash
$ docker ps
$ docker ps -a
$ docker stop $(docker ps)
$ docker rm $(docker ps -a)
$ docker rmi $(docker images)
FROM tomcat
MAINTAINER apandiri@evoketechnologies.com
ADD build/libs/ microservice-employee-web.war /usr/local/tomcat/webapps/
EXPOSE 8989
CMD ["catalina.sh", "run"]
Copyright © 2016 Evoke Technologies. All rights reserved 6
Dockerizing MongoDB would mean starting a MongoDB within a Docker container. This is accom-
plished using one of the following methods:
In most cases, the first method would suffice. The following commands represent the steps to docker-
ize MongoDB:
Using the Docker compose technique, one could easily start MongoDB within a container and also
link it with other containers. Take a look at the command below:
# Usage: docker pull <user-name>/<repository>
$ docker pull mongo
# Usage: docker build –t <user-name>/<repository> <Dockerfile_location>
$ docker build –t mongo .
# dot “.”defines the Dockerfile in current directory location
# Usage: docker run - p <port mapping> --name <name for container> -d <user-name>/<repository>
$ docker run -p 27017:27017 --name mongo_container -d mongo_image
version: '2'
services:
#Creating MongoDB container from mongo image
mongodb:
image: mongo
container_name: mongodb
expose:expose:
- "27017"
Using Docker compose, one can orchestrate more than one container. The following can be defined
in a YAML file in relation with running multiple containers:
Pulling a MongoDB image directly from the public Docker registry and running a container using
this image.
Building a custom MongoDB image using Dockerfile and then running a container using this
image.
Copyright © 2016 Evoke Technologies. All rights reserved 7
To demonstrate the benefits of Docker compose, the example below specifies three containers,
namely employee, web app and MongoDB containers in a docker-compose.yml file.
Docker Compose file defines services which get started in an interactive mode using “docker-com-
pose up”command. Based on the order the dependencies are declared under “depends_on”in the
docker-compose.yml file, the chosen service, starts first followed by others. In docker-compose file
shown below, the service creation order will be first MongoDB, followed by employee service and
To run all the containers at once interactively and also to find out in which order containers are being
created, use the below command and use –d to start all the containers in the background:
To monitor log output of all the containers, make use of the following command:
$ docker-compose up
$ docker-compose up –d
$ docker-compose logs
In order to stop the running containers, one could use the command given below. Further, it should
be noted that the order of exiting the containers is in the reverse order of their creation.
Specify images,
Specify configurations,
Specify how containers will link,
Specify container dependencies
Specify all ports that needed to be exposed
Copyright © 2016 Evoke Technologies. All rights reserved 8
version: '2'
services:
# Dockerized employee microservice container for exposing services
employee:
build: employee/
dockerfile: Employee_Dockerfile
container_name: employeecontainer_name: employee
ports:
- "8181:8080"
depends_on:
- mongodb
#MongoDB container
mongodb:
image: mongoimage: mongo
container_name: mongodb
expose:
- "27017"
#Dockerized webapp container acting as external web client for consuming restful web services
webapp:
build: webapp/
dockerfile: Webapp_Dockerfiledockerfile: Webapp_Dockerfile
container_name: webapp
ports:
- "8989:8080"
depends_on:
- employee
$ docker-compose stop
$ docker-compose rm --all
To remove all the containers
Copyright © 2016 Evoke Technologies. All rights reserved 9
example quoted above MongoDB and employee containers will be created first and subsequently
webapp is created.
Here MongoDB container is created by consuming the Mongo image published by another in Docker
hub and this will get exposed on port 27017. Navigate to the folder where docker-compose.yml file, all
the respective dockerfiles and JAR files of webapp and RESTful services application have been placed.
Copyright © 2016 Evoke Technologies. All rights reserved
Stay Connected: /EvokeTechnologies | /EvokeUS | /company/evoke-technologies | /+EvoketechnologiesUS
7106 Corporate Way, Dayton, OH - 45459, United States
Sales: +1 (937) 660-4925
Email: sales@evoketechnologies.com
10

More Related Content

What's hot (20)

PPTX
Introduction To Docker
Dr. Syed Hassan Amin
 
PDF
Docker
Chen Chun
 
PDF
Docker by Example - Quiz
CodeOps Technologies LLP
 
PPTX
Learn docker in 90 minutes
Larry Cai
 
PDF
Docker from A to Z, including Swarm and OCCS
Frank Munz
 
PDF
Docker by Example - Basics
CodeOps Technologies LLP
 
PDF
Docker 101 - Intro to Docker
Adrian Otto
 
ODP
Docker - The Linux Container
Balaji Rajan
 
PDF
Docker at Djangocon 2013 | Talk by Ken Cochrane
dotCloud
 
PPTX
Architecting .NET Applications for Docker and Container Based Deployments
Ben Hall
 
PDF
Introducing Docker
Francesco Pantano
 
PDF
Docker 101 @KACST Saudi HPC 2016
Walid Shaari
 
PDF
Real-World Docker: 10 Things We've Learned
RightScale
 
PDF
Docker - From Walking To Running
Giacomo Vacca
 
PDF
Building Reusable Development Environments with Docker
Revelation Technologies
 
PDF
Docker fundamentals
Alper Unal
 
PDF
Docker for developers
andrzejsydor
 
PDF
docker installation and basics
Walid Ashraf
 
PDF
Introduction to Docker and deployment and Azure
Jérôme Petazzoni
 
PDF
The state of the swarm
Mathieu Buffenoir
 
Introduction To Docker
Dr. Syed Hassan Amin
 
Docker
Chen Chun
 
Docker by Example - Quiz
CodeOps Technologies LLP
 
Learn docker in 90 minutes
Larry Cai
 
Docker from A to Z, including Swarm and OCCS
Frank Munz
 
Docker by Example - Basics
CodeOps Technologies LLP
 
Docker 101 - Intro to Docker
Adrian Otto
 
Docker - The Linux Container
Balaji Rajan
 
Docker at Djangocon 2013 | Talk by Ken Cochrane
dotCloud
 
Architecting .NET Applications for Docker and Container Based Deployments
Ben Hall
 
Introducing Docker
Francesco Pantano
 
Docker 101 @KACST Saudi HPC 2016
Walid Shaari
 
Real-World Docker: 10 Things We've Learned
RightScale
 
Docker - From Walking To Running
Giacomo Vacca
 
Building Reusable Development Environments with Docker
Revelation Technologies
 
Docker fundamentals
Alper Unal
 
Docker for developers
andrzejsydor
 
docker installation and basics
Walid Ashraf
 
Introduction to Docker and deployment and Azure
Jérôme Petazzoni
 
The state of the swarm
Mathieu Buffenoir
 

Viewers also liked (20)

PDF
Running the Oracle SOA Suite Environment in a Docker Container
Guido Schmutz
 
PPTX
Microservices with Docker
Venkata Naga Ravi
 
PPTX
Mule soft esb – data validation best practices
alfa
 
PDF
Dockerized tests with dockerized jenkins
Fernando Valverde
 
PPTX
IBM Container Service Overview
Kyle Brown
 
PDF
Integration Testing with Docker Containers with DockerCompose
Mike Holdsworth
 
PDF
DEPLOYING A DOCKERIZED DISTRIBUTED APPLICATION IN MESOS
Julia Mateo
 
PPTX
Dockerize it all
Puneet Behl
 
PPTX
SDLC Using Docker for Fun and Profit
dantheelder
 
PPTX
Micro services and Containers
Richard Harvey
 
PPTX
Monitoring docker container and dockerized applications
Ananth Padmanabhan
 
PPT
Docker Practical Use Cases
Alexei Yuzhakov
 
PDF
Dockerize All The Things
Chris Tankersley
 
PDF
Dockerize WordPress on Mac/Windows
Kite Koga
 
PDF
Modern DevOps with Docker
Shippable
 
PPTX
Container as a Service with Docker
Patrick Chanezon
 
PDF
I tried to dockerize my app but I had to PaaS
Jorge Morales
 
PPTX
Immutable infrastructure with Docker and EC2
dotCloud
 
PDF
How Docker Fits into DevOps Ecosystem
Edureka!
 
PDF
CI/CD with Docker, DC/OS, and Jenkins
Karl Isenberg
 
Running the Oracle SOA Suite Environment in a Docker Container
Guido Schmutz
 
Microservices with Docker
Venkata Naga Ravi
 
Mule soft esb – data validation best practices
alfa
 
Dockerized tests with dockerized jenkins
Fernando Valverde
 
IBM Container Service Overview
Kyle Brown
 
Integration Testing with Docker Containers with DockerCompose
Mike Holdsworth
 
DEPLOYING A DOCKERIZED DISTRIBUTED APPLICATION IN MESOS
Julia Mateo
 
Dockerize it all
Puneet Behl
 
SDLC Using Docker for Fun and Profit
dantheelder
 
Micro services and Containers
Richard Harvey
 
Monitoring docker container and dockerized applications
Ananth Padmanabhan
 
Docker Practical Use Cases
Alexei Yuzhakov
 
Dockerize All The Things
Chris Tankersley
 
Dockerize WordPress on Mac/Windows
Kite Koga
 
Modern DevOps with Docker
Shippable
 
Container as a Service with Docker
Patrick Chanezon
 
I tried to dockerize my app but I had to PaaS
Jorge Morales
 
Immutable infrastructure with Docker and EC2
dotCloud
 
How Docker Fits into DevOps Ecosystem
Edureka!
 
CI/CD with Docker, DC/OS, and Jenkins
Karl Isenberg
 
Ad

Similar to How to Dockerize Web Application using Docker Compose (20)

PDF
Why everyone is excited about Docker (and you should too...) - Carlo Bonamic...
Codemotion
 
PDF
codemotion-docker-2014
Carlo Bonamico
 
PDF
Docker: A New Way to Turbocharging Your Apps Development
msyukor
 
PDF
Faster and Easier Software Development using Docker Platform
msyukor
 
PPTX
Introduction to docker
Frederik Mogensen
 
PDF
Docker basic
Somenath Ghosh
 
PDF
Docker From Scratch
Giacomo Vacca
 
PPTX
Developer workflow with docker
Wyn B. Van Devanter
 
PDF
Docker for (Java) Developers
Rafael Benevides
 
PDF
Introduction to Docker - VIT Campus
Ajeet Singh Raina
 
PDF
Docker workshop 0507 Taichung
Paul Chao
 
PDF
手把手帶你學 Docker 入門篇
Philip Zheng
 
PDF
Introduction to Docker - Vellore Institute of Technology
Ajeet Singh Raina
 
PDF
手把手帶你學Docker 03042017
Paul Chao
 
PDF
Killer Docker Workflows for Development
Chris Tankersley
 
PPTX
Java developer intro to environment management with vagrant puppet and docker
Getting value from IoT, Integration and Data Analytics
 
PPTX
Dockerizing your java development environment
Buhake Sindi
 
PDF
docker.pdf
vivekpatnaik8
 
PPTX
Docker 101 describing basic docker usage
ZiyanMaraikar1
 
PPTX
Introduction to automated environment management with Docker Containers - for...
Lucas Jellema
 
Why everyone is excited about Docker (and you should too...) - Carlo Bonamic...
Codemotion
 
codemotion-docker-2014
Carlo Bonamico
 
Docker: A New Way to Turbocharging Your Apps Development
msyukor
 
Faster and Easier Software Development using Docker Platform
msyukor
 
Introduction to docker
Frederik Mogensen
 
Docker basic
Somenath Ghosh
 
Docker From Scratch
Giacomo Vacca
 
Developer workflow with docker
Wyn B. Van Devanter
 
Docker for (Java) Developers
Rafael Benevides
 
Introduction to Docker - VIT Campus
Ajeet Singh Raina
 
Docker workshop 0507 Taichung
Paul Chao
 
手把手帶你學 Docker 入門篇
Philip Zheng
 
Introduction to Docker - Vellore Institute of Technology
Ajeet Singh Raina
 
手把手帶你學Docker 03042017
Paul Chao
 
Killer Docker Workflows for Development
Chris Tankersley
 
Java developer intro to environment management with vagrant puppet and docker
Getting value from IoT, Integration and Data Analytics
 
Dockerizing your java development environment
Buhake Sindi
 
docker.pdf
vivekpatnaik8
 
Docker 101 describing basic docker usage
ZiyanMaraikar1
 
Introduction to automated environment management with Docker Containers - for...
Lucas Jellema
 
Ad

Recently uploaded (20)

PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 

How to Dockerize Web Application using Docker Compose

  • 2. Enhancing the application development process in all its phases—building, scaling, shipping, deploying and running—plays a vital role in today’s competitive IT industry by shortening the time between writing code and running it. Moreover, the methodologies of DevOps and such container virtualization tech- nologies as Docker make adopting microservice architecture style with reduced risk and complexity vir- tually effortless. It is significantly easier to achieve the state of continuous deployment, monitoring and delivery using Docker. Before we look into the steps of dockerizing web applications, let us quickly understand some of Docker’s fundamentals. Shipping an application into containers is popularly known as “Dockerizing”. Dockerizing offers several substantial benefits. Here are some ways Docker container technology proves beneficial in day-to-day software development and testing: Containers provide an isolated and secure application platform for running an applica- tion. They also isolate applications from one another and the underlying infrastructure, while providing an added layer of protec- tion for the application. Containers can help eliminate a lot of issues related to configuration, environment setup, application setup and more. Most of these issues tend to occur due to configu- ration differences in development, testing and production environment stages, and cause the deployment to fail. Containers enhance the overall process of developing, shipping, testing and deploying the code in different environments, and make the process very convenient for various stakeholders. This convenience creates self-service development and testing environments, and enhances overall productivity of software developers and testproductivity of software developers and test engineers. Copyright © 2016 Evoke Technologies. All rights reserved 1
  • 3. Docker Client Containers Containers Docker Host Docker Daemon Docker build Docker pull Docker run Docker Registry Tomcat Ubuntu CentOS The diagram below represents a high level architecture of Docker, showing these key Docker building blocks: Docker Client Docker Images Docker Daemon Docker Containers Docker Registry Copyright © 2016 Evoke Technologies. All rights reserved 2 ------------ --------------- ---------- --------------- ---------------------------- --------------------- ----------------------------------- .............. ... ......... ...... ....
  • 4. Here are some of the common commands one would come across while working with Docker images: One of the biggest advantages of using Docker containers is the creation of self-service development and test environments, along with the deployment of software packages. To realize this advantage, one must first figure out distinct ways in which Docker containers representing multiple application components (web servers, hosting web application and services, databases and so on) can be orchestrated. Two approaches for orchestrating multiple containers to create self-service development and testing environments are: In this white paper, we will demonstrate how to use Docker compose to orchestrate multiple contain- ers for creating self-service development/test environments. The application use case demonstrated in this paper is a Java-based web application that calls a microservice to retrieve data from MongoDB. A traditional scenario would use dedicated software development and testing environments that need to be configured as appropriate whenever new releases are made. This white paper will demonstrate the following, Dockerized method: We will use Docker compose to orchestrate these containers. The outline below represents required steps in dockerizing a RESTful web service built using a Spring Boot application. Docker containers with configuration management tools such as Ansible/Chef/Puppet Docker compose tool to orchestrate Docker containers A Java web application will run within a Docker container. This web application will invoke a Spring Boot microservice. The Spring Boot microservice will run within another container. This RESTful service preserves its data in MongoDB. The MongoDB will run within another container. Used to build images using the instructions from Dockerfile. Used to fetch the Docker image from the Docker regis- tries. In the example shown in this white paper, the Docker client interacts with Docker registries via the Docker daemon to pull the existing CentOS image.CentOS image. Copyright © 2016 Evoke Technologies. All rights reserved 3
  • 5. Build a Maven-based Spring Boot employee microservice RESTful application. The service will be exposed over a certain URI (Uniform Resource Identifier). The service saves the data in MongoDB that runs in another container. Build a WAR file of the above application and place it in a desired location, specified in the Dockerfile. To run the application in a Docker container, create a Dockerfile consisting of a set of instructions to build the image. Once the Dockerfile is created, it could be used to build an image for this service. Copyright © 2016 Evoke Technologies. All rights reserved 4 Here is a sample Dockerfile representing the image of this service. Note these important points about the Dockerfile above: FROM java:8 MAINTAINER [email protected] VOLUME /tmp ADD build/libs/ microservice-employee-service.jar app.jar EXPOSE 8181 RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongodb/micros", "-ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongodb/micros", "- Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  • 6. [“java”,"-Dspring.data.mongodb.uri=mongodb://mongodb/micros" ,“-Djava.security.egd=- file:/dev/./urandom”,“-jar”,“/app.jar”]: execute our fat-jar (urandom is for Tomcat source of entropy) "-Dspring.data.mongodb.uri=mongodb://mongodb/micros", this environment variable for connecting to mongodb service which we are going to create using Docker-Compose and the data base name is micros. A variable dev/./urandom added to reduce the tomcat start time /app.jar is our jar file to be run. Djava.security.egd=file:/dev/./urandom for running Spring Boot application Dockerfile consists of a set of instructions to build an image. As represented in the diagram above, the build command is used to build an image A, per the instruction provided in Dockerfile. Additionally, the image A can be pushed in Docker hub using the push command. The pushed image A can be fetched using the Docker pull command. A container can be created from the run command. The above illustration depicts how the run command was used to create container A from the image A. Operations that can be performed on container A include start, stop and restart - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ----------------------------------- ----------------------------------- ---------------------------------- - - - Start, Stop, Restart Commit Build Run Pull Push Copyright © 2016 Evoke Technologies. All rights reserved 5 Docker Host Repository Image A Docker Hub Docker Daemon Image A Container ADocker File
  • 7. Dockerizing a Spring web application would simply mean starting a Spring web application within a Docker container. One of the key benefits of this approach is that one could build an image once and then start the Spring web application container as and when required for development and training purposes. In cases where the Spring web application is part of a bigger enterprise applica- tion, it brings a lot of benefits in terms of self-service, on-demand development and testing setup, thereby enhancing the overall productivity of software developers and test engineers. In this white paper, we have shown how one can build a maven-based Spring web application that runs on Tomcat. For this, one would be required to build a WAR file and place it in a desired location (by default, webapps) under Tomcat installation. $ docker build –t <username>/ <Repositoryname>:<tagname>. Specifying the <username>/<Repositoryname> :<tagname> as employee and “.”to look for Dockerfile in the current directory. $ docker images $ docker run –d –name container_name image_name $ docker exec -it container_name /bin/bash $ docker ps $ docker ps -a $ docker stop $(docker ps) $ docker rm $(docker ps -a) $ docker rmi $(docker images) FROM tomcat MAINTAINER [email protected] ADD build/libs/ microservice-employee-web.war /usr/local/tomcat/webapps/ EXPOSE 8989 CMD ["catalina.sh", "run"] Copyright © 2016 Evoke Technologies. All rights reserved 6
  • 8. Dockerizing MongoDB would mean starting a MongoDB within a Docker container. This is accom- plished using one of the following methods: In most cases, the first method would suffice. The following commands represent the steps to docker- ize MongoDB: Using the Docker compose technique, one could easily start MongoDB within a container and also link it with other containers. Take a look at the command below: # Usage: docker pull <user-name>/<repository> $ docker pull mongo # Usage: docker build –t <user-name>/<repository> <Dockerfile_location> $ docker build –t mongo . # dot “.”defines the Dockerfile in current directory location # Usage: docker run - p <port mapping> --name <name for container> -d <user-name>/<repository> $ docker run -p 27017:27017 --name mongo_container -d mongo_image version: '2' services: #Creating MongoDB container from mongo image mongodb: image: mongo container_name: mongodb expose:expose: - "27017" Using Docker compose, one can orchestrate more than one container. The following can be defined in a YAML file in relation with running multiple containers: Pulling a MongoDB image directly from the public Docker registry and running a container using this image. Building a custom MongoDB image using Dockerfile and then running a container using this image. Copyright © 2016 Evoke Technologies. All rights reserved 7
  • 9. To demonstrate the benefits of Docker compose, the example below specifies three containers, namely employee, web app and MongoDB containers in a docker-compose.yml file. Docker Compose file defines services which get started in an interactive mode using “docker-com- pose up”command. Based on the order the dependencies are declared under “depends_on”in the docker-compose.yml file, the chosen service, starts first followed by others. In docker-compose file shown below, the service creation order will be first MongoDB, followed by employee service and To run all the containers at once interactively and also to find out in which order containers are being created, use the below command and use –d to start all the containers in the background: To monitor log output of all the containers, make use of the following command: $ docker-compose up $ docker-compose up –d $ docker-compose logs In order to stop the running containers, one could use the command given below. Further, it should be noted that the order of exiting the containers is in the reverse order of their creation. Specify images, Specify configurations, Specify how containers will link, Specify container dependencies Specify all ports that needed to be exposed Copyright © 2016 Evoke Technologies. All rights reserved 8
  • 10. version: '2' services: # Dockerized employee microservice container for exposing services employee: build: employee/ dockerfile: Employee_Dockerfile container_name: employeecontainer_name: employee ports: - "8181:8080" depends_on: - mongodb #MongoDB container mongodb: image: mongoimage: mongo container_name: mongodb expose: - "27017" #Dockerized webapp container acting as external web client for consuming restful web services webapp: build: webapp/ dockerfile: Webapp_Dockerfiledockerfile: Webapp_Dockerfile container_name: webapp ports: - "8989:8080" depends_on: - employee $ docker-compose stop $ docker-compose rm --all To remove all the containers Copyright © 2016 Evoke Technologies. All rights reserved 9
  • 11. example quoted above MongoDB and employee containers will be created first and subsequently webapp is created. Here MongoDB container is created by consuming the Mongo image published by another in Docker hub and this will get exposed on port 27017. Navigate to the folder where docker-compose.yml file, all the respective dockerfiles and JAR files of webapp and RESTful services application have been placed. Copyright © 2016 Evoke Technologies. All rights reserved Stay Connected: /EvokeTechnologies | /EvokeUS | /company/evoke-technologies | /+EvoketechnologiesUS 7106 Corporate Way, Dayton, OH - 45459, United States Sales: +1 (937) 660-4925 Email: [email protected] 10