import ballerina/http;
import ballerina/log;
import ballerina/docker;
@docker:Expose {}
listener http:Listener helloWorldEP = new(9090);
@docker:Config {
    name: "helloworld",
    tag: "v1.0"
}
@http:ServiceConfig {
    basePath: "/helloWorld"
}
service helloWorld on helloWorldEP {
    resource function sayHello(http:Caller outboundEP, http:Request request) {
        http:Response response = new;
        response.setTextPayload("Hello World from Docker ! \n");
        var responseResult = outboundEP->respond(response);
        if (responseResult is error) {
            error err = responseResult;
            log:printError("Error sending response", err);
        }
    }
}

Docker Deployment

Ballerina supports generating Docker images, Dockerfile based on annotations. A single Ballerina module is mapped to a single Docker Image. Docker should be configured to run the sample.

For more information, see How to Deploy Ballerina Programs and Services in the Cloud.

import ballerina/http;
import ballerina/log;
import ballerina/docker;
@docker:Expose {}
listener http:Listener helloWorldEP = new(9090);

Adding the @docker:Expose{} annotation to a listener endpoint exposes the endpoint port.

@docker:Config {

Adding the @docker:Config{} annotation to a service modifies the generated Docker image and Dockerfile. This sample generates a Docker image as helloworld:v1.0.0.

    name: "helloworld",

Docker image name should be helloworld.

    tag: "v1.0"
}
@http:ServiceConfig {
    basePath: "/helloWorld"
}
service helloWorld on helloWorldEP {
    resource function sayHello(http:Caller outboundEP, http:Request request) {
        http:Response response = new;
        response.setTextPayload("Hello World from Docker ! \n");
        var responseResult = outboundEP->respond(response);
        if (responseResult is error) {
            error err = responseResult;
            log:printError("Error sending response", err);
        }
    }
}

Docker image version should be v1.0.

# Build the Ballerina program to generate the Docker image and Dockerfile.
$ ballerina build docker_deployment.bal
Compiling source
	docker_deployment.bal
Generating executables
	docker_deployment.jar
Generating docker artifacts...
	@docker 		 - complete 2/2 
	Run the following command to start a Docker container:
	docker run -d -p 9090:9090 helloworld:v1.0
# Verify if the Docker image is generated
$ docker images
REPOSITORY  TAG      IMAGE ID            CREATED             SIZE
helloworld  v1.0    8d5b2f26145b        2 seconds ago        127MB
# Run the generated Docker image
$ docker run -d -p 9090:9090 helloworld:v1.0
# Access the service
$ curl http://localhost:9090/helloWorld/sayHello
Hello World from Docker!