❤️使用Maven插件为SpringBoot应用构建Docker镜像

小明的学习圈子2024-03-25环境

一、Docker开启远程管理API

首先我们需要开启Docker的远程管理API。

1、查询docker服务

systemctl status docker.service

[root@linux-local ~]# systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2024-04-13 16:14:21 CST; 1min 4s ago
     Docs: https://docs.docker.com
 Main PID: 4144 (dockerd)
    Tasks: 45
   Memory: 48.3M
   CGroup: /system.slice/docker.service
           ├─4144 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
           ├─4292 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9849 -container-ip 172.17.0.2 -container-port 9849
           ├─4297 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 9849 -container-ip 172.17.0.2 -container-port 9849
           ├─4308 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9848 -container-ip 172.17.0.2 -container-port 9848
           ├─4313 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 9848 -container-ip 172.17.0.2 -container-port 9848
           ├─4324 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8848 -container-ip 172.17.0.2 -container-port 8848
           └─4328 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 8848 -container-ip 172.17.0.2 -container-port 8848

Apr 13 16:14:19 linux-local dockerd[4144]: time="2024-04-13T16:14:19.830085291+08:00" level=info msg="Firewalld: interface docke...urning"
Apr 13 16:14:20 linux-local dockerd[4144]: time="2024-04-13T16:14:20.238206444+08:00" level=info msg="Default bridge (docker0) i...ddress"
Apr 13 16:14:20 linux-local dockerd[4144]: time="2024-04-13T16:14:20.391664558+08:00" level=info msg="Firewalld: interface docke...urning"
Apr 13 16:14:21 linux-local dockerd[4144]: time="2024-04-13T16:14:21.250148575+08:00" level=info msg="Loading containers: done."
Apr 13 16:14:21 linux-local dockerd[4144]: time="2024-04-13T16:14:21.309770089+08:00" level=warning msg="[DEPRECATION NOTICE]: A... the do
Apr 13 16:14:21 linux-local dockerd[4144]: time="2024-04-13T16:14:21.309803462+08:00" level=info msg="Docker daemon" commit=8b79...=26.0.0
Apr 13 16:14:21 linux-local dockerd[4144]: time="2024-04-13T16:14:21.309839538+08:00" level=info msg="Daemon has completed initialization"
Apr 13 16:14:21 linux-local dockerd[4144]: time="2024-04-13T16:14:21.570025936+08:00" level=info msg="API listen on var/run/docker.sock"
Apr 13 16:14:21 linux-local dockerd[4144]: time="2024-04-13T16:14:21.578427707+08:00" level=info msg="API listen on [::]:2375"
Apr 13 16:14:21 linux-local systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

2、用vim编辑器修改docker.service文件;

vim /usr/lib/systemd/system/docker.service

3、修改配置

将ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock注释

替换成
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

替换后
#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

4、修改配置后需要重新启动Docker服务才能生效

systemctl daemon-reload
systemctl restart docker

5、开启防火墙打开的Docker远程管理API端口2375

firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload

6、查看端口是否开启

netstat -nlpt

[root@linux-local ~]# systemctl restart docker
[root@linux-local ~]# netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1696/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1074/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1077/cupsd
tcp        0      0 0.0.0.0:9848            0.0.0.0:*               LISTEN      4736/docker-proxy
tcp        0      0 0.0.0.0:9849            0.0.0.0:*               LISTEN      4720/docker-proxy
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1470/master
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      2544/sshd: root@pts
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      606/rpcbind
tcp        0      0 0.0.0.0:8848            0.0.0.0:*               LISTEN      4752/docker-proxy
tcp6       0      0 :::22                   :::*                    LISTEN      1074/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      1077/cupsd
tcp6       0      0 :::9848                 :::*                    LISTEN      4741/docker-proxy
tcp6       0      0 :::9849                 :::*                    LISTEN      4725/docker-proxy
tcp6       0      0 ::1:25                  :::*                    LISTEN      1470/master
tcp6       0      0 ::1:6010                :::*                    LISTEN      2544/sshd: root@pts
tcp6       0      0 :::2375                 :::*                    LISTEN      4584/dockerd
tcp6       0      0 :::111                  :::*                    LISTEN      606/rpcbind
tcp6       0      0 :::8848                 :::*                    LISTEN      4757/docker-proxy
[root@linux-local ~]#

7、验证


[root@linux-local ~]# docker -H 192.168.1.71:2375 images
REPOSITORY           TAG                 IMAGE ID       CREATED         SIZE
minio/minio          latest              a66f62e448f2   13 days ago     159MB
mongo                4                   3593bb45b7ce   6 weeks ago     427MB
redis                7                   170a1e90f843   3 months ago    138MB
mysql                5.7                 5107333e08a8   4 months ago    501MB
nginx                1.22                0f8498f13f3a   12 months ago   142MB
elasticsearch        7.17.3              3c91aa69ae06   24 months ago   613MB
kibana               7.17.3              4897f4b8b6ee   24 months ago   797MB
logstash             7.17.3              dd4291c803f4   24 months ago   774MB
rabbitmq             3.9.11-management   6c3c2a225947   2 years ago     253MB
nacos/nacos-server   2.0.2               c8cd99bb135c   2 years ago     1.05GB
[root@linux-local ~]#

二、使用Maven构建Docker镜像

1、在应用的pom.xml文件中添加docker-maven-plugin插件的依赖;

<properties>
    <docker.host>http://192.168.1.71:2375</docker.host>
    <docker.maven.plugin.version>0.40.2</docker.maven.plugin.version>
</properties>
<plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>${docker.maven.plugin.version}</version>
                <executions>
                    <!--如果想在项目打包时构建镜像添加-->
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- Docker 远程管理地址-->
                    <dockerHost>${docker.host}</dockerHost>
                    <images>
                        <image>
                            <!--定义镜像名称-->
                            <name>stucoding/${project.name}:${project.version}</name>
                            <!--定义镜像构建行为-->
                            <build>
                                <!--定义基础镜像-->
                                <from>openjdk:8</from>
                                <args>
                                    <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                                </args>
                                <!--定义哪些文件拷贝到容器中-->
                                <assembly>
                                    <!--定义拷贝到容器的目录-->
                                    <targetDir>/</targetDir>
                                    <!--只拷贝生成的jar包-->
                                    <descriptorRef>artifact</descriptorRef>
                                </assembly>
                                <!--定义容器启动命令-->
                                <entryPoint>["java", "-jar","-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]</entryPoint>
                                <!--定义维护者-->
                                <maintainer>stucoding</maintainer>
                            </build>
                        </image>
                    </images>
                </configuration>
            </plugin>

完整依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.auggie</groupId>
    <artifactId>student_server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>student_server</name>
    <description>student_server</description>
<properties>
    <docker.host>http://192.168.1.71:2375</docker.host>
    <docker.maven.plugin.version>0.40.2</docker.maven.plugin.version>
</properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>${docker.maven.plugin.version}</version>
                <executions>
                    <!--如果想在项目打包时构建镜像添加-->
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- Docker 远程管理地址-->
                    <dockerHost>${docker.host}</dockerHost>
                    <images>
                        <image>
                            <!--定义镜像名称-->
                            <name>stucoding/${project.name}:${project.version}</name>
                            <!--定义镜像构建行为-->
                            <build>
                                <!--定义基础镜像-->
                                <from>openjdk:8</from>
                                <args>
                                    <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                                </args>
                                <!--定义哪些文件拷贝到容器中-->
                                <assembly>
                                    <!--定义拷贝到容器的目录-->
                                    <targetDir>/</targetDir>
                                    <!--只拷贝生成的jar包-->
                                    <descriptorRef>artifact</descriptorRef>
                                </assembly>
                                <!--定义容器启动命令-->
                                <entryPoint>["java", "-jar","-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]</entryPoint>
                                <!--定义维护者-->
                                <maintainer>stucoding</maintainer>
                            </build>
                        </image>
                    </images>
                </configuration>
            </plugin>


        </plugins>
    </build>

</project>

2、相关配置说明:

    • executions.execution:此处配置了在maven打包应用时构建docker镜像;
    • image.name:用于指定镜像名称,mall-tiny是仓库名称,${project.name}为镜像名称,${project.version}为镜像标签名称;
    • dockerHost:打包后上传到的docker服务器地址;
    • build.from:该应用所依赖的基础镜像,此处为openjdk
    • entryPoint:docker容器启动时执行的命令,可以使用-Dspring.profiles.active=prod指定应用配置文件;
    • assembly:定义哪些文件拷贝到容器中;
    • assembly.targetDir:定义拷贝到容器的目录;
    • assembly.descriptorRef:只拷贝生成的jar包;
    • maintainer:定义项目的维护者。
  • 添加application-prod.yml配置文件,只是将之前的数据库地址的localhost改为了db

注意:可以把docker中的容器看作独立的虚拟机,student_server访问localhost自然会访问不到mysql,docker容器之间可以通过指定好的服务名称db进行访问,至于db这个名称可以在运行student_server容器的时候指定。

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://db:3306/studentms?setUnicode=true&characterEncoding=utf8&useAffectedRows=true
    username: root
    password: stucoding

mybatis:
  type-aliases-package: com.auggie.student_server.entity
  mapper-locations: classpath:mapper/*.xml

server:
  port: 10086

三、使用IDEA打包项目并构建镜像

注意:依赖的基础镜像需要先行下载,否则会出现构建镜像超时的情况,比如我本地并没有openjdk:8的镜像,就需要先把镜像pull下来,再用maven插件进行构建。

执行maven的clean、package命令

1、构建成功:

"C:\Program Files\Java\jdk1.8.0\bin\java.exe" -Dmaven.multiModuleProjectDirectory=D:\konglxmoney2024\student_server -Djansi.passthrough=true -Dmaven.home=E:\konglx_work\server\apache-maven-3.6.0 -Dclassworlds.conf=E:\konglx_work\server\apache-maven-3.6.0\bin\m2.conf "-Dmaven.ext.class.path=D:\Program Files\JetBrains\IntelliJ IDEA 2023.2\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2524:D:\Program Files\JetBrains\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath E:\konglx_work\server\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2023.2 -s E:\konglx_work\server\apache-maven-3.6.0\conf\settings.xml package
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.auggie:student_server >----------------------
[INFO] Building student_server 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ student_server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 2 resources
[INFO] Copying 5 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ student_server ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ student_server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory D:\konglxmoney2024\student_server\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ student_server ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ student_server ---
[INFO] 
[INFO] --- maven-jar-plugin:3.2.2:jar (default-jar) @ student_server ---
[INFO] Building jar: D:\konglxmoney2024\student_server\target\student_server-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.6.3:repackage (repackage) @ student_server ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --- docker-maven-plugin:0.40.2:build (build-image) @ student_server ---
[INFO] Copying files to D:\konglxmoney2024\student_server\target\docker\stucoding\student_server\0.0.1-SNAPSHOT\build\maven
[INFO] Building tar: D:\konglxmoney2024\student_server\target\docker\stucoding\student_server\0.0.1-SNAPSHOT\tmp\docker-build.tar
[INFO] DOCKER> [stucoding/student_server:0.0.1-SNAPSHOT]: Created docker-build.tar in 527 milliseconds
[INFO] DOCKER> [stucoding/student_server:0.0.1-SNAPSHOT]: Built image sha256:258e2
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.307 s
[INFO] Finished at: 2024-04-13T21:40:41+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

2、镜像仓库已有该镜像:


[root@linux-local ~]# docker images
REPOSITORY                 TAG                 IMAGE ID       CREATED          SIZE
stucoding/student_server   0.0.1-SNAPSHOT      258e2fda3ac0   13 minutes ago   549MB
minio/minio                latest              a66f62e448f2   13 days ago      159MB
mongo                      4                   3593bb45b7ce   6 weeks ago      427MB
redis                      7                   170a1e90f843   3 months ago     138MB
mysql                      5.7                 5107333e08a8   4 months ago     501MB
nginx                      1.22                0f8498f13f3a   12 months ago    142MB
openjdk                    8                   b273004037cc   20 months ago    526MB
elasticsearch              7.17.3              3c91aa69ae06   24 months ago    613MB
kibana                     7.17.3              4897f4b8b6ee   24 months ago    797MB
logstash                   7.17.3              dd4291c803f4   24 months ago    774MB
rabbitmq                   3.9.11-management   6c3c2a225947   2 years ago      253MB
nacos/nacos-server         2.0.2               c8cd99bb135c   2 years ago      1.05GB
[root@linux-local ~]#

四、在Docker容器中运行项目

1、运行mysql服务

mysql安装运行参照地址open in new window

2、运行springboot应用服务


[root@linux-local ~]#   docker run -p 10086:10086 --name student_server \
>   --link mysql:db \
>   -v /etc/localtime:/etc/localtime \
>   -v /mydata/app/mall-tiny-docker/logs:/var/logs \
>   -d stucoding/student_server:0.0.1-SNAPSHOT
39aea735097b273ac9b67d45703d7e836625b1b91a92fb0de937d85b26333aca
[root@linux-local ~]# docker logs -f ^C
[root@linux-local ~]# docker logs -f 39aea735097b273ac9b67d45703d7e836625b1b91a92fb0de937d85b26333aca

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2024-04-13 13:57:01.509  INFO 1 --- [           main] c.a.s.StudentServerApplication           : Starting StudentServerApplication v0.0.1-SNAPSHOT using Java 1.8.0_342 on 39aea735097b with PID 1 (/student_server-0.0.1-SNAPSHOT.jar started by root in /)
2024-04-13 13:57:01.511  INFO 1 --- [           main] c.a.s.StudentServerApplication           : The following profiles are active: prod
2024-04-13 13:57:03.440  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 10086 (http)
2024-04-13 13:57:03.456  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-04-13 13:57:03.456  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2024-04-13 13:57:03.555  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-04-13 13:57:03.555  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1963 ms
2024-04-13 13:57:05.332  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 10086 (http) with context path ''
2024-04-13 13:57:05.355  INFO 1 --- [           main] c.a.s.StudentServerApplication           : Started StudentServerApplication in 4.666 seconds (JVM running for 5.302)
2024-04-13 13:57:25.768  INFO 1 --- [io-10086-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-04-13 13:57:25.769  INFO 1 --- [io-10086-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-04-13 13:57:25.775  INFO 1 --- [io-10086-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 6 ms
正在查询学生信息 By id 9
2024-04-13 13:57:25.928  INFO 1 --- [io-10086-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-04-13 13:57:26.775  INFO 1 --- [io-10086-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.

3、访问访问

http://192.168.1.71:10086/student/findById/9

image-20240413215812141

4、源码获取地址open in new window

五、前端在Linux环境下的部署

1、nginx安装参照open in new window

2、请求地址修改为虚拟机的ip

将所有的url地址http://localhost/ 修改为http://192.168.1.71/

axios.get('http://192.168.1.71:10086/course/deleteById/' + row.cid).then(function (resp) 

3、然后使用如下命令进行打包

npm run build

4、打包后的代码将保存在dist目录下

 student_client@0.1.0 build E:\20230406WorkSpace\20230417\student_client
> vue-cli-service build

Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating

-  Building for production...Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
\  Building for production...Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
/  Building for production...Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
-  Building for production...Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
/  Building for production...Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
-  Building for production...Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
 WARNING  Compiled with 3 warnings                                                                        下午10:48:54

 warning  

asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
This can impact web performance.
Assets: 
  js/chunk-vendors.b556547f.js (870 KiB)

 warning  

entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
Entrypoints:
  app (1.15 MiB)
      css/chunk-vendors.a16c4353.css
      js/chunk-vendors.b556547f.js
      css/app.e6d587cb.css
      js/app.76d7fe5b.js


 warning 

webpack performance recommendations:
You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.
For more info visit https://webpack.js.org/guides/code-splitting/

  File                                   Size                                 Gzipped

  dist\js\chunk-vendors.b556547f.js      870.06 KiB                           235.08 KiB
  dist\js\app.76d7fe5b.js                102.13 KiB                           16.63 KiB
  dist\css\chunk-vendors.a16c4353.css    209.55 KiB                           33.63 KiB
  dist\css\app.e6d587cb.css              0.15 KiB                             0.14 KiB

  Images and other types of assets omitted.

 DONE  Build complete. The dist directory is ready to be deployed.

5、可以使用压缩软件将dist目录打包为dist.tar.gz文件;

Windows如何压缩tar.gz格式open in new window

image-20240413225155803

6、将dist.tar.gz上传到linux服务器上,上传目录为Nginx的html目录

tar -zxvf dist.tar.gz

7、移动dist文件夹到admin文件夹;

mv dist stucoding-student

[root@linux-local html]# ll
total 1332
drwxrwxrwx. 6 root root      88 Apr 13 22:48 dist
-rw-r--r--. 1 root root 1362618 Apr 13 22:54 dist.tar.gz
[root@linux-local html]# mv dist stucoding-student
[root@linux-local html]# ll
total 1332
-rw-r--r--. 1 root root 1362618 Apr 13 22:54 dist.tar.gz
drwxrwxrwx. 6 root root      88 Apr 13 22:48 stucoding-student

8、需要修改nginx的默认配置

由于我们将使用服务器IP进行访问,所以我们还需要修改nginx的默认配置文件default.conf

vim /mydata/nginx/conf/conf.d/default.conf

默认

drwxrwxrwx. 6 root root      88 Apr 13 22:48 dist
-rw-r--r--. 1 root root 1362618 Apr 13 22:54 dist.tar.gz
[root@linux-local html]# mv dist stucoding-student
[root@linux-local html]# ll
total 1332
-rw-r--r--. 1 root root 1362618 Apr 13 22:54 dist.tar.gz
drwxrwxrwx. 6 root root      88 Apr 13 22:48 stucoding-student
[root@linux-local html]# cd /mydata/nginx/conf/conf.d/
[root@linux-local conf.d]# ll
total 4
-rw-r--r--. 1 root root 1093 Apr  4 11:54 default.conf
[root@linux-local conf.d]# ^C
[root@linux-local conf.d]# vim /mydata/nginx/conf/conf.d/default.conf
[root@linux-local conf.d]# cat /mydata/nginx/conf/conf.d/default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

修改后

    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

[root@linux-local conf.d]# ^C
[root@linux-local conf.d]# vim /mydata/nginx/conf/conf.d/default.conf
[root@linux-local conf.d]# cat /mydata/nginx/conf/conf.d/default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  192.168.1.71;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

将默认的server_namelocalhost改为192.168.1.71

9、修改完配置后重启nginx

docker restart nginx

10、访问首页并登录:http://192.168.1.71/stucoding-student/

Last Updated 2024/4/13 23:21:06