Create Spring Boot-gradle-mysql development environment with Docker

Create Spring Boot-gradle-mysql development environment with Docker

Gradle environment to container for CentOS using Docker Build a mysql environment in a DB container Allow Spring Boot to run in a virtual environment

Constitution

container CentOS container: java8 / gradle / mysql Mysql container: Mysql

Start Spring Boot on CentOS container, access db of Mysql container to get DB data, and then display DB data on host through tomcat port 8080.

Directory structure

docker-compose.yml centos Dockerfile gradle-project- [project-name] -gradle project files mysql-settings-empty folder -Sql-init.sql

github

Directory / file description

docker-compose.yml: docker-compose description for creating CentOS and Mysql container

centos-> Dockerfile: Docker file to start CentOS container

gradle-project-> [project-name]: Gradle project files of Spring Boot application

mysql-> settings: volume (/ var / lib / mysql) for persisting DB data of mysql -> sql-> init.sql: Initialization script when starting Mysql container

docker-compose.yml


version: "3"

services:
  centos:
    build: ./centos #Centos Dockerfile build settings(See below)
    container_name: boot-container #centos container name specification
    ports:
      - "8080:8080" #Port forwarding settings for communication between host OS containers
    volumes:
      - ./gradle-project:/gradle-project #Gradle project persistence settings
    links:
      - mysql # service[mysql]Allowing inter-container communication processing
    depends_on:
      - mysql #Set to create centos container after starting mysql container
    privileged: true #Grant administrator privileges by centos operation(Just in case, you don't have to worry about command operations.)
    tty: true #Set centos to keep running after container creation

  mysql:
    image: mysql:5.7
    container_name: boot-mysql
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci #mysql daemon command character code specification
    volumes:
      - ./mysql/sql:/docker-entrypoint-initdb.d #Place the initialization script in the container
      - ./mysql/settings:/var/lib/mysql #Mount the directory that stores the DB data group you want to persist
    environment: #Each parameter setting of mysql
      MYSQL_DATABASE: bootdb
      MYSQL_USER: boot
      MYSQL_PASSWORD: boot
      MYSQL_ROOT_PASSWORD: root

centos-> Dockerfile


#Get centos7
FROM centos:7

#Acquisition of command software performed in the container
RUN yum -y update
RUN yum -y install wget
RUN yum -y install unzip

# install mysql5.7
RUN yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# setting for mysql version 5.6 # mysql5.If you want to use 6, uncomment below
#  RUN yum-config-manager --disable mysql57-community
#  RUN yum-config-manager --enable mysql56-community

RUN yum install -y mysql mysql-server

# install java 
RUN yum install -y java-1.8.0-openjdk
RUN yum install -y java-1.8.0-openjdk-devel

# install gradle
RUN wget https://services.gradle.org/distributions/gradle-6.4.1-bin.zip
RUN unzip -d /opt/gradle /gradle-6.4.1-bin.zip
RUN rm -rf /gradle-6.4.1-bin.zip
RUN ls /opt/gradle/

# setting path # gradle/Define environment variables in advance so that you can operate java commands
ENV GRADLE_HOME /opt/gradle/gradle-6.4.1
ENV JAVA_HOME /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el7_8.x86_64
ENV PATH ${JAVA_HOME}/bin:${GRADLE_HOME}/bin:${PATH}

gradle-project -> [project-name]:

Place the gradle project file in the [project-name] directory Spring Initilizer output and Eclipse gradle project files Place it in this directory as it is

Since the persistence setting is made, the file operations in the host computer and the container are performed in conjunction with each other. Therefore, you can proceed with development with Eclipse etc. and perform build / jar execution operation on the container as it is.

mysql -> settings:

Stores files that store mysql DB data Before creating the container, it's an empty directory Data files etc. are added according to the mysql operation after creating the container

Data files created inside the container can be persisted volume is defined

mysql -> sql -> init.sql

Define initialization script when starting container This time, get the contents of the following data record from Spring Boot, I'm writing code to display


CREATE DATABASE IF NOT EXISTS bootdb;

CREATE TABLE IF NOT EXISTS test(
  id int(11) PRIMARY KEY,
  data varchar(255) NOT NULL
);

INSERT INTO test (id, data) VALUES (1, 'welcome to spring boot with docker');

From container startup to operation check



cd ./*docker-compose.Move to the current directory of yml

#Start the container in the background
docker-compose up -d
  Creating boot-mysql ... done
  Creating boot-container ... done

#Container start confirmation
docker ps
  CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
  74f3771fef53        boot_with_docker_centos   "/bin/bash"              4 minutes ago       Up 4 minutes        0.0.0.0:8080->8080/tcp   boot-container
  5448c1a111c0        mysql:5.7                 "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        3306/tcp, 33060/tcp      boot-mysql

#Build project with gradle
docker exec -w /gradle-project/docker-app boot-container gradle build
#Confirm that the jar file has been created
docker exec -w /gradle-project/docker-app/build/libs boot-container ls
  docker-app-0.0.1-SNAPSHOT.jar

#boot boot
docker exec -w /gradle-project/docker-app/build/libs boot-container java -jar docker-app-0.0.1-SNAPSHOT.jar --spring.profiles.active=docker
* application.The docker prefix of yml is used to separate the startup process in the local environment and the container environment.

#Operation check
curl http://localhost:8080/docker/test
result[welcome to spring boot with docker]

#Container down
docker-compose down


Supplement

mysql prevents direct access from the host OS. CentOS container (server) as a stepping stone setting (links) It is running as a private network to access the Mysql container (server) from the CentOS container.

Therefore, if you want to manipulate data to Mysql, you need to access Mysql from CentOS. Therefore, as described in the CentOS Dockerfile, mysql is also installed on the centos side.

Below is an example of Mysql access


#Start bash process of CentOS container
docker exec -it  boot-container /bin/bash

#Mysql login
mysql -u boot -p -h mysql

#Data confirmation
mysql> use bootdb
mysql> select * from test;

+----+------------------------------------+
| id | data                               |
+----+------------------------------------+
|  1 | welcome to spring boot with docker |
+----+------------------------------------+
1 row in set (0.00 sec)


Finally

When developing database linkages such as Spring Boot and Mysql It is necessary to install each software in the local environment, If you manage containers with Docker, you can easily create a development environment, so I also considered using Docker for personal development.

Use Docker's Volume feature to persist database data It is recommended because it also simplifies the complexity of managing Spring projects.

If you want to check the settings of Spring Boot, please refer to github. github

Reference

https://blog.tiqwab.com/2017/03/21/docker-java.html [Docker textbook for programmers](https://www.amazon.co.jp/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83% 9E% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AEDocker% E6% 95% 99% E7% A7% 91% E6% 9B% B8-% E3% 82% A4 % E3% 83% B3% E3% 83% 95% E3% 83% A9% E3% 81% AE% E5% 9F% BA% E7% A4% 8E% E7% 9F% A5% E8% AD% 98% EF % BC% 86% E3% 82% B3% E3% 83% BC% E3% 83% 89% E3% 81% AB% E3% 82% 88% E3% 82% 8B% E7% 92% B0% E5% A2 % 83% E6% A7% 8B% E7% AF% 89% E3% 81% AE% E8% 87% AA% E5% 8B% 95% E5% 8C% 96-WINGS% E3% 83% 97% E3% 83 % AD% E3% 82% B8% E3% 82% A7% E3% 82% AF% E3% 83% 88% E9% 98% BF% E4% BD% 90% E5% BF% 97% E4% BF% 9D -ebook / dp / B017UGA7NG / ref = sr_1_2? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & crid = USOWDBYOQ865 & dchild = 1 & keywords =% E3% 83% 97% E3 % 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9E% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AEdocker% E6% 95 % 99% E7% A7% 91% E6% 9B% B8 & qid = 1601222075 & sprefix =% E3% 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9E% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AEDocker% 2Caps% 2C250 & sr = 8-2)

Recommended Posts

Create Spring Boot-gradle-mysql development environment with Docker
Create a Spring Boot development environment with docker
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
Create a Vue3 environment with Docker!
Create SolrCloud verification environment with Docker
Create Laravel environment with Docker (docker-compose)
I tried to create a padrino development environment with Docker
Create Spring Boot development environment on Vagrant
Laravel development environment construction with Docker (Mac)
Build a PureScript development environment with Docker
Create Rails 6 + MySQL environment with Docker compose
Create a MySQL environment with Docker from 0-> 1
[Docker] Create Node.js + express + webpack environment with Docker
Spring Boot + Docker Java development environment construction
Build a Wordpress development environment with Docker
Lightweight PHP 7.4 development environment created with Docker
Spring Boot environment construction with Docker (January 2021 version)
[Memo] Create a CentOS 8 environment easily with Docker
Build a WordPress development environment quickly with Docker
Create Spring Boot environment with Windows + VS Code
[First team development ②] Build an environment with Docker
Wordpress local environment construction & development procedure with Docker
Create a java web application development environment with docker for mac part2
Pytorch execution environment with Docker
WSL2 + VSCode + Docker development environment
[First team development ③] Share the development environment created with Docker
[Docker] Rails 5.2 environment construction with docker
Spring Boot starting with Docker
Build docker environment with WSL
[Note] Create a java environment from scratch with docker
Build Java development environment with WSL2 Docker VS Code
Build a development environment to create Ruby on Jets + React apps with Docker
Docker × Spring Boot environment construction
Create microservices with Spring Boot
React environment construction with Docker
Beginners create Spring Tools Suite environment with VS Code
Database environment construction with Docker in Spring boot (IntellJ)
Debug the VSCode + Docker + PHP development environment with XDebug.
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
I tried to create a java8 development environment with Chocolatey
Create an AWS IoT EduKit development environment with Ubuntu 20.04 + VirtualBox 6.1
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
How to build Rails, Postgres, ElasticSearch development environment with Docker
Create Rails5 and postgresql environment with Docker and make pgadmin available
Build WebAPP development environment with Java + Spring with Visual Studio Code
Rails + MySQL environment construction with Docker
Node.js environment construction with Docker Compose
Build Couchbase local environment with Docker
Let's create Ubuntu environment with vmware
Environment construction with Docker for beginners
Create RUNTEQ's environment with Windows DockerDesktop
Hot deploy with Spring Boot development
Prepare Java development environment with Atom
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Create an app with Spring Boot
Build jooby development environment with Eclipse
[Environment construction with Docker] Rails 6 & MySQL 8