Deploying using Docker

This section describes the steps to deploy sample HATS application in Docker using either docker run or docker-compose.

Note : This example is only for the demonstration purpose.

Deploying using Docker run

Follow these steps to deploy the HATS application using docker run command:

  • Create a HATS project and export it as an ear.

  • Create a Dockerfile (or refer it from here using below code which deploys a simple HATS application in a Liberty server.

    # Use the official Liberty image as the base image
    FROM open-liberty:21.0.0.9-full-java8-openj9
    
    # Copy HATS application ear file to the Liberty dropins directory
    COPY HATS.ear /config/dropins/
  • Navigate to the docker file location, add the HATS ear file to this location, open the terminal and build the docker file using below command, which creates an image called Hats.

    docker build -t Hats .
  • Once the image is built successfully, deploy the HATS application using below command.

    Syntax:

    docker run --name <container_name> -p <local-machine-port>:<container-port> -d <image-name>:<image_tag>

    Example:

    docker run --name hatscontainer -d -p 82:9080 Hats
  • After the successful deployment, access the HATS application using below URL in the browser.

    http://localhost:82/<HatsProjName>
  • User can modify the Dockerfile to customize the base Liberty image or adjust any other build parameters based on your requirements.

Adding environmental variables

Environment variables can be either set during image creation through Docker File or while running the container through docker run command "-e" option.

To set the enviroment variables while building the image, user can use below code in DockerFile:

 ENV ENV_Var_Name= ENV_Var_Value 

Example:

ENV HATS_RUNTIME_CFG_ENV=/home/runtime 

To set the enviroment variables while starting the container, user can use below command:

docker run --name <container_name> -p <local-machine-port>:<container-port> -e "<env-variable-name1>=<env-variable-value2>" -d <image-name>:<image-tag>

Example:

docker run -p 82:9080 -e HATS_RUNTIME_CFG_ENV:/home/runtime -d hatsimage:latest

For more information on HATS specific environmental variables, refer to Application ENV variables

Adding volume mounts

To access the runtime logs and other information, user can add the volume mounts and save the required log files in the preffered location. Use below command to mount the local directory with the container.

docker run --name <container_name> -p <local-machine-port>:<container-port> -v <your-local-repo-path>:<container-folder-to-be-mounted> -d <image-name>:<image_tag>

Example:

docker run -p 82:9080 -v C:/hats/logs:/home/logs -d hatsimage:latest

Deploying Application using Docker Compose

  1. Download docker compose configuration

  2. Open a terminal window and change to the root directory where the docker-compose.yml is located in the downloaded docker compose configuration folder.

  3. Make sure the docker-compose folder has read/write permission for volume requirements.

  4. If needed user can edit the directories mapped in the docker compose file to their preferred location which is valid and has appropriate permissions.

  5. Run the below command to start the container images

     docker-compose up –d
  6. You can optionally check the container logs using the following command:

     docker-compose logs -f <container_name>
  7. To stop all the containers run:

    docker-compose down	
  8. Once the command has been launched, be sure that the containers are started using the following command

    docker ps
  9. After the successful deployment, access the HATS application using below URL in the browser.

    	http://localhost:<mapped-port>/<hatsappname>