Containerizing a Native Executable in Spring 3

Yash Patidar
3 min readAug 29, 2024

--

In today’s cloud-native world, deploying applications in containers has become essential for scalability and portability. In this article, we’ll go through the steps to containerize a native executable built from a Spring 3 application using Docker.

Why Containerize a Native Executable?

Containerizing a native executable provides several benefits:

  • Consistency: Ensures that the application runs the same way in different environments.
  • Isolation: Each container operates independently, reducing conflicts between applications.
  • Scalability: Easily scale your application by deploying multiple container instances.

Prerequisites

Before we start, make sure you have the following:

  • A Spring 3 application compiled into a native executable (as discussed in previous articles).
  • Docker installed on your machine.

Steps to Containerize the Native Executable

Step 1: Create a Dockerfile

The first step is to create a Dockerfile in the root of your project directory. This file contains the instructions for building the Docker image. Here’s a simple Dockerfile suitable for our native executable:

FROM public.ecr.aws/docker/library/oraclelinux:8-slim
WORKDIR /app
COPY target/native-executable .
RUN chmod +x native-executable
EXPOSE 9090
ENTRYPOINT ["/app/native-executable"]

Breakdown of the Dockerfile

  • FROM: This line specifies the base image for your container. In this case, we are using the slim version of Oracle Linux 8.
  • WORKDIR: Sets the working directory for the container to /app. This is where your application will reside.
  • COPY: This command copies the native executable from the target directory of your local machine into the /app directory of the container.
  • RUN: This command changes the permissions of the native executable to make it executable.
  • EXPOSE: This line informs Docker that the container listens on port 9090 at runtime. You can change this port based on your application’s requirements.
  • ENTRYPOINT: This command defines the command that will run when the container starts, in this case executing the native executable.

Step 2: Build the Docker Image

Navigate to the directory containing your Dockerfile and run the following command to build your Docker image:

docker build -t spring-native-executable .

This command will create a Docker image named spring-native-executable using the Dockerfile in the current directory.

Step 3: Run the Docker Container

Once the image is built, you can run a container from it using the following command:

docker run -p 9090:9090 spring-native-executable

In this command, the -p flag maps port 9090 of your host machine to port 9090 of the container, allowing you to access your application via http://localhost:9090.

Step 4: Verify the Application

After running the container, open your web browser or use a tool like curl to verify that your Spring application is running:

curl http://localhost:9090

You should see the expected response from your application.

Conclusion

Containerizing a native executable built with Spring 3 is a straightforward process that enhances your application’s portability and scalability. By following the steps outlined above, you can easily deploy your Spring application in a Docker container, making it ready for cloud environments.

--

--