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

How to modify the default storage location of Docker images (solution)

Due to the initial partitioning of the system, th...

How to import Chinese data into csv in Navicat for SQLite

This article shares with you the specific method ...

VMware Workstation Pro installs Win10 pure version operating system

This article describes the steps to install the p...

The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands

What does Ctrl+c, Ctrl+d, Ctrl+z mean in Linux? C...

MySQL 8.0.13 decompression version installation graphic tutorial under Windows

This article shares with you the MySQL 8.0.13 ins...

Detailed explanation of Nginx proxy_redirect usage

Today, I encountered a little problem when I was ...

js to upload pictures to the server

This article example shares the specific code of ...

WeChat applet realizes taking photos and selecting pictures from albums

This article shares the specific code for WeChat ...

Detailed introduction to JS basic concepts

Table of contents 1. Characteristics of JS 1.1 Mu...

Linux lossless expansion method

Overview The cloud platform customer's server...

mysql error number 1129 solution

SQLyog connects to mysql error number 1129: mysql...

How to use less in WeChat applet (optimal method)

Preface I am used to writing less/sass, but now I...