Sunday, 9 August 2015

Deploy your Spring Boot microservice + Kibana in a Docker container

Summary


In this step, we are are going to deploy our solution to a Docker container. This eases deployment as we don´t have to start/stop one by one each module (ie. the microservice, ElasticSearch and Kibana) and the port configuration is much simpler to do.

Docker installation


If you are running on a Linux distribution, Docker comes out of the box. In Windows or MacOS, it´s a bit more complicated. One option to run Docker on those systems is to use Boot2Docker, (which actually runs a virtualized Linux with Docker installed in it).
You can find the installation instructions here.

Creation of the Docker image


The specifications of the Docker Image are defined in a DockerFile file, placed within the application resources. Here is an explanation on each line of the file:


Note: I based this file on the one used in this example.

In order to create the Docker image to run, you need to get access to the DockerFile file and our application´s executable Jar file generated by the Maven build. You can achieve this in two steps:

  • Create a shared folder called "stokker" in your VirtualBox agent. This folder should point to the folder in your local host where the code is compiled.
  • Run this command to mount that folder in the embedded Linux OS running Docker:
    • sudo mount -t vboxsf stokker /mnt/stokker

Now, change the current directory to /mnt/stokker/target and run this command to register locally the image:

docker build -t victor-ferrer/stokker


Run it!


First, find out the image id of the newly created image:
docker images

By executing this command you will launch the image execution:
docker run -i <image_id> -P

Note: The -P option enables the port exposure defined in the DockerFile. If that parameter is missing, you will have to manually provide the mappings (along with the -p option).

Note #2: If you are running the Docker image in VirtualBox, you will have to expose the same ports again, so they can be used from outside VirtualBox. In order to do so, open the VM network configuration and add the following port forwarding:





That should be all. You can check any of the Spring Boot Actuation endpoints exposed in port 8080 or the Elastic search cluster status endpoint:

  • http://<container ip address>:8080/health
  • http://<container ip address>:9200/_cluster/health

You can get the <container ip address> by executing a netstat command and choosing the IP associated to the VirtualBox container.

Note: I´m still working on launching Kibana along our application and ElasticSearch...
Moreover, it is stated in the Docker best practices, that is not a good idea to run several processes in the same Docker container, as that impairs horizontal scalability and application decoupling. So, I´m not sure whether I will finally add Kibana to this image or to a new one...

Related articles


  1. Define the application input: Google Spreadsheets
  2. Implement a Spring Integration flow with Spring Boot
  3. Integrate the application with ElasticSearch
  4. Represent and search your data with Kibana

2 comments:

  1. Victor, nice work! I recommend you to have a look at http://www.jarloo.com/category/finance/ Inside there is a lot of useful information about Yahoo / Google / Interactive Brokers / ... APIs for retrieving finance data.

    ReplyDelete
    Replies
    1. Thanks Paco.
      Definitively worth looking at.

      VĂ­ctor

      Delete