[Java] How to build docker environment with Gradle for intelliJ

2 minute read

spring-boot-doma2-sample I referred to the repository.
** build.gradle ** has been written in a new way.

Spring Initializr

First, create a spring application at the following site.
https://start.spring.io/

Add the plugins you want to use in your application to Dependencies.

  • Spring boot DevTools
  • Spring Web
  • Thymeleaf
  • Spring Data JDBC
  • Flyway Migration
  • MySQL Driver

Finally, click * GENERATE * at the bottom to unzip the downloaded zip file and rename it to a suitable directory name.

スクリーンショット 2020-06-01 22.51.44.png

** docker-compose ** does not appear in initializr, so add it to ** build.gradle **.

Open a project in intelliJ

In ** Open or Import **, select the renamed directory mentioned above.
スクリーンショット 2020-06-01 23.10.05.png

Modify build.gradle

Open ** build.gradle ** in your project.
Since it is as follows, please add the // add part.

build.gradle


plugins {
	id 'java'
	id 'org.springframework.boot' version '2.3.1.BUILD-SNAPSHOT'
	id 'io.spring.dependency-management' version '1.0.9.RELEASE'
	id 'com.avast.gradle.docker-compose' version '0.12.1' //add to
	id 'org.flywaydb.flyway' version '6.4.3'
}

// ...Abbreviation

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.flywaydb:flyway-core'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	runtimeOnly 'mysql:mysql-connector-java'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}

Site to check gradle plugins → Search Gradle plugins

スクリーンショット 2020-06-01 22.45.33.png

スクリーンショット 2020-06-01 22.46.05.png

docker settings

Create a ** docker ** directory in your project and create ** docker-compose.yml **.

docker-compose.yml


version: '2'
services:
    todo-db:
        build: ./mysql  #Specify the directory where the Dockerfile is located
        environment:
            - MYSQL_DATABASE=todo
            - MYSQL_ROOT_USER=root
            - MYSQL_ROOT_PASSWORD=passw0rd
            - TZ=Japan
        image: docker_todo-db
        container_name: todoDb
        ports:
            - "33068:3306"  #If you are already using 33068, change it to a different number

Create a ** mysql ** directory and create ** Dockerfile ** and ** my.cnf **.

Dockerfile


FROM mysql:5.7

RUN /bin/cp -f /etc/localtime /etc/localtime.org
RUN /bin/cp -f /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

COPY ./my.cnf /etc/mysql/conf.d/

RUN mkdir -p /var/log/mysql
RUN chown mysql.mysql /var/log/mysql

my.cnf


[mysqld]
default-storage-engine=InnoDB
user=mysql
character-set-server=utf8mb4
skip-character-set-client-handshake

;general_log=1
;general_log_file=/var/log/mysql/query.log
long_query_time=3
slow_query_log_file=/var/log/mysql/slow-query.log

max_allowed_packet=16MB
innodb_file_per_table
innodb_buffer_pool_size=64M
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=2M
group_concat_max_len=2048

[mysqld_safe]
log-error=/var/log/mysqld.log

[mysql]
default-character-set=utf8mb4

Start Docker

Open a terminal in intelliJ and run ./gradlew compose Up.
If you see a log like the one below, you’re done.

> Task :composeUp
Building todo-db

...abridgement

TCP socket on localhost:33068 of 'todoDb' is ready
+--------+----------------+-----------------+
| Name   | Container Port | Mapping         |
+--------+----------------+-----------------+
| todoDb | 3306           | localhost:33068 |
+--------+----------------+-----------------+

BUILD SUCCESSFUL in 15s

Check the table

Check if the table is created in MySQL in the docker container.

Use the IntelliJ terminal.
Run docker ps. Confirm that the running container is displayed as shown below.

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
149c67cc70d5        docker_todo-db      "docker-entrypoint.s…"   17 seconds ago      Up 15 seconds       33060/tcp, 0.0.0.0:33068->3306/tcp   todoDb

Run docker exec -it todoDb / bin / bash to get inside the container.
It becomes root @ <CONTAINER ID>: / #, and you can see that it is inside the container.

$ docker exec -it todoDb /bin/bash
[email protected]:/#

Access MySQL with mysql -u root -p.
You will be asked for a password, so enter the “** passw0rd **” specified in ** docker-compose.yml ** here.

MYSQL_ROOT_PASSWORD=passw0rd

$ mysql -u root -p
Enter password: 
    • Cursor etc. are not displayed on mac *
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| todo               |
+--------------------+
5 rows in set (0.00 sec)

A table called ** todo ** has been created properly.

In the next article, I would like to automate migration using ** flyway-core **.
Next article ⇨ migration automation article