Method of developing Java 8 Spring Boot application program in Docker
- 2021-07-09 09:31:24
- OfStack
In this article, I'll show you how to use Java 8 to develop and run a simple Spring Web application without having to install Java 8 on your local computer.
Python developers use virtual environments to create and manage separate environments for different projects, each using a different version of Python to execute, store, and resolve Python dependencies. Java and many other technologies do not support the virtual environment concept. At this point, Docker comes to help us.
Docker is a virtualization platform. You can find the basic information and installation guide from the official website of Docker.
1 Once the Docker toolkit is installed, there is no need to install Java 8 or MySQL required in our sample application.
First, let's check 1
Docker-compose
Documents:
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 is named 'springappserver' and uses the
springapp.dockerfile
Configuration. The second is named mysqldbserver and uses the
mysqldb.dockerfile
Configure.
Now, let's look at springapp. dockerfile:
#
# Java 1.8 & Maven Dockerfile
#
#
# pull base image.
FROM java:8
# maintainer
MAINTAINER Dursun KOC "dursunkoc@gmail.com"
# update packages and install maven
RUN \
export DEBIAN_FRONTEND=noninteractive && \
sed -i 's/# \(.*multiverse$\)/\1/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 inherits from the Java 8 image of Docker Hub. On the Java 8 image, I installed vim, wget, curl, Maven, and set up volumes to house my existing project code. Finally, execute the Maven command to run my application.
Now let's check 1 mysqldb. dockerfile:
FROM mysql/mysql-server
MAINTAINER Dursun KOC <dursunkoc@gmail.com>
# 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 inherits from the MySQL/mysql-server image of Docker Hub. On the MySQL image, I placed 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 derived from
src / com / turkcell / softlab / Application.java
At the beginning of the file, our only 1 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 the 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 "
Summarize