Creating docker image of Node and Spring Boot applications.

With Docker’s ease of deploying the application, getting rid of configuring server, and the environment of the application, ,most company uses docker for their products. In this article, I will talk about how we can create a docker image of the Node.js and Spring boot applications. Dockerizing Spring boot applications can be done in different ways which we will discuss.

Docker File

Docker file is the file used by the docker engine to create the docker image of the application. It is a set of instruction which are run by docker engine. Think of it as steps that you would have followed while deploying your application to the server. Instruction are in specific format Argument/Command + Instruction. Argument(Command) tells what we want to do with the instruction whether to run, copy etc. In the below command we are telling to run the instruction to install MySQL.

RUN pip3 install mysql

Docker image of Node.js app

To dockerize react app, angular app or any node.js application we can use the same file with some modifications with the command. To create an image, first we will create a file named Docker without any extension.

We can use the other image as well to create the environment. Here our first command will be to set up the node environment, for which we can use the node image as the base image on which our image will be created. We can specify the node version with tag using :tagname or else it will pick the latest docker image.

FROM node

Now we have set up the environment of the application. We have to setup the working directory where we will add all the files of the project.

WORKDIR /app

Now we can add all the files of the project there. We can skip adding node_modules file by adding it to the .dockerignore file to fast the process of image creation. And run the npm install command later on. You may find online that they copy the package.json first and then run npm install. It is the same as copying all the contents and then runs the command.

COPY . .
RUN npm install

We have completed the set up of the environment, added the source code and the dependencies. To run the application we will pass the same command as we use it on our local machine without docker to CMD. It can be npm start which is the command as mentioned in package.json or node server.js

CMD ["npm", "start"]

There can be other steps like exposing the port which can be done using command EXPOSE 8080. The docker file should be at the root level of project or else mention the path of docker file. To create image run the following command.

docker build -t dockerhub-username/imagename:tag

Spring Boot application

To create image of spring boot application we can either use maven or docker file.

Using JAR file

Docker file for creating from jar. Change the jdk version corresponding to version your application is using. Here we did not created any workdir as we are using the default. You can create one and change the path for copy command.

FROM openjdk:16-alpine3.13
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

The above dockerfile uses the jar file created from the application. You can create it using command

./mvnw package && java -jar target/springboot-docker.jar

Using Maven Wrapper

You can create docker file with commands similar to running the application on local machine without creating jar file.

FROM openjdk:16-alpine3.13
WORKDIR /app
COPY src ./src
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:go-offline
CMD ["./mvnw", "spring-boot:run"]

.mvn folder contains project specifications for running the maven command. We are using the maven wrapper(mvnw) as we do not want to install the maven in our docker image environment. We are using go-offline, as we do not want it to check the dependency version online, it should versions as mentioned in the pom file only. It may fail if the artifact is not available locally, so make sure to run the application successfully before creating the application.

After creating the docker files use the docker build command to create the application.

Using Maven plugin

Maven plugin can be used to create the image of the application

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>docker-hub-username/app-name:tag</name>
</image>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

After adding above code to pom.xml file, run mvn clean install. It will create the docker image of application.

Key points

Remember I said we can copy all files and then run the npm install command. The image will be created successfully but it will not be an efficient image. Docker builds images using layers and every command is a layer. Docker builds the cache which helps to save time in the next build. The efficient approach of creating build is to write commands from less frequently changed to more frequently changed.

In build the dockerhub-username is added as it makes easy to push the images to the dockerhub.

I hope you will be able to create the docker images easily by reading this article. Feel free to comment below, any suggestion or any topic you want me to write on.

◾Software Developer ◾Medium Writer. https://web.cs.dal.ca/~ruminder

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store