How to develop Java 8 Spring Boot applications in Docker

How to develop Java 8 Spring Boot applications in Docker

In this article, I will show you how to develop and run a simple Spring web application using Java 8 without having Java 8 installed on your local machine.

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 our aid.

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

Once you have the Docker Toolbox installed, you do not need to install Java 8 or MySQL which are required in our sample applications.

First, let's check Docker-compose file:

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 each on 'net-spring-db'. The first is called 'springappserver' and is configured using springapp.dockerfile . The second one is named mysqldbserver and is configured using mysqldb.dockerfile .

Now, let’s take a look at the 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$\)/\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 a Docker image that inherits from the Java 8 image from Docker Hub. On the Java 8 image, I installed vim, wget, curl, Maven, and set up volumes to house my existing project code. Finally, I execute the Maven command to run my application.

Now let's check the 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/<yyyy-MM-dd> /docker-entrypoint-initdb.d/

The Docker file configures a Docker image that inherits from the MySQL/mysql-server image from Docker Hub. On the MySQL image I placed my db-schema creation scripts, they are in the MySQL folder. I have a SQL file in this folder - data.sql - to create the 'person' table.

Now, let's look at the application structure.

Our application is started in the src / com / turkcell / softlab / Application.java file and our only Controller is the PersonController (src/com/turkcell/softlab/controller/PersonController.java).

You can run the entire project with a simple command:

docker-compose up -d

To test, use the following two commands on your local computer:

• Create a new person:

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 existing people in the database:

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

Summarize

The above is the method I introduced to you for developing Java 8 Spring Boot applications in Docker. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to start a Java program in docker
  • Detailed explanation of how to use Docker to build a simple Java development and compilation environment
  • Steps to build a Java environment using Docker
  • How to use Docker, a Java data development auxiliary tool, and ordinary programs

<<:  JS implements sliding up and down on the mobile terminal one screen at a time

>>:  MySQL binlog opening steps

Recommend

Analysis and solution of Chinese garbled characters in HTML hyperlinks

A hyperlink URL in Vm needs to be concatenated wit...

How to change the mysql password on the Xampp server (with pictures)

Today, I found out while working on PHP that if w...

Introduction to HTML page source code layout_Powernode Java Academy

Introduction to HTML page source code layout This...

5 Reasons Why Responsive Web Design Isn’t Worth It

This article is from Tom Ewer's Managewp blog,...

Hyperlink icon specifications: improve article readability

1. What is the hyperlink icon specification ?<...

MySQL database Load Data multiple uses

Table of contents Multiple uses of MySQL Load Dat...

Implementation of Docker cross-host network (overlay)

1. Docker cross-host communication Docker cross-h...

Detailed explanation of MySQL multi-table join query

Table of contents Multi-table join query Inner Jo...

Develop a vue component that encapsulates iframe

Table of contents 1. Component Introduction 2. Co...

How webpack implements static resource caching

Table of contents introduction Distinguish betwee...

JavaScript implementation of the Game of Life

Table of contents Concept Introduction Logical ru...