How to develop Java 8 Spring Boot application in Docker?

In this article, I'll show you how to use Java 8 to develop and run simple Spring Web applications without having to install Java 8 on your local machine.

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 environments. At this point, Docker will help us.

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

Once the Docker toolkit is installed, there is no need to install Java 8 or MySQL as required in our sample application.

First, let's check the 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 on 'net spring db'. The first is called 'springappserver' and is configured using springapp.dockerfile. The second is named mysqldbserver and configured with mysqldb.dockerfile.
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 is inherited from the Java 8 image of the 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 <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 the Docker Hub. On the MySQL image, I put my DB schema creation scripts 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 starts from the src / com / turkcell / softlab / Application.java file. Our only Controller is the personal Controller (src / com / turkcell / softlab / controller / PersonController.java).
You can run the entire project with simple commands:

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

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

Keywords: Java Docker MySQL Maven

Added by Pellefant on Thu, 24 Oct 2019 12:10:40 +0300