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.
Output the value of the database (Mysql) built using docker in the sample project to the screen
docker beginner or Engineer with little development experience (1 or 2 years)
・ How to create a docker file -Linkage between MySQL environment created on docker and spring boot project
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-
・ 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.
■ 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)
Until now, it was only links, but I would like to put out my own words soon.
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)
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.
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"
Right-click docker-compose.yml on your project and select "Play".
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.
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
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
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;
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.
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