There may be many microservices in a projects so to run microservices on docker environment we need to create docker images for microservices.This document provides steps to create docker images for microservices.
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.
Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. Containers are lightweight because they don’t need the extra load of a hypervisor, but run directly within the host machine’s kernel. This means you can run more containers on a given hardware combination than if you were using virtual machines. You can even run Docker containers within host machines that are actually virtual machines
Docker Engine is a client-server application with these major components:
- A server which is a type of long-running program called a daemon process (the dockerd command).
- A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
- A command line interface (CLI) client (the docker command).
For running the MS as a container .we will use docker swarm architecture,
swarm will consist of managers, nodes and services.
A node is basically a worker of your swarm, it will only receive instructions.
A manager is a node that can give instructions to your swarm (i.e. creating and removing services). Managers can also run instances of services.
A service can have multiple replicas and Docker will distribute them evenly across your nodes – if you have 3 (active) nodes and a service with 3 replicas, each node will have a running instance of that service.
For any service in the swarm, Docker will do a round-robin between the replicas (shameless plug, watch Load balancing is impossible). Docker call this feature of distributing incoming connections from outside the swarm ingress load balancing. Is important to say that if you access a service from within the swarm it will also go through the built-in load balancer.
If you try to access a service on a node that’s not running it Docker will re-reoute the request to a node that has a running instance of that service.
Here the component labeled LB is serving as a reverse proxy and it will have to be able to discover new nodes in your swarm dynamically.
Save your MS folder under /root/Docker-MS-Testing
Create docker file and build the image.
Installation steps to create docker image for MS and push on docker registry
These steps have been testes for an ant build.
Image Creation Step for MS:
1- Copy dist folder to docker host.
2- Create docker file in dist folder
RUN mkdir -p RSA_docker
RUN [“chmod”, “777”, “./runRSA.sh”]
RUN [“echo”,”———-runRSA.sh——–EXECUTED “]
3- Build image with given below command
docker build -t (name of image) .
Image name should be (MSNAME-MSVENDOR-VERSION)
4- Check image created successfully with command
5- Tag image and push to docker repository
docker tag RSA-1.0.0 docker-repo.com:5000/ RSA-1.0.0
docker push RSA-1.0.0 docker-repo.com:5000/RSA-1.0.0
Running MS Images with Docker swarm service
For Running the MS docker image on swarm node, will use docker swarm service from docker swarm manager.
1-Without Node Constraints
docker service create –name rsaservice –reserve-cpu 1 –reserve-memory 5560mb –network my-overlay-net –replicas 1 -p 7099:7099 docker-repo.com:5000/rsaimage
2- With Node Constraints
a- Get the node details with given below command. In command output we get the node id list.
docker node ls
b- Run the command with node id on docker manager
docker service create –name rsaservice –reserve-cpu 1 –reserve-memory 5560mb –network my-overlay-net –replicas 1 –constraint ‘node.id==jjmjz9ns6lev1r1gef32og1vz’ -p 7099:7099 docker-repo.com:5000/rsaimage
3- Access logs in running container using bash on docker host
To know where container is running we have to run following command from docker swarm manager host:
a- Give the details of node id
docker node ls
b- To give docker running swarm service list
docker service ls
c- To find the docker container id and node id on which container is running
docker service ps (service id)
from above command we will get task id for service running on docker host
then inspect the task id with given below command
docker inspect (task-id)
In output we will get container id and node id on which service container is running.
d- Docker host on which container is running, to login into container using bash command
docker exec -it (container-id) /bin/bash
4-To stop running service
To stop running service we will run the given below command from docker swarm manager.
It will stop and remove the container running on dock