• 关于前后端分离的项目打包方案docker


    就拿近期很火的sonic来说吧,
    GitHub:https://github.com/SonicCloudOrg
    主要打包部署的就是这两个部分在这里插入图片描述

    前端vue项目:
    准备好vue环境

    构建一个dockerfile文件:写下如下内容:

    FROM nginx:1.20.0
    COPY nginx.conf /etc/nginx/nginx.conf
    COPY dist/  /usr/share/nginx/html/
    COPY replace.sh /
    RUN chmod 777 /replace.sh
    CMD ["/replace.sh"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    然后npm run build,这个时候项目中会多出一个文件夹叫dist,在idea中显示是橙色的如下:在这里插入图片描述
    然后执行:
    docker build -t sonic-org/sonic-client-web .(本地有一个dockerfile文件,所以可以直接使用)
    在这里插入图片描述
    可以看到打包成功,然后再执行docker images 就可以看到一个名称为sonic-org/sonic-client-web的镜像,如果此时有仓库可以直接push,没有的话就导出一个压缩文件留着,再搬运到服务器上解压成镜像文件在这里插入图片描述
    本地生成镜像后,使用docker save -o 命令生成一个压缩文件
    然后使用docker load -i 命令导出成images
    保存docker save -o aaa_v1.tar aaa:v1 :如下:
    docker save -o web.tar sonicorg/sonic-client-web:latest

    导入docker load -i aaa_v1.tar
    docker load -i web.tar

    docker save -o web.tar sonicorg/sonic-client-web:latest

    要是不需要迁移,直接在服务器打包的话,就不需要后面导出的步骤拉,

    后端server项目打包:
    首先需要mvn环境,mac如果是m1芯片的话,可能会打包失败,可以一试

    首先准备好mvn环境,搜教程自己搞吧
    然后在pom文件中加进来

    <!-- 打包构建配置  -->
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
            <!--   最终打包的jar包名,使用artifactId     -->
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>1.2.2</version>
                    <executions>
                        <execution>
                            <id>imageBuild</id>
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                            </goals>
                            <configuration>
                                <pushImage>true</pushImage>
                                <imageName>sonicorg/${project.artifactId}:v${project.version}</imageName>
                                <baseImage>adoptopenjdk/openjdk15:alpine AS builder</baseImage>
                                <runs>
                                    <run>ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime</run>
                                </runs>
                                <entryPoint>
                                    ["java","-server","-XX:-UseGCOverheadLimit","-XX:+DisableExplicitGC","-XX:SurvivorRatio=1","-XX:LargePageSizeInBytes=128M","-XX:SoftRefLRUPolicyMSPerMB=0","-Djava.security.egd=file:/dev/./urandom","--add-exports=java.naming/com.sun.jndi.ldap=ALL-UNNAMED","-jar","/${project.build.finalName}.jar"]
                                </entryPoint>
                                <resources>
                                    <resource>
                                        <targetPath>/</targetPath>
                                        <directory>${project.build.directory}</directory>
                                        <include>${project.build.finalName}.jar</include>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    然后输入mvn package 就可以了,界面点的话就是这样
    在这里插入图片描述
    在这里插入图片描述
    这个是包含子项目的,分别点一下就好了

  • 相关阅读:
    0xc000007b应用程序无法正常启动win10解决方法
    计算机专业毕业设计项目推荐10-饮食搭配平台(Go+微信小程序+Mysql)
    【Vue】事件修饰符
    MySQL窗口函教-序号函数(row_number、rank、dense_rank)
    深入理解计算机系统:内存越界引用和缓冲区溢出
    【uniapp+vue3 】页面加载时根据不同角色设置导航栏标题
    通用返回结果类ResultVO
    Linux CentOS 8(用户管理)
    LeetCode | 391.完美矩形
    猿创征文|时间序列分析算法之二次指数平滑法和三次指数平滑法详解+Python代码实现
  • 原文地址:https://blog.csdn.net/c1719561053/article/details/126711820