[JAVA] Create a Spring Boot development environment with docker

Introduction

As a hobby development, the team talked about creating a web service using Spring Boot. I understand java, but my knowledge of Spring Boot is 0, so I tried to make a REST API using docker. In creating it, I referred to the following article.

-Spring Boot with Docker development environment -Connecting Spring Boot Docker container and MySQL Docker container

The development environment is as follows

Click here for the source of this article

What to make

スクリーンショット 2018-03-04 22.02.53.png

--DB container uses existing MySQL image --The APP container creates a DockerFile and mounts the file, eliminating the need to create a new image each time you build. --Myadmin container is for easy operation of MySQL

Build Spring Boot

I don't know why SpringBoot, so I will make RestApi locally as a preparation.

DB preparation

Put the necessary data in the mysql container and start it up. If you do not do this, the test will not pass and you will not be able to build. What you need to do when starting the container is as follows

--If you create a sql file in advance and mount it on /docker-entrypoint-initdb.d, it will be executed at startup, so mount it. --Create a mysql user sboot for Spring Boot --Link to port 3306 on localhost

When these are added, the command becomes as follows. docker container run -v $(PWD)/sql:/docker-entrypoint-initdb.d -d -e MYSQL_DATABASE=mydb -e MYSQL_USER=sboot -e MYSQL_PASSWORD=sboot -e MYSQL_ROOT_PASSWORD=root-pass -p 3306:3306 -d mysql

It will be a long time, but let's put up with it until we make docker-compose.

Create build.gradle

SpringBoot seems to use gradle for build and library management. Fill in the required items in SPRING INITIALIZR and drop the template. Edit the build.gradle file at the top of the directory as follows.

buildscript {
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'boot-get-started'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile('mysql:mysql-connector-java:6.0.6')
    compileOnly('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

It's convenient to fill in the required library in the dependencies. I'm glad that you're probably testing at testCompile. I want to learn more about it later.

gradlew clean build completes the library installation.

Creation of Entity and Repository

Create Enttity and Repository for DB in src / main / java / com / example. I don't really understand this area because I just copied the reference article. This time, the main focus is on building the environment, so I will divide it.

Create application.yml

Create src / main / resources / application.yml. Enter the DB connection destination here. Note that the connection destination changes depending on whether the application is executed locally or connected on the container.

spring:
  profiles:
    active: localhost
---
spring:
  profiles: localhost
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mydb
    username: sboot
    password: sboot
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true
    hibernate:
      ddl-auto: update
  data:
    rest:
      base-path: /api

---
spring:
  profiles: docker
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://dbserver/mydb
    username: sboot
    password: sboot
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true
    hibernate:
      ddl-auto: update
  data:
    rest:
      base-path: /api

Create a profile corresponding to localhost and docker and set localhost as the default. Profile switching is done in DockerFile (described later)

Build

Do gradlew build with the mysql container set up, and if BUILD SUCCESS FUL appears, it's OK. Now that the build is successful, it's time to containerize the application and run it.

Creating DockerFile and Compose

First, create a DockerFile for your application.

# use alpine as base image
FROM ubuntu:16.04

RUN apt-get update
RUN apt-get -y install openjdk-8-jdk
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64

# recommended by spring boot
VOLUME /tmp

# create directory for application
RUN mkdir /app
WORKDIR /app

# jar target
ENV JAR_TARGET "boot-get-started-0.0.1-SNAPSHOT.jar"

# set entrypoint to execute spring boot application
ENTRYPOINT ["sh","-c","java -jar -Dspring.profiles.active=docker build/libs/${JAR_TARGET}"]

--Switch to the profile described in ʻapplication.yml with -Dspring.profiles.active = docker of ʻENTRY POINT. --Mount the file built at runtime on / app. --Execute the file specified in JAR_TARGET. (By default "boot-get-started-0.0.1-SNAPSHOT.jar")

Next, write docker-compose.yml which is a combination of db, app and phpmyadmin containers.

version: '2'
services:

    dbserver:
        image: mysql
        volumes:
            - ./sql:/docker-entrypoint-initdb.d
            - mysql-db:/var/lib/mysql
        environment:
            MYSQL_DATABASE: mydb
            MYSQL_USER: sboot
            MYSQL_PASSWORD: sboot
            MYSQL_ROOT_PASSWORD: root
           
    app:
        build: .
        image: watari/boot:0.1.0                            
        depends_on:
            - dbserver
        ports:
            - "8080:8080"
        volumes:
            - .:/app
        environment:
            JAR_TARGET: boot-get-started-0.0.1-SNAPSHOT.jar

    myadmin:
        image: phpmyadmin/phpmyadmin
        depends_on:
            - dbserver
        environment:
            PMA_ARBITRARY: 1
            PMA_HOST: dbserver
            PMA_USER: root
            PMA_PASSWORD: root
        ports:
            - "1111:80"

volumes:
    mysql-db:
        driver: local

Run

It can be executed with docker-compose up. The first time you need to create an image from a Docker file, it's a good idea to add the --build option. After launching the container and executing it, check it with the curl command.

$ curl http://localhost:8080/api/users
{
  "_embedded" : {
    "users" : [ {
      "firstName" : "Taro",
      "lastName" : "Yamada",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/api/users/1"
        },
        "user" : {
          "href" : "http://localhost:8080/api/users/1"
        }
      }
    }, {
      "firstName" : "Hanako",
      "lastName" : "Tanaka",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/api/users/2"
        },
        "user" : {
          "href" : "http://localhost:8080/api/users/2"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/users"
    },
    "profile" : {
      "href" : "http://localhost:8080/api/profile/users"
    }
  }
}

in conclusion

I don't have any knowledge of Spring Boot, but I was able to easily build an environment by using a container. However, since the build is done locally, I feel that it is something, so it is better to let the container do the build as well? I also think. Also, I'm developing based on emacs now, but I think it's necessary to combine it well when developing in an integrated development environment such as eclipse in the future (I don't have any know-how here). I think I'll use this as a starting point for trial and error.

Recommended Posts

Create a Spring Boot development environment with docker
Create Spring Boot-gradle-mysql development environment with Docker
Create a Vue3 environment with Docker!
Create Spring Boot development environment on Vagrant
Build a PureScript development environment with Docker
Create a MySQL environment with Docker from 0-> 1
Build a Wordpress development environment with Docker
I tried to create a padrino development environment with Docker
Spring Boot environment construction with Docker (January 2021 version)
Create a website with Spring Boot + Gradle (jdk1.8.x)
[Memo] Create a CentOS 8 environment easily with Docker
Create a simple search app with Spring Boot
Create Spring Boot environment with Windows + VS Code
Create a web api server with spring boot
Create microservices with Spring Boot
Create a Spring Boot app development project with the cURL + tar command
Create a java web application development environment with docker for mac part2
[Note] Create a java environment from scratch with docker
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
Build a Node.js environment with Docker
Create an app with Spring Boot 2
Create SolrCloud verification environment with Docker
Create an app with Spring Boot
Create Laravel environment with Docker (docker-compose)
Build a development environment to create Ruby on Jets + React apps with Docker
Spring Boot gradle build with Docker
I tried to create a java8 development environment with Chocolatey
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Create a web environment quickly using Docker
Laravel development environment construction with Docker (Mac)
Create Rails 6 + MySQL environment with Docker compose
Create a simple on-demand batch with Spring Batch
Let's create a Java development environment (updating)
[Docker] Create Node.js + express + webpack environment with Docker
Start web application development with Spring Boot
Lightweight PHP 7.4 development environment created with Docker
Build a simple Docker + Django development environment
[SAP] Create a development environment with NW AS ABAP Developer Edition (1)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
I tried to create a Spring MVC development environment on Mac
Build a development environment for Django + MySQL + nginx with Docker Compose
Build a development environment for Docker + Rails6 + Postgresql
Let's get started with Java-Create a development environment ②
Let's get started with Java-Create a development environment ①
Create a Privoxy + Tor environment instantly using Docker
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Build a Laravel / Docker environment with VSCode devcontainer
Create a Spring Boot application using IntelliJ IDEA
Spring boot development-development environment-
I tried to build a Firebase application development environment with Docker in 2020
A note about trying Oracle 11g + Spring boot with Vagrant + Docker compose
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
Build a simple Docker Compose + Django development environment
Build Spring Boot project by environment with Gradle
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
[Win10] Build a JSF development environment with NetBeans
Create your own Utility with Thymeleaf with Spring Boot