preface
Update Oracle Cloud instance (Centos 8) with Spring Boot +Mysql.
Spring Boot and mysql are separate Docker containers, so they need to interact via IP or link.
Docker-compose is not used in this release
Create a bridge network
Docker has three common network modes: Bridge, host, and overlay.
- Bridge, where the applications are all containers on a single host and the containers need to communicate with each other;
- Host, where the container and the native application are intermixed and need to access each other;
- Overlay, a scenario in which containers on different hosts need to communicate with each other.
This section only focuses on bridge mode. You can search for the other two modes by yourself. Bridges are divided into “default Bridge” and “user-defined Bridge”. After docker is installed, a bridge network (Docker0) will be created by default, which is the “default Bridge”. Containers that do not specify a network will use this network by default. Custom Bridges are often used. We can define a private network to isolate our application from other applications, which is very useful on a shared development server. In addition, a custom network has the following two advantages:
- Only user-defined Bridge networks have the DNS function and can access each other through container names. Otherwise, only IP addresses can be used for inter-container access. IP addresses change and are hard to remember.
- The container can be easily replaced by a custom bridge on the fly.
docker network create spring-net
Copy the code
With the following command, you can see a new Bridge network.
docker network ls
Copy the code
You can also use the following command to see the details of Spring-Net
docker network inspect spring-net
Copy the code
Install and run Mysql8
Enter it under Docker Shell
docker pull mysql:latest
Copy the code
Get mysql8 image. Use the following command to see that it has been downloaded to mysql8.
docker image ls
Copy the code
Run mysql8
$ docker run -it --network spring-net --name MySQL80 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
Copy the code
, p: specify docker port conversion e: pass parameters to Docker and set password to root by specifying MYSQL_ROOT_PASSWORD outside the container. D: Running in background.
Install and run the SpringBoot instance
Compile the SpringBoot application with IDEA into an executable JAR, such as MyBatisstudent-0.0.1-snapshot.jar.
On Oracle Cloud, use xshell to create the directory /usr/mystudent and upload myBatisstudent-0.0.1-snapshot. jar to this directory using XFTP.
Create a new Dockerfile file in this directory as follows:
If there is no basic image, Docker will automatically download it. If there is, it will not download it. FROM openjdk:16.0.1- JDK # # add myBatisstudent-0.0.1-snapshot. jar to myBatisstudent. jar from the same directory ENV username="" ENV password="" ENV URL ="" # open port 8081 EXPOSE 8081 # ENTRYPOINT specify ENTRYPOINT ["sh","-c"," Java -jar/myBatisstudent.jar" --spring.datasource.username=$username --spring.datasource.url=$url --spring.datasource.password=$password""]Copy the code
After saving the Dockerfile, run the following command to compile the SpringBoot program’s image.
#docker build --force-rm --no-cache -t mybatisstudent:v1 .
Notice the dot at the end, which indicates the current directory.
To run the Spring Boot application:
#docker run --name studentapp --network spring-net --link MySQL80:MySQL80 -e username="root" -e password="Sj750513" -e url="jdbc:mysql://MySQL80:3306/studentmanager? setUnicode=true&characterEncoding=utf8&useAffectedRows=true" -p 8081:8081 -d mybatisstudent:v1Copy the code
In the browser input XXX.XXX.XXX.XXX :8081 test, all ok.