IDEA+Docker插件一键部署SpringBoot项目到远程服务器

发布于:2025-03-19 ⋅ 阅读:(9) ⋅ 点赞:(0)

1. 服务端

1.1 安装Docker

默认安装Docker,直接跳过

1.2 Docker放开远程连接

[root@localhost ~]# vim /usr/lib/systemd/system/docker.service

## 添加 -H tcp://0.0.0.0:2375
	......
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
	......

1.3 重启Docker

systemctl daemon-reload
systemctl start docker

1.4 开放端口

1.4.1 云端

开放防火墙的 9090 端口

  • 如果你使用的是云服务器,在安全组中放行 9090 端口
  • 如果你安装了宝塔,除了在安全组中放行 9090 端口,还要在宝塔中放行 9090
    端口

1.4.2 服务器内部防火墙指令

# ubuntu
sudo ufw allow 9090
sudo ufw reload

# centos
sudo firewall-cmd --zone=public --add-port=9090 /tcp --permanent
sudo firewall-cmd --reload

2.IntelliJ IDEA

版本:IntelliJ IDEA 2023.1 (Ultimate Edition)

需要部署时候,建议使用管理员运行IDEA

2.1 安装IDEA

默认安装IDEA,跳过安装

2.2 安装Docker插件

File | Settings | Plugins
docker插件

2.3 SSH Configurations

File | Settings | Tools | SSH Configurations

SSH配置

2.4 Docker选择对应的SSH

File | Settings | Build, Execution, Deployment | Docker
Docker_SSH

2.5 Dockerfile

fdy-live-user-provider/docker/Dockerfile
dockerfile

2.5.1 Dockerfile

FROM openjdk:17-jdk-alpine
VOLUME /tmp
COPY target/fdy-live-user-provider-docker.jar /app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

2.5.2 Dockerfile Edit Run Configuration

编辑Dockerfile配置框
Dockerfile配置框
重点:Context folder 里面填写项目的根<fdy-live-user-provider>, 默认fdy-live-user-provider/docker,会导致检索不了target文件夹,从而获取不了对应的Jar包

2.5.2.1 Maven全局指令

Maven

clean package -Dautoconfig.interative=false -Dmaven.test.skip=true -Dmaven.javadoc.skip=true

2.5.2.2 配置镜像映射端口

端口

2.5.2.3 补充说明1

dockerfile配置说明

2.5.2.4 补充说明2

启动说明

2.6 Maven

2.6.1 安装Maven

默认安装,跳过

2.6.2 pom.xml 相关Docker部署配置

重点:<build>标签部分

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.idea</groupId>
        <artifactId>fdy-live-app</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <version>1.0.1</version>

    <artifactId>fdy-live-user-provider</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
    </properties>

    <dependencies>
    </dependencies>

    <build>
        <finalName>${artifactId}-docker</finalName>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.2</version>
                <executions>
                    <!-- 当mvn执行install操作的时候,执行docker的build -->
                    <execution>
                        <id>build</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--覆盖相同标签镜像-->
                    <forceTags>true</forceTags>
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                    </imageTags>
                    <imageName>${project.build.finalName}</imageName>
                    <!--指定Dockerfile文件的位置-->
                    <dockerDirectory>${project.basedir}/docker</dockerDirectory>
                    <!-- 指定jar包路径,这里对应Dockerfile中复制 jar 包到 docker 容器指定目录配置,也可以写到 Docokerfile 中 -->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!-- 将下边目录的内容,拷贝到docker镜像中 -->
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- 项目基于maven pom多模块的开发的,需要设置goal-repackage属性为true,否则打包后文件依赖文件没有一起打包,然后镜像内没有可以运行的程序文件 -->
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

网站公告

今日签到

点亮在社区的每一天
去签到