Docker 环境下如何部署jar包(SpringBoot jar)

 

打包配置

  1. pom.xml 保证jar包内包含了所有依赖jar内容 采用springboot官方推荐的打包方式即可,如下
    <build>
        <!-- 输出的 jar 不带版本号,方便启动脚本调用 -->
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 生成可执行 jar -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 使用 assembly 打 zip 包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>create-distribution</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <!-- 最终 zip 包文件名,带版本号 -->
                            <finalName>${project.artifactId}-${project.version}</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>assembly/zip.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    打包完成后将jar和配置放入docker配置的指定目录中

 

部署配置

配置docker-compoes.yml(注意这里docker-compoes 需要单独安装插件)

version: '3'

services:
    rzbd: #别名
        image: tomcat:8.5.66-jdk8-adoptopenjdk-openj9 #镜像
        container_name: rzbd #容器名称
        #这里是重点 这里执行了java -jar 命令 并指定了对应的配置文件(docker容器内的文件地址---地址在下面一行的volumes配置中做了映射)
        command: /bin/sh -c "sleep 6;java -jar  /xxx/applications/rzbd.jar  --spring.config.location=/xxx/applications/config/application.properties"       
        #映射配置
        volumes:
            - /xxx/plat_distribute/rzbd:/xxx/applications           
            - /xxx/log/rzbd:/icooper/log/rzbd
            - /xxx/plat_distribute/rzbd/config:/xxx/applications/config
            - /xxx/appdata/:/xxx/appdata/ 
        #这个不重要这是针对tomcat的配置内容
        env_file:
            - tomcat.env
        #指定同一宿主机容器间的通信别名可以理解为这是一个ip   
        networks:
            scnet:
                aliases:
                    - rzbd
#这里和上面别名配置一致
networks:
   scnet:
     external: true
 
配置完成后更改名称将文件放到/xxx/xxx/xx内
执行语句创建容器
docker-compose -f yml指定路径 up -d

修改nginx配置(/xxx/xxx/nginx/config/nginx.conf)

添加对应的路由映射地址
posted @ 2022-08-01 17:10  搏一搏摩托变摩托  阅读(1561)  评论(0)    收藏  举报