The method of developing Java 8 spring boot application in docker

Time:2021-5-12

In this article, I’ll show you how to develop and run a simple spring web application using java 8 without installing Java 8 on your local computer.

Python developers use virtual environments to create and manage separate environments for different projects. Each environment uses different versions of Python to execute, store and parse Python dependencies. Java and many other technologies do not support the concept of virtual environment. Docker is here to help us.

Docker is a virtualization platform. You can find the basic information and installation guide from docker official website.

Once the docker toolkit is installed, there is no need to install Java 8 or mysql, which is required in our sample application.

First, let’s checkDocker-composeFile:


version : '2'
services:
 springappserver:
  build:
   context: . 
   dockerfile: springapp.dockerfile
  ports: 
   - "8080:8080"
  networks:
   - net-spring-db
  volumes:
   - .:/vol/development
  depends_on:
   - mysqldbserver
 mysqldbserver:
  build:
   context: . 
   dockerfile: mysqldb.dockerfile
  ports:
   - "3306:3306"
  networks:
   - net-spring-db
  environment:
   MYSQL_DATABASE: testdb
   MYSQL_USER: myuser
   MYSQL_PASSWORD: mypassword
   MYSQL_ROOT_PASSWORD: myrootpassword
  container_name: mysqldbserver
networks:
 net-spring-db:
  driver: bridge

We have two servers on ‘net spring db’. The first one is called ‘springappserver’ and usesspringapp.dockerfileto configure. The second one is named mysqldbserver and uses themysqldb.dockerfileTo configure.

Now, let’s take a look at springapp.dockerfile:


#
# Java 1.8 & Maven Dockerfile
#
#
# pull base image.
FROM java:8
# maintainer
MAINTAINER Dursun KOC "[email protected]"
# update packages and install maven
RUN \
 export DEBIAN_FRONTEND=noninteractive && \
 sed -i 's/# \(.*multiverse$\)//g' /etc/apt/sources.list && \
 apt-get update && \
 apt-get -y upgrade && \
 apt-get install -y vim wget curl maven
# attach volumes
VOLUME /vol/development
# create working directory
RUN mkdir -p /vol/development
WORKDIR /vol/development
# maven exec
CMD ["mvn", "clean", "package", "exec:java"]

The docker file configures the docker image, which is inherited from the Java 8 image of docker hub. On the Java 8 image, I installed VIM, WGet, curl, maven, and set the volume to place my existing project code. Finally, execute the Maven command to run my application.

Now let’s check mysqldb.dockerfile:


FROM mysql/mysql-server
MAINTAINER Dursun KOC <[email protected]>
# Copy the database initialize script: 
# Contents of /docker-entrypoint-initdb.d are run on mysqld startup
ADD mysql/ /docker-entrypoint-initdb.d/

The docker file configures the docker image, which is inherited from the MySQL / MySQL server image of docker hub. On the MySQL image, I put my DB schema creation scripts in the MySQL folder. I have an SQL file in this folder – data. SQL – to create the ‘person’ table.

Now let’s look at the application structure.

Our application is fromsrc / com / turkcell / softlab / Application.javaAt the beginning of the file, our only controller is personcontroller (SRC / COM / turkcell / softlab / controller / personcontroller. Java).

You can run the entire project with a simple command:

docker-compose up -d

When testing, use the following two commands on your local computer:

Create new people:


curl -H “Content-Type:application / json” -X POST -d “{\”first \“:\”Mustafa \“,\”last \“:\”KOÇ\“,\”dateofbirth \“381110400000 ,“placeofbirth”:\“Erzincan \”}“ ”http://192.168.99.100:8080/people“

List the existing people in the database:


curl -H “Content-Type:application / json” -X GET “http://192.168.99.100:8080/people”

summary

The above is the method of developing Java 8 spring boot application in docker introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!
If you think this article is helpful to you, please reprint, please indicate the source, thank you!