Dockerizing B/E API’s – Spring boot application
This blog demonstrates the basic steps to dockerize a spring boot REST API, it uses a simple spring boot application. Except the application specific dependencies, integrations and configurations, the core procedure remains the same for any spring application.
Here we use a simple Spring boot application and package it as a docker image on developer’s machine. Once image is built, it will run on local as a docker container.
The sample spring boot application is a counter REST API having 2 endpoints, first increments the counter and second one to fetch the value of counter. The application is targeted to run on port 8099
Below is the list of pre-requisites required:
- Java 8
- Docker Desktop installation
Docker installation is available in 2 type – Docker Engine and Docker Desktop. The recommended approach is to install Docker Desktop as it includes Docker Engine, Docker CLI, Docker Compose as out of the box installation. Installation instructions can be found here.
- Eclipse – enabled with Spring starters or any Other IDE
Spring boot application structure looks like below:
To package this application as a docker image, we need to create a dockerfile. The contents of docker file are
- FROM: This instruction specifies the base image on which we intend to run our application. This is usually the first statement in dockerfile. In the above example we are using ‘8-jdk-alpine’ tag of openjdk image from docker hub as our base image – https://hub.docker.com/_/openjdk. This image has open jdk 8 preinstalled on Linux OS.
- RUN: This instruction executes commands on the base image, this helps us configure the base image. In our case we run ‘apk update’, ‘apk add bash’ & ‘apk add curl’ commands as they help in debugging issues once the docker container is running
- EXPOSE: This instruction informs Docker that the container listens on the specified network ports at runtime, in our case as the spring boot application runs on 8099 port we have exposed 8099 port
- ADD: Here we copy the maven packaged spring boot runnable to filesystem of image
- ENV: This instruction helps set environment variables; in the current example we use it to set JAVA_OPTS environment variable. Spring boot application properties can be externalized using OS environment variable or Java system properties, ‘ENV’ instruction is handy for the same.
- ENTRYPOINT: Helps in configuring the container. In our case we set the execution commands to be run once the container is up, here we run the spring boot executable jar
Building the image: Run the below command on command prompt or terminal
“mvn clean install && docker image build . -t demo/samplespringboot”
The above command actually fires 2 commands in succession –
- Builds the spring boot application using maven
- Creates a docker image & tags it as ‘demo/samplespringboot’ and saves the image on local repository
To verify whether the image is built properly run ‘docker image list‘ command and check if the image with ‘demo/samplespringboot’ exists in the list.
Running the image: We run the image with below command
“docker run -p 8080:8099 demo/samplespringboot”
In the above command ‘-p’ Publish a container’s port(s) to the host, i.e. 8080 on the post is published to 8099 on container so that requests on 8080 localhost are directed to 8099 on running container
To verify if the container is running or now fire “docker ps” command and the image should be in the list
We can test our endpoints running the below cURL commands
- curl –location –request PUT ‘http://localhost:8080/count/increment’
- curl –location –request GET ‘http://localhost:8080/count’
Running in detached mode: ‘docker run -d -p 8080:8099 demo/samplespringboot ‘. Once the docker runs in detached mode it returns Container_id which can be used to fetch logs & stop the container
Fetching logs of running container: “docker logs ‘CONTAINER_ID’”. Example – docker logs 3d7ff3d032bb97022c5d77a86916a71cdea9fdcbf195d95328ccb881abe2d83d
Stopping a running container: “docker stop ‘CONTAINER_ID’”. Example – docker stop 3d7ff3d032bb97022c5d77a86916a71cdea9fdcbf195d95328ccb881abe2d83d
The commands and instructions mentioned in this article are the basic ones to get a developer started with docker. The detailed description and usage of all docker CLI commands can be found at https://docs.docker.com/engine/reference/commandline/docker/
Docker images can be published to a centralized remote repository. Docker hub is a widely used open repository, most cloud providers provide private docker repositories specific to an enterprise cloud account.