Database environment construction with Docker in Spring boot (IntellJ)

Database linkage with Spring boot + docker!

First article. Please point out a lot.

When I thought about creating a portfolio with Spring boot, I took a lot of time to build a DB with docker, so I made a note.

This goal

Output the value of the database (Mysql) built using docker in the sample project to the screen

・ Target readers ...

docker beginner or Engineer with little development experience (1 or 2 years)

Perspective to focus on this time

・ How to create a docker file -Linkage between MySQL environment created on docker and spring boot project

What is docker

I will post an article by @ kotaro-dr. [Illustration] Understanding the whole picture of Docker -Part 1- ┗ The document is clean and easy to understand even for beginners [Illustration] Understanding the whole picture of Docker -Part 2- ┗ At the beginning of the second part, there will be a story about data management related to database persistence, so this time it's OK if you glance at at least that much. I will also post the second part. [Illustration] Understanding the whole picture of Docker -Part 2-

Why docker

・ Lighter than Vurtual box ← Details are omitted because the theme and focus are out of focus. ・ Development environment can be prepared immediately ┗ "It works on this device, but it doesn't work on my device." ┗ When asking a question to an acquaintance or using a QA service such as teratail, you can pass on your current environment to the other party as it is, and it will be easier for you to get an answer.

Let's do it right away

■ Environmental information Language: Java (jdk11) FW:Spring boot IDE:IntelliJ

This is the tree of the project to be created this time. (Sorry for the image)

スクリーンショット 2019-02-24 15.49.49.jpg

Until now, it was only links, but I would like to put out my own words soon.

Creating a project

This time, I will make a simple thing that outputs all the values in the database. It's a level that you can see by looking at it, so with a 3-minute cooking pattern, the result is as follows. spring-boot-docker(github)

About ʻapplication.properties`

spring.datasource.url=jdbc:mysql://localhost:3306/demo?characterEncoding=UTF-8&serverTimezone=JST 
#//demo is the name of this database
spring.datasource.username=root //User name
spring.datasource.password=p@ssw0rd  //password
spring.jpa.hibernate.ddl-auto=update  //Create if there is no table corresponding to Entity when starting the application

This is the minimum setting required for database linkage. Please note that it is necessary to match the contents of docker-compose.yml that will be created later.

Creating a docker file

This time, I will create a folder called docker in the same hierarchy as src and create related files in it. I am aware that there is no specific specification because I could not find a description about the location.

fockerfile



FROM mysql:8.0  #Specify the image to be acquired from docker hub

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

COPY: The left side of the command is the local side, and the right side is the docker image side. RUN: Can execute commands installed on the target image

docker-compose.yml


version: '3'
services:
    mysql:
        build: ./mysql
        environment:
            - MYSQL_DATABASE=demo
            - MYSQL_ROOT_USER=root
            - MYSQL_ROOT_PASSWORD=p@ssw0rd
            - TZ=Japan
        volumes:
            - ./initdb.d:/docker-entrypoint-initdb.d
            - ./dbdata:/var/lib/mysql
        ports:
            - "3306:3306"

↑ When separated by a colon (:)

-The left side shows the path on the host side, and the right side shows the path on the container side.
-Initial data input-> Put the sql file to be read first in the initdb.d directory
Persistence → Every time you process dbdata, files equivalent to data will come in more and more

Let's start docker-compose

Right-click docker-compose.yml on your project and select "Play". スクリーンショット 2019-03-01 22.49.47.jpg Then, interpreting the dockerfile and docker-compose.yml that I wrote earlier, the environment construction will start. When you see 'Compose: docker' has been deployed successfully. as shown below, you are done. スクリーンショット 2019-03-01 22.53.15.jpg

Now, let's go into the container and check if the database is complete. In the terminal (command prompt for Windows), change to the project directory. inside that Check the current operating status of the container with the docker ps command. The figure below is after execution スクリーンショット 2019-03-02 0.18.36.jpg

You can see that the image of mysql is created. Use this CONTAINER ID or NAMES to start. This time I will start it using the CONTAINER ID.

Enter the container with docker exec -it [CONTAINER ID] bash. root @ 37c06170b19b: / # If the terminal (command prompt) looks like this, it is included. From this screen, connect to Mysql as the root user. mysql -u root This time, the password is p @ ssw0rd. Connect to mysql as below スクリーンショット 2019-03-02 0.40.51.jpg

Specify this sample database with ʻuse demo. After that, check sql properly. Since the ʻuser table is included as the initial data. select * from user; スクリーンショット 2019-03-02 0.46.40.jpg I was able to check the contents of the table.

After that, if you run your own shabby sample project from Docker Application ... We have succeeded in linking with the database as shown below. スクリーンショット 2019-03-02 0.52.29(2).jpg

If you use docker in this way, you can pass the entire environment to the other party, so you do not have to bother to set up the database and input data to the other party. The knowledge of docker is still shallow, but I think that it will be used more and more in the future, so I would like to actively use it.

Next time, I would like to try the authentication screen with Spring boot.

Recommended Posts

Database environment construction with Docker in Spring boot (IntellJ)
Spring Boot environment construction with Docker (January 2021 version)
Create a Spring Boot development environment with docker
[Spring Boot] Environment construction (macOS)
[Docker] Rails 5.2 environment construction with docker
React environment construction with Docker
Node.js environment construction with Docker Compose
◆ Spring Boot + gradle environment construction memo
Environment construction with Docker for beginners
Database linkage with doma2 (Spring boot)
[Environment construction with Docker] Rails 6 & MySQL 8
Spring Boot gradle build with Docker
GPU environment construction with Docker [October 2020 version]
Rails environment construction with Docker (personal apocalypse)
Spring Boot environment construction memo on mac
Laravel development environment construction with Docker (Mac)
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Edit Mysql with commands in Docker environment
Create Spring Boot-gradle-mysql development environment with Docker
Laravel + MySQL + phpMyadmin environment construction with Docker
Try Spring Boot 1 (Environment construction ~ Tomcat startup)
[Spring] Environment construction
Docker environment construction
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Self-hosting with Docker of AuteMuteUs in Windows environment
Environment construction command memo with Docker on AWS
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Build Spring Boot project by environment with Gradle
DB environment construction with DBFlute Intro + H2 Database
React + Django + Nginx + MySQL environment construction with Docker
Create Spring Boot environment with Windows + VS Code
Wordpress local environment construction & development procedure with Docker
Coexistence of Flyway in the embedded database (h2) of the development environment and the release database (SQL Server) with Spring Boot
Rails Docker environment construction
Spring boot development-development environment-
Download with Spring Boot
Include external jar in package with Spring boot2 + Maven3
Switch environment with Spring Boot application.properties and @Profile annotation
[Note] Build a Python3 environment with Docker in EC2
Until you start development with Spring Boot in eclipse 1
How to boot by environment with Spring Boot of Maven
Until you start development with Spring Boot in eclipse 2
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
Pytorch execution environment with Docker
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
MySQL 5.7 (Docker) environment construction memo
Spring Boot 2 multi-project in Gradle
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
JavaFX environment construction in Java 13
Redmine (Docker) environment construction memo
Error in Spring database connection
Hello World with Spring Boot
Major changes in Spring Boot 1.5
Getting Started with Spring Boot