Microservice architecture resembles a Service Oriented architecture in the part that both rely on cohesive
loosely coupled services, strung together to provide a solution. Beyond this similarity, the common nature
of the architecture seems to end. Microservice architetcure consists of completely decoupled services orchestrated
with each other via REST+HTTP API interfaces. These services can each be running in its own environment including
different programming languages. Each service can have a different deployment/management cycle while keeping the
final solution consistent.
Docker is a technology that is naturally suited for building an Application with a Microservice architecture.
You can visualize Docker as a wafer thin Linux VM that can host multiple containers without a tight dependency
on the host Operating System. Compared to a regular VM, it is light weight and more manageable. You can have
Microservices loaded into individual containers each completely isolated in environment from each other.
Besides Isolation, Docker also provides a consistent environment between code movement from Development -> QA
-> Production. Developers can have development time Docker containers that run on minimal hardware resources
and have the same code deployed consistently in maximized Production environments including Cloud and PAAS/IAAS
infrastructures. Same code, different scale!!!
With that in mind, I would like to cover the implementation details associated with developing Java Microservices
in a Docker environment.
This step involves developing the Java Microservice. Keeping the scope of the blog in mind, this microservice can be downloaded and run with the following instructions
|Git Clone||git clone https://github.com/sohishah/hello-microservice.git|
|Build the project in Maven||mvn clean package|
|Run the Microservice||java -jar target/hello-microservice-1.0-SNAPSHOT.jar server hello-microservice.yaml|
|See the Microservice in action||http://localhost:9000/java/microservice|
Implementation details about the Microservice can be studied in the source code by loading the project into your preferred Java IDE such as Eclipse.
Before the Microservice can be run inside Docker, the Docker technology must be installed on your local machine. You can follow step-by-step Docker installation procedure at: Docker Installation
Once Docker is installed correctly, you can test your installation using the following command:
|docker run hello-world|
Create a Microservice Docker Image
In the Docker ecosystem, there are two main concepts to understand.
- Docker container: A Docker container is a lightweight instance of a Linux based OS running on top of your host Operating System
- Docker image: Docker image represents your Application software + entire environment running inside a container
For the above microservice, the container loads the microservice image, and as part of this image it not only loads the Application Code for the microservice, but also the Java 8 environment it needs to run the microservice.
But, before you can load the microservice into Docker, you need to create a Docker image for that software. The steps to create the image are as follows:
|Create a directory next to your microservice project||mkdir hello-microservice-build|
|Copy microservice artifacts to the build directory||
cp target/hello-microservice-1.0-SNAPSHOT.jar ../hello-microservice-build
cp hello-microservice.yaml ../hello-microservice-build
|Create the Dockerfile file under hello-microservice-build directory||
ADD hello-microservice-1.0-SNAPSHOT.jar hello-microservice-1.0-SNAPSHOT.jar
ADD hello-microservice.yaml hello-microservice.yaml
CMD java -jar hello-microservice-1.0-SNAPSHOT.jar server hello-microservice.yaml
|From the Docker session, goto the hello-microservice-build directory and issue the command||docker build -t hello-microservice-local .|
The Docker build process uses a file named Dockerfile to get its instructions about what to do when building an image. In this particular microservice, the Dockerfile instructs the Docker system to download an image called 'java:8'. This is the core infrastructure needed to run the microservice. Next it adds the microservice jar and configuration to the image. And later, it exposes the ports 9000 and 9001 to service the requests.
docker build -t hello-microservice-local . (is the command that processes the Dockerfile and produces the hello-microservice-local image)
Note: make sure this command is issued from the Docker session and not just any command line session.
Once this Java Microservice Docker image is created, it must be run inside a Docker container using the following command:
|docker run -p 9000:9000 --name hello-microservice-local -t hello-microservice-local|
|-p: exposes port 9000 to the host machine port|
You can test the Microservice in the browser using: http://localhost:9000/java/microservice
Once this works, you can stop the microservice using: docker stop hello-microservice-local
Publish the Microservice Docker Image
Now that you have the Microservice Docker Image working locally, you can publish this image to DockerHub to share with your team. This can be accomplished as follows:
|Create a DockerHub Account||https://docs.docker.com/mac/step_five/|
|Create your Remote Image Repository||
Steps to post your local image to the remote repository
|Get the image id of the local hello-microservice-local image||docker images|
|Tag the local image for push to remote repository||
docker tag localimageid yourdockerhubusername/hello-microservice-remote:latest
|Push to the remote repository||
docker push yourdockerhubusername/hello-microservice-remote
Before testing the remote image, you need to delete the local images. Get the image id for both 'hello-microservice-local' and 'hello-microservice-remote' using: docker images
and remove the two images using the command: docker rmi -f imageid
Once, the images are removed, you can test the remote image using the following command:
|docker run -p 9000:9000 --name hello-microservice-remote -t yourdockerhubusername/hello-microservice-remote|