Playing with Docker- Deploying micro-services in Docker
What is docker
Docker is a computer program that performs operating-system-level virtualization also known as containerization. It was first released in 2013 and is developed by Docker, Inc. [1]. Five years old ha?, Better late than never, It’s good to give a try and explore.
Above definition may not give you a good understanding, unless you do some further reading. Therefore let me rephrase as how I understood. For me It’s a VM (called as container) with a management programme (Docker), where you can instantiate your VM(container) and programme the deployment of your application.
Docker containers are similar to VMs bit not the same. This [2] has a comparison of Docker containers and VMs. Containers and virtual machines have similar resource isolation and allocation benefits, but function differently because containers virtualize the operating system instead of hardware[2].
Why docker
We use VMs when we do not have actual physical machines. Nowadays applications are complex and distributed, while a app/service runs on a one server, databases and analytics run on some other servers, and these may have multiple instances to support high availability. Hence the virtualization techniques needs to be advanced and support the same. Here comes Docker into play, Docker supports instantiating containers and deploy your applications as and when you need it. Docker containers are fast, lightweight and easy to configure and deploy comparing same with VMs. Additionally you can have multiple containers running on the same machine and programme the deployment.
How deploy a simple app/service with Docker
1. Install Docker
Docker can be installed on Windows, Mac Or Linux. Official docker website has the instructions for each. However installing and working with docker is easy in Linux OSs. I installed docker on Ubuntu which is my favorite following this[3] link.
2. Create Image
Similar to VMs, docker containers starts with a base image, where you can install your app and its dependencies on this image. There is another way of creating image, that is Dockerfile, where you list down the instructions of how to create the image. I prefer this method as we have the history or the way how we created the image
3. Run/Start Docker Image
There are few useful docker commands to pull, create, and push images to/from Docker-hub. Also Run,Stop and attached to running container and check status of containers.
4. Access the webservice
To access the services inside docker, we have to expose those ports via host OS. Docker provides this support via expose directive.
Hands On
Hope installing docker will not be a big issue, I’ll share what I have done for points 2,3,4 above. I have chosen a micro service to demonstrate a running service inside a docker. You can easily create a micro-service, using some framework or library and test that on your machine so that we know it works. I sued WSO2 ms4j[4] for this, if you follow ‘Hello world with MSF4J’ section
on this[4] readme doc you can easily built and test it.
For image creation I chose the Dockerfile approach, where this file defines how to pull the base image, install your service artifacts on top it, set the startup command and how to expose the required ports. Without going into details Ill just list my Dockerfile as it describes itself.
#FROM is the base image for which we will run our application
FROM java:8
# Copy files and directories from the application
COPY Hello-Service*.jar /opt/
COPY helloService.sh /opt/
#Tell Docker what command to start
CMD sh /opt/helloService.sh
# Tell Docker we are going to use this port
EXPOSE 9090
Here, the helloService.sh file contains the command to start the service
nohup java -jar /opt/Hello-Service-*.jar & tail -f nohup.out
Make a folder let say MicroService and have all artifacts together, Here’s the content in my folder
ubuntu@ip-172–31–27–52:~/MicroService$ ls -la
-rw-rw-r — 1 ubuntu ubuntu 304 Aug 5 02:27 Dockerfile
-rw-r — r — 1 ubuntu ubuntu 13944703 Aug 5 02:09 Hello-Service-0.1-SNAPSHOT.jar
-rw-rw-r — 1 ubuntu ubuntu 61 Aug 5 02:19 helloService.sh
Now within the folder run these commands
sudo docker build -t microservice-test .
This will create a image called microservice-test
sudo docker images will show you the image you just build
REPOSITORY TAG IMAGE ID CREATED SIZE
microservice-test latest 125512326917 4 weeks ago 657MB
Then start the image with following command
sudo docker run -p 8080:9090 microservice-test
sudo docker container ls will show you the running containers
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cc3f6fb0621 microservice-test “/bin/sh -c ‘sh /opt…” 4 weeks ago Up 4 weeks 0.0.0.0:8081->9090/tcp goofy_hermann
Test the service
ubuntu@ip-172–31–27–52:~/MicroService$ curl 127.0.0.1:8080/hello/susinda
Hello susinda
You can stop a running container using
sudo docker stop 6cc3f6fb0621