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/
Finally, click * GENERATE * at the bottom to unzip the downloaded zip file and rename it to a suitable directory name.
** docker-compose ** does not appear in initializr, so add it to ** build.gradle **.
In ** Open or Import **, select the renamed directory mentioned above.
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
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
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 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
root@149c67cc70d5:/#
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