Run Spring Boot Application inside Docker container

In this tutorial, we’ll learn how to run a Spring Boot application inside a docker container along with that we’ll also learn how to create a docker image.

Pre-requisite

  • Docker Installation
  • Spring Boot application

Reference to the code

Download from Github.

Create Docker Image

Terminal
1
$ touch Dockerfile

Open this file in editor.

Pull Base Image

To run java application inside our container, it is mandatory to include Java Runtime environment in the container. For this, we’ll use base linux image with JDK/JRE pre-installed in it from Docker Image Repository.

Dockerfile
1
FROM  openjdk:oraclelinux8

Working Directory

Now, we will define working directory inside our container.

Dockerfile
1
2
FROM  openjdk:oraclelinux8
WORKDIR /app

Copy resources

Now, we will copy our java application inside the container using COPY command

Dockerfile
1
2
3
FROM  openjdk:oraclelinux8
WORKDIR /app
COPY soap-service-using-spring-0.0.1-SNAPSHOT.jar ./

To start this application inside our container, we would require some commands which we will write in a separate file to keep the Dockerfile as clean as possible. We’ll create a shell script for that.

start.sh
1
2
#!/bin/bash
java -jar soap-service-using-spring-0.0.1-SNAPSHOT.jar

Link for the jar file used in this example.

Now, we’ll write instruction to include this file in the image. Along with that we need to provide permission to execute this shell script using chmod +x

Dockerfile
1
2
3
4
5
FROM  openjdk:oraclelinux8
WORKDIR /app
COPY soap-service-using-spring-0.0.1-SNAPSHOT.jar ./
COPY start.sh ./
RUN chmod +x /app/start.sh

Expose port

As our spring boot application will run on 8080 port. We’ll use EXPORT command to inform docker to listen 8080 port on runtime.

Dockerfile
1
2
3
4
5
6
FROM  openjdk:oraclelinux8
WORKDIR /app
COPY soap-service-using-spring-0.0.1-SNAPSHOT.jar ./
COPY start.sh ./
RUN chmod +x /app/start.sh
EXPOSE 8080

Final step is to provide the command to start the application which packaged inside image. We’ll use CMD to inform docker to execute start.sh file.

Dockerfile
1
CMD  ["./start.sh"]

So the file should look like this

Dockerfile
1
2
3
4
5
6
7
FROM  openjdk:oraclelinux8
WORKDIR /app
COPY soap-service-using-spring-0.0.1-SNAPSHOT.jar ./
COPY start.sh ./
RUN chmod +x /app/start.sh
EXPOSE 8080
CMD ["./start.sh"]

Build Docker Image

To build the image we’ll write build command in terminal. If you’re running this command for first time. Docker will downalod openjdk:oraclelinux8 image from the repository, so it might take sometime & depends on internet speed. Otherwise you’ll see following output.

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ docker build .
Sending build context to Docker daemon 18.69MB
Step 1/7 : FROM openjdk:oraclelinux8
---> 95b80f783bd2
Step 2/7 : WORKDIR /app
---> Using cache
---> ab428f3005b6
Step 3/7 : COPY soap-service-using-spring-0.0.1-SNAPSHOT.jar ./
---> Using cache
---> 24f93965c2be
Step 4/7 : COPY start.sh ./
---> Using cache
---> a0a0f87344c1
Step 5/7 : RUN chmod +x /app/start.sh
---> Using cache
---> 27ea01678cf0
Step 6/7 : EXPOSE 8080
---> Using cache
---> 83b543cc1ed9
Step 7/7 : CMD ["./start.sh"]
---> Using cache
---> ebf5928537a4
Successfully built ebf5928537a4

Now, you can check the images built locally.

Terminal
1
2
3
$ docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 60c5c8e03fa8 4 seconds ago 543MB

It become difficult to identify the images in furture because there is no name attached to the repository. For this docker provides a way to add meaningful to your image.

$ docker build -t <username>/<repository-name> .

For this example, we’ll use

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ docker build -t tutorialflix/soapservice .
Sending build context to Docker daemon 18.69MB
Step 1/7 : FROM openjdk:oraclelinux8
---> 95b80f783bd2
Step 2/7 : WORKDIR /app
---> Using cache
---> ab428f3005b6
Step 3/7 : COPY soap-service-using-spring-0.0.1-SNAPSHOT.jar ./
---> Using cache
---> 2cbc28ea1ae6
Step 4/7 : COPY start.sh ./
---> Using cache
---> 269ebed9e767
Step 5/7 : RUN chmod +x /app/start.sh
---> Using cache
---> 75a5932d2c95
Step 6/7 : EXPOSE 8080
---> Using cache
---> 4986dca9c21f
Step 7/7 : CMD ["./start.sh"]
---> Using cache
---> 60c5c8e03fa8
Successfully built 60c5c8e03fa8
Successfully tagged tutorialflix/soapservice:latest

Now, we can identify our newly built image.

Terminal
1
2
3
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tutorialflix/soapservice latest 60c5c8e03fa8 5 minutes ago 543MB

Running a Docker image

To run the docker image, we’ll use run command. Use -p argument to supply the port on which container is accesible for the host machine, followed by the colon on which our application is listening.

Terminal
1
$ docker run -p3000:8080 tutorialflix/soapservice

After triggering this container will start in the terminal window. To run it in the detached mode, use argument -d with command

Terminal
1
$ docker run -p3000:8080 -d tutorialflix/soapservice

Now, our application is up and running on the port 3000. Ensure this by using this url http://localhost:3000/ws/customer.wsdl

Download the from Github.

If you face any issues, do let me know in the command section.

Author

Vaibhav Bhootna

Posted on

2020-11-01

Updated on

2020-11-02

Licensed under

Comments