Step 4: Run the application inside a Docker container
Intro
In this step we will prepare our backend application for deployment, so that it can become a web service.
We will use Docker, so make sure it is installed on the server where you want to deploy the application (this may be your local computer, too).
Update the application.properties
Add the following code to the (initially empty) backend app
file application.properties
located in src/main/resources/
:
server.port=${APP_PORT:8080}
This will allow us to configure the application port via an environmental variable.
Dockerfile
for the backend app
Create a file named Dockerfile
in the root of the application’s backend source folder:
Mind the capital 'D' letter in the name of the file. It is important that the file name be exactly 'Dockerfile'. |
Put the following code in the created Dockerfile
file:
FROM openjdk:17-alpine as builder
WORKDIR /app
COPY . .
RUN chmod a+x ./mvnw
RUN ./mvnw install:install-file -Dfile=lib/com.company.tutorial3.datamodel_1.0.0.jar -DgroupId=com.company.tutorial3 -DartifactId=datamodel -Dversion=1.0.0 -Dpackaging=jar -DgeneratePom=true
RUN ./mvnw install:install-file -Dfile=lib/com.company.tutorial3.simulation_1.0.0.jar -DgroupId=com.company.tutorial3 -DartifactId=simulation -Dversion=1.0.0 -Dpackaging=jar -DgeneratePom=true
RUN ./mvnw clean package
FROM openjdk:17-alpine as runner
WORKDIR /app
COPY --from=builder ./app/target/backend-0.0.1-SNAPSHOT.jar backend-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","backend-0.0.1-SNAPSHOT.jar"]
Here we set up the details for out future Docker image of the backend application:
-
As the base image for compilation and running, a Docker image with pre-installed
JDK17
is used. -
The
app
folder is created in the root of the internal file system and used as the working directory (inside image’s file system). -
the
mvnw
is a so-called 'Maven wrapper' - a tool to use Maven even when it is is not installed. Themvnw
script is included in the generated Spring Boot application by default. Here we ensure that it has execution privileves. -
The
install:install-file
installs a library into the local Maven repository. Here we install the 'datamodel' and 'simulation' jars to be able to use them as Maven dependencies when building the backend. -
The
./mvnw clean package
builds a jar file of the application and places it into the/target
folder. -
The second
FROM
command starts another stage in the multi-stage build. -
The compiled application (jar file) is copied from the first stage and placed in the
app
folder (which is the current working directory). -
Finally, the entrypoint is set: when a Docker container is created, the application will be started via the
java -jar <jar file path>
command.
Build and run
-
Copy the backend app source code (including the newly created
Dockerfile
) into a system with Docker installed (we will call it 'the server'). -
In the backend app source root on the server, run the following command (mind the dot in the end):
docker build --tag sc-tutorial-part-4:latest .
-
Run the following command on the server (from any folder) to start the backend application on port 13500:
docker run -d --rm -p 13500:8080 --name sc-tutorial-part-4 sc-tutorial-part-4:latest
Check the result
Send a GET
request to run scenario analysis (use curl, Postman, or any other API tool of your choice).
Here is a command you can run from the server with the backend app:
curl localhost:13500/scenarioAnalysis
This command should return a JSON similar to that in the 'Check the result' section of Step 2.
Try to call this API endpoint from another computer; use your server address (IP or domain name) instead of 'localhost'.
To stop the Docker container, run this command on the server:
docker stop sc-tutorial-part-4
Well done! Your simulation service is available over the web.