Skip to content

Dockerizing an application

Creating a Docker File

First thing you really need to do for your application is to create a Dockerfile which will essentially be a build script for the docker image.

Dockerfile
# Use Amazon Corretto 17 as the base image
FROM amazoncorretto:17

# Create app directory
RUN mkdir -p /var/apps

# Set the working directory
WORKDIR /var/apps

# Add the application's jar to the container
ADD build/libs/app.jar app.jar

# Make port 9085 available to the world outside this container
EXPOSE 9085

# Run the jar file
ENTRYPOINT ["java","-Xmx512m","-jar","app.jar"]

When it comes to deploying our app in a docker container on our server we have two strategies

  1. Deploy our app.jar, Dockerfile, and a docker-compose.yml file which runs the docker command to build the image and run the container

  2. Build the image locally, deploy the image to a docker repository, like our self hosted nexus, then simply run a docker pull command on an already deployed docker-compose.yml file that specifies the docker repository, credentials, and image name.

Deploying Dockerfile with build and docker-compose

On the target server, we can go ahead and create a directory for our docker app in

/home/thomas/docker/springboot-app

Let's create a docker-compose.yml file in our app directory next to our Dockerfile.

docker-compose.yml
version: '3.8'

services:
  springboot-app:
    build:
      context: .  # This should be the directory where Dockerfile is located
      dockerfile: Dockerfile
    ports:
      - "9085:9085"  # Maps the host port to the container's exposed port
    environment:
      - JAVA_OPTS=-Xmx512m
    restart: always
    volumes:
      - ./logs:/var/opt/fantasydashboard/spring/logs

Note

If you wanted to run this app using docker compose, use the --build flag to ensure it's building the image, particularly if you update the app.jar file. So that would look like

docker compose up -d --build

Deploying with Gradle

In this SpringBoot example, we'll use the gradle plugin id 'org.hidetake.ssh' version '2.11.2' to ssh to our server, deploy each file, and run the docker up command.

build.gradle
plugins {
    id "application"
    id 'java'
    id 'org.springframework.boot' version '3.3.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'org.hidetake.ssh' version '2.11.2' // SSH plugin
}

version '1.0-SNAPSHOT'

dependencies {
...
}

/**
 * Make the you add the server as a known_host
 * To do this, connect to it in command prompt like
 * ssh -i "C:\Users\Thomas\path\to\private\key\file" <linux-user-name>@<server-ip-address
 */
ssh.settings {
    knownHosts = file("${System.env.USERPROFILE}/.ssh/known_hosts")
}

/**
 * These are properties in my user gradle.properties file located at C:\Users\Thomas\.gradle\gradle.properties
*/

remotes {
    homeServer {
        host = home_ip // Replace with your instance IP
        user = home_user // Replace with your SSH username
        identity = file(home_pk) // Replace with the path to your private SSH key
    }
}

bootJar {
    archiveFileName = 'app.jar'
}

task deployHome(dependsOn: 'bootJar') {
    doLast {
        ssh.run {
            session(remotes.homeServer) {
                // Assuming your JAR is built to the build/libs directory
                put from: project.file("build/libs/app.jar"), into: '/home/thomas/docker/fantasy-microservices/' // Replace with remote path
                put from: project.file("Dockerfile"), into: '/home/thomas/docker/fantasy-microservices/' // Replace with remote path
                put from: project.file("docker-compose.yml"), into: '/home/thomas/docker/fantasy-microservices/' // Replace with remote path
                // Restart the service on the remote server
                execute 'docker compose -f /home/thomas/docker/fantasy-microservices/docker-compose.yml up -d --build' // Replace with your actual service name
            }
        }
    }
}

Now when we run the gradle task deployHome, we'll see our build app.jar, Dockerfile, and docker-compose.yml file get deployed to the server and the container is up and running.

Comments