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
--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
I don't know why SpringBoot, so I will make RestApi locally as a 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.
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.
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 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)
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.
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
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"
}
}
}
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