Skip to main content

Deploying ActiveJ Server to AWS Using Docker

In this tutorial we will deploy the HTTP Server from the getting started tutorial to AWS. We will use Docker for this purpose.

1. Assemble JAR-file

Open your server's pom.xml file and insert this config:

<build>
<plugins>
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>HttpHelloWorldExample</mainClass>
</transformer>
</transformers>
<finalName>HelloWorldExample</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Next, type the following commands:

  • mvn clean
  • mvn package -DskipTests=true

These actions will produce a HelloWorldExample.jar archive. You can find it in the target folder of your project. It is a typical way to distribute and run Java applications.

2. Wrap it as a Docker image

If you don't have Docker installed on your machine, you can follow this guide.

Docker is a de-facto standard for deploying any cloud-based application. We are following this standard and propose you to wrap your ActiveJ application as a Docker image.

For that, you need to create a Dockerfile in the root directory of your project. Here is a minimal Dockerfile example:

FROM openjdk:17-alpine

WORKDIR /app
COPY target/HelloWorldExample.jar ./
EXPOSE 8080

ENTRYPOINT java $SYS_PROPS -jar HelloWorldExample.jar
note

This application uses JDK/JRE 17 with Linux Alpine inside

Short logic description:

  • First of all, we should build our application. It copies all necessary data for JAR file creation and builds it.
  • Later, it moves JAR file to the application root and launches it on port 8080.

You can build it using the following command: docker build -t activej-app ., and run this image on docker daemon: docker run --rm -p8080:8080 activej-app. All actions will be the same if you are using docker-machine.

3. Deploying your application to the cloud (AWS, as an example)

First of all, you should own an Amazon AWS EC2 account and have a running EC2 instance.

Here is a guide on how to deploy your application (not Docker image):

  • Download your Amazon key (key_name.pem).
  • Execute chmod 400 key_name.pem for granting read-only property to this file.
  • Connect to your EC2 instance via ssh: ssh -i key_name.pem user@instance-id
  • Open new Terminal/iTerm tab and try to send your JAR file via FTP protocol: scp -i key_name.pem your/app/path/HelloWorldServer.jar ubuntu@instance-id:
  • Wait until your file uploads.
  • Run java -jar HelloWorld.jar in your ssh tab.

Voila! You are running your application on AWS instance. Check it out on your IP address.

4. Deploying Docker container

Here is a guide on how to deploy your Docker image:

  • Download your Amazon key (key_name.pem).
  • Execute chmod 400 key_name.pem for granting read-only property to this file.
  • Transform Docker image to tar archive : sudo docker save activej-app >> activej-app.tar
  • Connect to your EC2 instance via ssh : ssh -i key_name.pem user@instance-id
  • Open new Terminal/iTerm tab and try to send your JAR file via FTP protocol:

scp -i key_name.pem your/image/path/activej-app.tar ubuntu@instance-id:

  • Wait until your file uploads.
  • Run such commands in your ssh tab:
    • sudo apt-get update -y to update cloud repository of Linux system.
    • sudo apt-get install -y docker.io to install Docker.
    • dockerd to launch Docker daemon.
    • docker load -i activej-app.tar to unzip archived image.
    • docker run -p 8080:8080 activej-app to launch your application in Docker container.

Hurray! You are running your Docker image with the ActiveJ app on AWS instance. Check it out on your IP address.