3 Simple Steps for Containerized Microservices (Dev) with Jolie and Docker

In this short tutorial, let’s look at how we can quickly kickstart a microservice’s codebase that can be developed and run in containers. We achieve this by using Jolie programming language, NPM, VS Code and Docker.

prerequisites

You need to have npm, Docker and VS Code installed. Make sure you have enabled support for dev containers in VS Code. (Jolie comes preloaded in the Docker images we’re going to use, so there’s no need to install it.)

ready? Go!

1. npm . build your project with

To follow this tutorial, create a new directory (for example, tutorial) and enter it.

mkdir tutorial
cd tutorial

Now run the following command from terminal within that directory.

You will be asked common questions that come with npm initLike the license you want to use.

You will then be presented with Jolie-specific questions. Go ahead and just hit Enter. The defaults are what we need for this tutorial.

Specifically, say yes to get the Dockerfile and devcontainer configurations. We will need them.

Next, you choose what type of Jolie project you want. “empty jolie project” Select.

Choose what type of Jolie project you want: Select a blank Jolie project

You should now have the following directory structure (plus the usual node_modules directory).

directory structure

2. Write your service in VSCode

Open the tutorial directory with VSCode.

The editor will automatically detect the presence of .devcontainer folder and ask us if we want to reopen the directory in a container. Go ahead and do so by clicking the blue button at the bottom.

reopen in container

Creating a container may take some time the first time. When done with VS Code Ready, you should see the green confirmation shown below in the status bar at the bottom of the window.

dev container green confirmation

Now we can start coding! In this tutorial, we create a simple service that accepts HTTP requests to generate a greeting, which takes in the query string the name to be greeted. For example, invoke http://localhost:8080/greet?name=Jane should return JSON value like { greeting: "Hi Jane" },

open main.ol file, which contains an empty service called Main,

Open the Main.ol file, which contains the Main .  There is an empty service called

We start by writing the API of our service. It consists of a single operation called greetwho receives messages GreetRequest Reply with more types of messages GreetResponse, For more information, you can see this Introduction to Jolie or Jolie Documentation.

type GreetRequest { name: string }
type GreetResponse { greeting: string }

interface GreeterInterface {
RequestResponse:
	greet( GreetRequest )( GreetResponse )
}

service Main {
	main {
		// Your code here
	}
}

Now we can implement our API in the service MainGetting the following code.

type GreetRequest { name: string }
type GreetResponse { greeting: string }

interface GreeterInterface {
RequestResponse:
	greet( GreetRequest )( GreetResponse )
}

service Main {
	execution: concurrent

	inputPort GreeterInput {
		location: "socket://localhost:8080"
		protocol: http { format = "json" }
		interfaces: GreeterInterface
	}

	main {
		greet( request )( { greeting = "Hi " + request.name } )
	}
}

Property execution: concurrent Tells Jolie that the service should handle clients concurrently.

then we have one inputPort To define an access point for our API, which is available on localhost on TCP port 8080 (location), uses HTTP as the preferred format with JSON as the transport (protocol), and exposes the interface we defined earlier (interfaces,

In main block, we define the implementation of greet after the name in the request “Hi!” Return operation.

3. Drive it!

We can run our service within dev container using terminal in VS Code. go for Terminal -> New Terminal To open terminal, as shown below.

go to terminal -> new terminal to open terminal”/></p>
<p>You should now see a terminal panel like the following.</p>
<p><img decoding=

Run the following command in it.

Your service should be running (with no visible output), and port 8080 should be automatically forwarded to your local machine.

The service should be running and port 8080 should be automatically forwarded to the local machine

You can test the service by running a command like the following from a normal terminal in your local machine (outside VS Code). here we use curlBut any tool for making HTTP requests should work (including your browser).

curl 'http://localhost:8080/greet?name=Jane'

test service with curl command

When you want to build a Docker image from your project, you can use the automatically generated Dockerfile, The following command will create a Docker image.

docker build . -t tutorial:latest

To test it, you can run it locally like this:

docker run -p 8080:8080 -it --rm tutorial:latest

For an explanation of the flags, check the Docker run documentation. necessarily, -p 8080:8080 makes port 8080 available to the host, -it runs the container interactively, and --rm Removes the container after it is finished. You should be able to test the service by running curl command shown above.

You can also watch this tutorial in the video below.

this much only! We have built a codebase to develop a microservice with Jolie in a dev container and deploy it with Docker!

Leave a Comment