In this tutorial, i am going to show you how to develop an Spring Boot REST Api application that runs on docker container. This is just a brief and quick demo of setting up spring boot application with docker. In this article, i have focused only on showing the steps of integrating docker support (for building and running image) for the spring boot web application.
Project Structure and Source Code
The fully source code of the application can be found at GitHub. Click here to download. The project file structure will be as follows.
Here is the implementation of the WelcomeController.java
package com.springbootdev.examples.api.controller; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
@RequestMapping(value = "/api") | |
public class WelcomeController | |
{ | |
@GetMapping("/welcome") | |
public String sayHello(){ | |
return "Welcome to http://www.SpringBootDev.com"; | |
} | |
} |
Dockerfile
Dockerfile contains the command and instructions for building the docker image from the project. The contents of the Dockerfile related to this project, can be given as follows.
FROM java:8 | |
LABEL maintainer=“chathuranga.t@gmail.com” | |
WORKDIR /app | |
COPY target/spring-boot-rest-api-docker-0.0.1-SNAPSHOT.jar /app/spring-boot-app.jar | |
ENTRYPOINT ["java","-jar","spring-boot-app.jar"] |
FROM java:8
java8 will be identified as the base image for this application. Therefore the final docker image for this application should be built based on java8 docker image. (in other words, in order to run this application, java8 docker image is required)
WORKDIR /app
working directory has been set as the /app. This directory will be created in the container and run the specified commands from this directory.
COPY
The copy command will copy the file from local project environment to docker image being built. The file target/spring-boot-docker-example-0.0.1-SNAPSHOT.jar in the local project environment will be copied as /app/spring-boot-app.jar.
ENTRYPOINT
The specified command will be executed once the docker image is successfully deployed and container is booted up.
docker-compose.yml
docker-compose is a utility/tool that is used to run multi container docker applications. docker-compose utility will read the docker-compose.yml file for setting up the related services for the application. This file should contains the declaration of the services that are required to run the application. If you need to run any service as a separate docker container, then you should declare it in the docker-compose.yml file.
The content of the docker-compose.yml file related to this project can be shown as follows.
version: '3' | |
services: | |
spring-boot-rest-api-app: | |
image: spring-boot-rest-docker-image | |
build: | |
context: ./ | |
dockerfile: Dockerfile | |
ports: | |
– 8087:8080 | |
volumes: | |
– /data/spring-boot-app |
The document complies with docker-compose document version 3.
The service name is “spring-boot-rest-api-app” and image name is “spring-boot-rest-docker-image“. The service should be deployed form the given image and if the image does not exist, it should be built with the Dockerfile available in the current working directory.
The port 8080 of the docker container should be mapped to the port 8087 of the docker host. So the service can be externally accessed with port 8087.
spring-boot-rest-api-app container will use the /data/spring-boot-app volume for managing data.
Building the project with maven
Since the Dockerfile depends on the final built artifact of the project (that is target/spring-boot-rest-api-docker-0.0.1-SNAPSHOT.jar), we need to build final deployable artifact before moving forward with building the docker image. This can be done with following command.
mvn clean install
Now the project is successfully built and we can move forward with building docker image and running it in a docker container.
Building the docker image
In terminal, go to the directory where your docker-compose.yml file is available. Then run the following command for building the docker image.
docker-compose build
This command can be used to build new image or rebuild existing images. That means if there is no docker image available for the given name, then it will directly build the image. Otherwise the existing image (already available image for the given name) will be removed and rebuild the image.
you can get a list of docker images available in the docker platform with following command and verify wether the image has been successfully built.
docker images
you can notice that the “spring-boot-rest-docker-image” is successfully built and available under the list of images.
Running application with docker-compose
This can be done with following command.
docker-compose up
After executing the above command, it will look for the services declared in the docker-compose.yml file and deploy and start each service in separate docker container.
Now, we should be able to access the REST api endpoint available in the WelcomeController.
GET /api/welcome