The docker method for connecting spring boot and mysql containers is described

  • 2020-06-23 02:29:04
  • OfStack

The small example of Spring Boot was run before using docker deployment, but without a database. In this article, you learned how docker starts the mysql container and how to run the Spring Boot container with the mysql container.

docker basic command

First, get familiar with the basic docker command commonly used in the operation process:


docker images : List all docker The mirror 
docker ps : Lists all running containers, -a Parameters can list all containers, including stopped ones 
docker stop container_id : Stop the container 
docker start container_name : Starts a stopped container 
docker rm container_id : To delete a stopped container, add -f Option to force the removal of a running container 
docker rmi image_id : Removes the image if it has no corresponding container 
docker run mysql The container 

The first is new Dockerfile:


FROM ubuntu:14.04
MAINTAINER loveqh
RUN apt-get update
RUN apt-get -y install mysql-server
RUN /etc/init.d/mysql start \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'%' identified by '123456';" \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';" 

RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \
  && echo 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/mysql/my.cnf > /tmp/my.cnf \
  && mv /tmp/my.cnf /etc/mysql/my.cnf

EXPOSE 3306 
CMD ["/usr/bin/mysqld_safe"]

Then create the mysql image:


docker build -t loveqh/mysql .

The next step is to start a container from this mirror:


docker run -d -P --name docker-mysql loveqh/mysql

Among them,

-ES34en means to run the container in the background and return the container id
-ES37en maps all exposed ports of the container to random port Numbers of the host, or -ES38en can be used to specify a container port mapping relationship, such as -ES39en 33060:3306, which maps port 3306 of the container to port 33060 of the host

You can view the container port mapping from docker ps, shown in column 1 of PORTS


0.0.0.0:32770->3306/tcp

That is, mysql is mapped to port 32770 of the host, so it can pass


mysql -h 0.0.0.0 -P 32770 -uroot -p

It's connected to the mysql container.

docker connects the spring boot and mysql containers

The previous article ran an instance of Spring Boot on docker, but did not use the database. Next we add database operations on a project basis.
First, create application. properties file under resources to configure the database:


spring.datasource.url = jdbc:mysql://localhost:3306/spring
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver

Note: url here will be modified later when the mysql container is connected.

Create a new schema. sql file, which is automatically executed by Spring Boot on startup, so you can create tables and insert test data in this file:


use spring;
create table if NOT EXISTS user (
  id int PRIMARY KEY NOT NULL auto_increment,
  name VARCHAR(30),
  password VARCHAR(10),
  email VARCHAR(30)
);
-- INSERT INTO user(name, password, email) values("test", "001", "test@163.com");
INSERT INTO user(name, password, email)
SELECT * FROM (SELECT "test", "001", "test@163.com") AS tmp
WHERE NOT EXISTS (
  SELECT name FROM user WHERE name='test' AND email='test@163.com'
) limit 1;

In this file, you specify the database spring to use, then create a new one if there is no user table. When you insert the test data next, you annotate the simple insert command because it inserts the same record every time you start the project, so replace it with the following statement.

Follow the previous steps to create the Spring Boot image:


docker build -t loveqh/spring-boot-mysql-docker .

Here is the connection to run the Spring Boot container and connect to the mysql database:


docker run -d -p 8088:8080  � name spring-web  � link docker-mysql:mysql loveqh/spring-boot-mysql-docker

Among them,

-d is still running in the background. If you don't want to run in the background, you can replace the -d parameter with -ES106en, so that you can see the output information of the project. Of course, you can also view the container log from docker logs ES109en-ES110en/ES111en-ES112en.
The -ES114en parameter maps the default port 8080 of Spring Boot in the container to port 8088 of the host
name specifies the name of the container so that it can be restarted from docker start ES121en-ES122en after the container has stopped
The link parameter connects to the ES125en-ES126en container and USES the alias mysql

The spring boot project initially had a hard time configuring the mysql address because it ran the mysql container without specifying a port mapping, which was random, and if we had written localhost: mapped port in mysql's url, we would have lost the use of link to connect the two containers. Using link, docker binds the parent container (mysql here) to the parent container's ip address in the /etc/hosts of the child container (spring boot here). Then we can access the database using mysql:3306. In other words, the database url in ES148en. properties is changed to:


FROM ubuntu:14.04
MAINTAINER loveqh
RUN apt-get update
RUN apt-get -y install mysql-server
RUN /etc/init.d/mysql start \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'%' identified by '123456';" \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';" 

RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \
  && echo 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/mysql/my.cnf > /tmp/my.cnf \
  && mv /tmp/my.cnf /etc/mysql/my.cnf

EXPOSE 3306 
CMD ["/usr/bin/mysqld_safe"]
0

The second mysql is the alias we set up earlier.

Next, visit http://localhost:8088 to see the results.

Project code

Since this article is mainly about connecting two containers with docker, the code is not explained, but simply pasting out the key code.

Index - Home page name:password:email:


FROM ubuntu:14.04
MAINTAINER loveqh
RUN apt-get update
RUN apt-get -y install mysql-server
RUN /etc/init.d/mysql start \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'%' identified by '123456';" \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';" 

RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \
  && echo 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/mysql/my.cnf > /tmp/my.cnf \
  && mv /tmp/my.cnf /etc/mysql/my.cnf

EXPOSE 3306 
CMD ["/usr/bin/mysqld_safe"]
1

FROM ubuntu:14.04
MAINTAINER loveqh
RUN apt-get update
RUN apt-get -y install mysql-server
RUN /etc/init.d/mysql start \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'%' identified by '123456';" \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';" 

RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \
  && echo 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/mysql/my.cnf > /tmp/my.cnf \
  && mv /tmp/my.cnf /etc/mysql/my.cnf

EXPOSE 3306 
CMD ["/usr/bin/mysqld_safe"]
2

list -  List of users 
 
 id 	 name 	 email 	 stat:index 	 stat:count 	 stat:size 	 stat:current 	 stat:even 	 stat:odd 	 stat:first 	 stat:last 
 id,error 	 name,error 	 email,error 	 error 	 error 	 error 	 error 	 error 	 error 	 error 	 error 

The problem record

In the process of doing a lot of problems, after looking up data to solve, now it is recorded.

Encountered an error in using thymeleaf template engine: org. xml. sax. SAXParseException: The element type "THYMELEAF_ROOT must be terminated by the matching end - tag

Many thymeleaf errors have been encountered before, which are all the closure problems of html tags. Carefully check whether the tags in html files are closed and whether they are corresponding
When visiting the registration page of index, an error was encountered: ES210en.lang

The thymeleaf auto-bound form was used for submission, and it took a long time to check the registry method and index.html, with the results all wrong. It turned out that th:object= "${user}" was on the index page, but the index method did not add the object to model at first, so it failed to get user. The solution is to add the user object to model in all methods that return index:


FROM ubuntu:14.04
MAINTAINER loveqh
RUN apt-get update
RUN apt-get -y install mysql-server
RUN /etc/init.d/mysql start \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'%' identified by '123456';" \
  && mysql -uroot -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';" 

RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \
  && echo 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/mysql/my.cnf > /tmp/my.cnf \
  && mv /tmp/my.cnf /etc/mysql/my.cnf

EXPOSE 3306 
CMD ["/usr/bin/mysqld_safe"]
4

conclusion

That is the end of this article's introduction to the docker method of connecting spring boot and mysql containers. Docker security mechanism kernel security and container between the network security, detailed explanation of Docker Linux iptables and Interfaces to manage the container network, Windows docker to open a new window error solutions, you can leave a message at any time, this site will reply you in time. Here I recommend several books related to docker for docker enthusiasts to learn:

Docker 1 complete pdf scan with bookmark directory

https://www.ofstack.com/books/514869.html

Docker Container and Container Cloud (2nd edition) Complete pdf Scan

https://www.ofstack.com/books/569549.html

Hope you enjoy it!


Related articles: