How to build docker environment with Gradle for intelliJ

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.

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: 
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

Recommended Posts

How to build docker environment with Gradle for intelliJ
How to build Rails 6 environment with Docker
[Rails] How to build an environment with Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Build docker environment with WSL
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to build Docker + Springboot app (for basic learning)
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
How to build Java development environment with VS Code
[Error resolution] Occurs when trying to build an environment for spring with docker
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Environment construction with Docker for beginners
Build docker + laravel environment with laradock
How to build CloudStack using Docker
How to start Camunda with Docker
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to share files with Docker Toolbox
Build a PureScript development environment with Docker
I tried to build the environment of PlantUML Server with Docker
How to install Pry after building Rails development environment with Docker
Build a Wordpress development environment with Docker
Build a development environment for Django + MySQL + nginx with Docker Compose
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build TensorFlow operation check environment with Docker
How to run Blazor (C #) with Docker
[For those who create portfolios] How to use binding.pry with Docker
How to execute with commands of normal development language in Docker development environment
01. I tried to build an environment with SpringBoot + IntelliJ + MySQL (MyBatis) (Windows10)
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to build an environment for any version of Ruby using rbenv
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Build a development environment for Docker + Rails6 + Postgresql
[Java] How to test for null with JUnit
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
Build Spring Boot project by environment with Gradle
How to give your image to someone with docker
How to build API with GraphQL and Rails
Build a development environment for Docker, java, vscode
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
How to build a Pytorch environment on Ubuntu
How to install Docker
How to build android-midi-lib
How to use Gradle
How to output jar with main class specified by gradle in Intellij IDEA
Build a development environment to create Ruby on Jets + React apps with Docker
Cache Gradle dependent files to speed up docker build
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
How to deal with No template for interactive request
[Note] Build a Python3 environment with Docker in EC2
How to delete untagged images in bulk with Docker
(For myself) Build gitlab with ubuntu18.04 + docker for home (Note)