Use Docker to publish SpringBoot projects

1. Install Docker

Use xshell to connect to your own server.

Prerequisite: Check the kernel environment: it must be 3.10 and above. The centos version is 7 and above.

[root@iZ8vb409m8717t5boglt61Z ~]# uname -r
3.10.0-862.14.4.el7.x86_64
[root@iZ8vb409m8717t5boglt61Z ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

Formal installation

See Docker's official help documentation .

1. Uninstall the old version

 sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2. Required installation package

sudo yum install -y yum-utils

3. Set up a mirrored warehouse

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo  #默认是国外的,很慢。
    
 #安装国内的阿里云镜像 
sudo yum-config-manager \
    --add-repo \   
  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. Install docker

#更新软件包索引
yum makecache fast
 sudo yum install docker-ce docker-ce-cli containerd.io

5. Start docker

sudo systemctl start docker

6. Use docker version to check whether the installation is successful

docker version

Insert picture description here

7. Test hello-world

docker run hello-world

Insert picture description here
8. View the mirror

docker images
[root@iZ8vb409m8717t5boglt61Z ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB

Extension: uninstall docker

#卸载依赖
sudo yum remove docker-ce docker-ce-cli containerd.io
#删除资源   /var/lib/docker是docker的默认工作路径
sudo rm -rf /var/lib/docker

2. Configure Alibaba Cloud Image Accelerator

Insert picture description here

#1
sudo mkdir -p /etc/docker

#2
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    
    
  "registry-mirrors": ["https://7j3fyo2t.mirror.aliyuncs.com"]
}
EOF

#3
sudo systemctl daemon-reload

#4
sudo systemctl restart docker

3. Server configuration

Mainly three aspects of configuration.

  • Firewall
  • Add port
  • Security group

1. Turn on the firewall

#查看防火墙状态
firewall-cmd --state

#开启防火墙
systemctl start firewalld.service
[root@iZ8vb409m8717t5boglt61Z ~]# firewall-cmd --state
not running
[root@iZ8vb409m8717t5boglt61Z ~]# systemctl start firewalld.service
[root@iZ8vb409m8717t5boglt61Z ~]# firewall-cmd --state
running

2. Add external ports

#添加80端口
[root@iZ8vb409m8717t5boglt61Z ~]#  firewall-cmd --zone=public --add-port=80/tcp --permanent
success

#添加8080端口
[root@iZ8vb409m8717t5boglt61Z ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success

#添加3306端口
[root@iZ8vb409m8717t5boglt61Z ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success

#添加端口之后,要重新加载防火墙
[root@iZ8vb409m8717t5boglt61Z ~]# firewall-cmd --reload
success

#查看开放的端口
[root@iZ8vb409m8717t5boglt61Z ~]# firewall-cmd --list-ports
6379/tcp 80/tcp 8080/tcp 3306/tcp

3. Security group configuration
Insert picture description here

4. Install mysql

Go to docker hub and search for mysql. There are detailed instructions on the document.

#1、搜索镜像
docker search mysql

#2、下载,默认是最新版
#也可以指定版本号 docker pull mysql:版本号
docker pull mysql

#3、查看下载的镜像
docker images

#4、创建mysql容器置并设置root的密码为123456
docker run -d -p 3306:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456

#5、查看容器
docker ps

#6、进入容器docker exec -it 容器号 /bin/bash
docker exec -it bab0353e153c /bin/bash
[root@iZ8vb409m8717t5boglt61Z ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
bab0353e153c        mysql:latest        "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:3306->3306/tcp, 33060/tcp   mymysql
[root@iZ8vb409m8717t5boglt61Z ~]# docker exec -it bab0353e153c /bin/sh
# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select version(); #查看mysql的版本号
+-----------+
| version() |
+-----------+
| 8.0.20    |
+-----------+
1 row in set (0.00 sec)

Use tools to connect to mysql

Insert picture description here

If you can't connect to mysql, you can refer to the blog .

The next step is to execute the sql file.

img

5. Package the SpringBoot project

1. Change the localhost in your database url to the ip of your server, and modify the mysql password installed on docker.

Next, package, right-click on the maven on the right, as shown below:

Check again and again, don't make a mistake.
Insert picture description here

2. Wait for the build to succeed. As shown below:
Insert picture description here

After success, you will find the following jars in the target.
Insert picture description here

6, write a Dockerfile file

#在home下创建docker文件夹
mkdir /home/docker 

You can write in the idea project or on the server, either.

The contents of the Dockerfile are as follows:

#创建Dockerfile文件并打开
vim Dockerfile

#添加的内容如下:
FROM java:8

ADD demo-blog-0.0.1-SNAPSHOT.jar /blog.jar

EXPOSE 8080

ENTRYPOINT ["java","-jar","/blog.jar"]

Explanation:

#注意,docker命令全部大写,这是规定。
#From 关键字表示,jar包依赖的环境。java:8  相当于jdk1.8
FROM java:8
 
#ADD命令 
#demo-blog-0.0.1-SNAPSHOT.jar:这是你上传jar包的名称。
#/blog.jar:这是自定义的名称。但是注意要有之前的/
ADD demo-blog-0.0.1-SNAPSHOT.jar /blog.jar
 
#EXPOSE 项目暴露的端口号
EXPOSE 8080
 
#/blog.jar此处的名称要和ADD命令后面的一样。
ENTRYPOINT ["java","-jar","/blog.jar"]

Transfer to the server.
Insert picture description here

7. Run

Enter /home/docker, you can see the uploaded file, a jar, and a Dockerfile.

[root@iZ8vb409m8717t5boglt61Z ~]# cd /home/docker
[root@iZ8vb409m8717t5boglt61Z docker]# ls
demo-blog-0.0.1-SNAPSHOT.jar  DockerFile

carried out

[root@iZ8vb409m8717t5boglt61Z docker]# docker build -t blog:1.0 .

#说明:
# blog   代表要打包成的镜像名称。按照自己实际情况写。
#:1.0   代表版本号,可以不写则默认为latest
# .    代表为当前目录。这就是为什么一直在步骤一文件夹中进行操作,并且Dockerfile在此文件夹中的原因。
 
#若之前Dockerfile不在步骤一的文件夹中 则需要指定到对应的地址。

The following is executed successfully.

[root@iZ8vb409m8717t5boglt61Z docker]# docker build -t blog:1.0 .
Sending build context to Docker daemon  33.24MB
Step 1/4 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete 
fce5728aad85: Pull complete 
76610ec20bf5: Pull complete 
60170fec2151: Pull complete 
e98f73de8f0d: Pull complete 
11f7af24ed9c: Pull complete 
49e2d6393f32: Pull complete 
bb9cdec9c7f3: Pull complete 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/4 : ADD demo-blog-0.0.1-SNAPSHOT.jar /blog.jar
 ---> 4e9a3ac9e03e
Step 3/4 : EXPOSE 8080
 ---> Running in 0897a57a4d85
Removing intermediate container 0897a57a4d85
 ---> 67dceec547a7
Step 4/4 : ENTRYPOINT ["java","-jar","/blog.jar"]
 ---> Running in ed80fc3d7499
Removing intermediate container ed80fc3d7499
 ---> 19e4ce0e729b
Successfully built 19e4ce0e729b
Successfully tagged blog:1.0

View the mirror:

[root@iZ8vb409m8717t5boglt61Z docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
blog                1.0                 19e4ce0e729b        3 minutes ago       676MB
mysql               latest              30f937e841c8        2 days ago          541MB
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB
java                8                   d23bdf5b1b1b        3 years ago         643MB

Run mirror

#这里我开放的是80端口,所以要通过80去映射项目中的8080
[root@iZ8vb409m8717t5boglt61Z docker]# docker run --name blog -d -p 80:8080 blog:1.0

Finally, you can use your public network ip to visit.

http://自己服务器的公网ip/:80

If there is an error or something during the operation, you can use the following command to check the log.

docker logs 容器id

It is not safe to use your own server address. You can apply for a domain name, bind the domain name, and use the domain name to access.

Below is my personal blog system. Put a few pictures.
If you need the source code, you can go here to get it . There are still many shortcomings. Learn more later.
Insert picture description here
Message board interface. Insert picture description here
Classification interface.
Insert picture description here
Label interface.
Insert picture description here
Archive interface.
Insert picture description here

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/106323382