How To: Dockerize WSO2 products

Written by hashanbn | Published 2017/12/31
Tech Story Tags: docker | wso2

TLDRvia the TL;DR App

I used to think dockerizing is a hard process, but when I actually tried it out I realized it’s not hard at all. Well, dockerizing isn’t an actual name, but it’s an easy way of saying containerizing a setup using docker. I didn’t come up with that name but my colleagues did, I’m merely using it since it’s actually easier to call it that :).

Anyhow, as I mentioned earlier once you actually start doing it, it doesn’t look hard at all. Let me guide you through it step by step using docker and WSO2 products.

First of all, install docker and docker-compose in your computer. I will not go through this procedure in detail since you can find dozens of articles online explaining how to do it. Once you are done with this step, create a folder (I will call it service_root) and copy the zip file of the WSO2 product into it. Create a file by the name Dockerfile inside service_root, and just leave it empty. I will explain what to put in this file, later in the article. Then create another directory inside the service_root and copy all the changed configuration files into that (I will call this directory “resources” from this point on-wards). Go into the resources directory and create a file in this directory named product-setup.sh (You can name this whatever you want :) )

Once you are done with the initial skeleton, open the product-setup.sh file in a text editor, use apt-get install command to install all the required packages inside the container. For example, in this case you need the unzip program to extract the product zip file inside the container. It’s also important to run apt-get update command before running the installation commands to ensure the package manager cache is updated. A very minimal example product-setup.sh file will look as follows.

apt-get updateapt-get install -y unzip

unzip /tmp/wso2ei-6.1.1.zip -d /wso2

mv -f /tmp/resources/changed_config_files /wso2/wso2ei-6.1.1/path_to_the_relevant_config

Don’t worry about how the product zip file got into /tmp , I will explain it later in the article. From this, the main idea you have to get into your head is, first you need to install relevant applications, then unzip the product zip to a desired folder and overwrite the relevant config files in the product distribution, with the updated config files you put to the resources directory. Rename “changed_config_files” to the file names and “path_to_the_relevant_config” to the path of the files in the distribution.

After completing the product-setup.sh file, lets look at the Dockerfile. This is the file docker calls when it’s building the docker image. And in this file, we can also, specify the start up scripts for the docker image. I will first give an example that suits the current context and explain it line by line.

FROM airdock/oracle-jdk:1.8

COPY wso2ei-6.1.1.zip /tmpADD resources /tmp/resources

RUN chmod +x /tmp/resources/product-setup.sh && /tmp/resources/product-setup.shRUN chmod +x /tmp/resources/wait.sh

CMD ["sh", "/wso2/wso2ei-6.1.1/bin/integrator.sh"]

The first ‘FROM’ command specifies the base docker images to be used for the current docker image. This can change based on the requirement. Since I’m trying to run a product that needs java on the docker container, I picked an image with java. If you you need some other service, then you can use a different pre-built image. Please visit docker-hub to explore the available pre-built images. You just have to select an image from here, then find the import address then use “FROM” command to use that pre-built image.

The ‘COPY’ command is used to copy the product distribution into the docker image. In here, I’m copying it into the /tmp folder inside the container. Then add the resources directory to the container. These paths are used in the product-setup.sh file to perform various tasks. Then give execution access to the product-setup.sh and run it. The “CMD” command specifies that the integrator.sh has to run upon start of the container.

Now that we are done with the product-setup.sh and the Dockerfile, we can move on to define any other services that we need to run. Go out of the directory that contains the Dockerfile, create folders for the rest of the services and in the same way we did earlier, you can define the rest of the services as well. After defining the setup for all the services we can use docker-compose to build and run those services. First we need to define the docker-compose.yml file outside the service_root directories. You can refer to this documentation to get a better idea about the docker-compose.yml. In this article I will briefly explain how you can use docker-compose to build and run these containers defined. Please see the example docker-compose.yml file below.

version: '2'

services:integrator-service:image: integrator-imgcontainer_name: integrator-svcbuild: ./ei-setupdepends_on:- analytics-service- mb-serviceports:- 9443:9443- 8243:8243- 8280:8280

First line specifies which version of the docker-compose definitions we are using. Then under services you can define details about the services which you would like to build and start using docker-compose. In this file, under services we give a name to our service (in this case “integrator-service”). “Image” specifies the name of the build image, “container_name” specifies the name of the container, “build” specifies the name of the folder for a particular service, “depends_on” specifies which services needs to start before the current service starts. This is a bit tricky though, because when you list down a set of services that needs to start first, docker-compose makes sure to start them before the current service, but it doesn’t make sure those services are up and running before it starts the current service. Therefore, you might run into issues if you are using dependent services, but in that case all you have to do is add a line in the start script of your service to wait till these dependent services are up and running. Then in the “ports” section, you can specify which ports needs to be exposed to the outside (product_port:container_port is the syntax to expose ports.

Another important thing is, if you want to communicate between these services, you can refer to them as protocol://service_name:port. Protocol can be http, tcp, etc. and the service name and the ports has to be as defined in the docker-compose.yml.

Now, you are all set, you can run the command docker-compose build && docker-compose up, to build and run the docker containers.

I hope this article was helpful in understanding how to containerize WSO2 products using docker. If there are any questions or any problems you run into while trying this out, please put them in the comments section. I will try and answer all the questions as soon as possible.


Published by HackerNoon on 2017/12/31