Micro Services with Docker
In our previous discussion, we had enough discussion on Containers vs Virtual Machines. So here I just want to talk more about containers and how they are used in the industry today. Previously, we used to run all our production servers on Ubuntu and have decided to move all our production set up to CentOs environment. It made us re-install all our dependencies again as we were doing it on different OS. It was very painful for us to download all dependencies once again manually. In order to overcome all these portability challenges, we have decided to use Docker.

On top of base OS, we will have to install container engine like Docker. For entire setup, we just need to have a pre-configured file called Dockerfile which helps us to run each application as an isolated VM called containers on top of container engine(Docker). It helps us to run containers on any platform, there won’t be any additional manual things to work upon.
Encapsulate spring boot application in a Docker container
I’m currently working on the online video interview platform named, Panna. Since it is an end to end interview platform, it has different services. So we have decided to break down all our services into MicroServices in order to make it independent of other services for easy maintenance and scaling. Let me explain how we have encapsulated one of our components, Resume parser.

The above Dockerfile will tell docker how our image should be built. Let’s take a look at each command:
FROM 235746/centos-java: This is a custom built docker image by me which consists of CentOs OS along with java on top of it.
RUN mkdir /opt/resume-application: It creates a folder to copy the jar file inside it.
COPY resume-snapshot-0.0.1.jar /opt/resume-application/: It copies the jar from the build directory to the specified path.
WORKDIR /opt/resume-application/: It will change our current directory to the specified path.
EXPOSE 9090: Here it tells Docker to expose the port 9090 when a container based on your image was started.
ENTRYPOINT [ “sh”, “-c”, “java -Dspring.profiles.active=production resume-snapshot-0.0.1.jar” ]: It will start our application, whenever the container starts.
Once we write our Dockerfile, now it’s time to push it to DockerHub or any other docker registry. You can even have our own docker host registry. Since I use DockerHub, let me build and push our Dockerfile into it.
docker build -t <name> .
The above command is used to build our Dockerfile. Let’s create a tag and push it to docker registry.
docker login
docker tag <tag-name> docker-id/repo:tagname
docker push docker-id/repo:tagname
Now, this can be pulled anywhere and start running by exposing it to port 80 or 443.
docker run -p 80:9090 docker-id/repo:tagname
You can access your application by the IP of the server you are running this container. I hope this would help you build your own Dockerfile.
http://hostname
Conclusion:
Docker simplifies much of admin work and ensures the process is clean. It helps us to maintain different version of the software and easy to build and shipped to production.
