How To Dockerize Web Application Using Docker Compose
How To Dockerize Web Application Using Docker Compose
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
Docker Host
---------------------
....
Docker pull
----------
......
Docker build
Docker Daemon
...
.........
Docker Client
-----------------------------------
---------------
..............
---------------
Docker Registry
Tomcat
Ubuntu
------------
Docker run
----------------------------
Containers
CentOS
Containers
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:
Docker containers with configuration management tools such as Ansible/Chef/Puppet
Docker compose tool to orchestrate Docker containers
In this white paper, we will demonstrate how to use Docker compose to orchestrate multiple containers 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:
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.
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.
Copyright 2016 Evoke Technologies. All rights reserved
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.
Here is a sample Dockerfile representing the image of this service.
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", "Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Note these important points about the Dockerfile above:
[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.
Djava.security.egd=file:/dev/./urandom for running Spring Boot application
A variable dev/./urandom added to reduce the tomcat start time /app.jar is our jar file to be run.
Docker File
----------------------------------
Build
Docker Host
Docker Daemon
Image A
Commit
Container A
Run
-----------------------------------
- - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Docker Hub
Repository
Pull
Push
Image A
-----------------------------------
$ docker build
t <username>/
<Repositoryname>:<tagname>.
Specifying the <username>/<Repositoryname>
$ docker ps
$ docker ps -a
$ docker images
$ docker rm $(docker ps -a)
$ docker run
d name container_name
image_name
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 application, 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.
FROM tomcat
MAINTAINER apandiri@evoketechnologies.com
ADD build/libs/ microservice-employee-web.war /usr/local/tomcat/webapps/
EXPOSE 8989
CMD ["catalina.sh", "run"]
Dockerizing MongoDB would mean starting a MongoDB within a Docker container. This is accomplished using one of the following methods:
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.
In most cases, the first method would suffice. The following commands represent the steps to dockerize MongoDB:
# 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
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:
version: '2'
services:
#Creating MongoDB container from mongo image
mongodb:
image: mongo
container_name: mongodb
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:
Copyright 2016 Evoke Technologies. All rights reserved
Specify images,
Specify configurations,
Specify how containers will link,
Specify container dependencies
Specify all ports that needed to be exposed
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-compose upcommand. Based on the order the dependencies are declared under depends_onin 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:
$ docker-compose up
$ docker-compose up d
To monitor log output of all the containers, make use of the following command:
$ 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.
Copyright 2016 Evoke Technologies. All rights reserved
$ docker-compose stop
version: '2'
services:
# Dockerized employee microservice container for exposing services
employee:
build: employee/
dockerfile: Employee_Dockerfile
container_name: employee
ports:
- "8181:8080"
depends_on:
- mongodb
#MongoDB container
mongodb:
image: mongo
container_name: mongodb
expose:
- "27017"
#Dockerized webapp container acting as external web client for consuming restful web services
webapp:
build: webapp/
dockerfile: Webapp_Dockerfile
container_name: webapp
ports:
- "8989:8080"
depends_on:
- employee
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.
Stay Connected:
/EvokeTechnologies |
/EvokeUS |
/company/evoke-technologies |
/+EvoketechnologiesUS
10