IDEA 使用 Docker插件运行SpringBoot工程

IDEA 使用 Docker插件运行SpringBoot工程

1、安装Docker插件

由于我安装的IDEA版本是2020.1.x,所以内部集成了Docker插件,故无需安装。如果你的IDEA版本没有预装,需要自行安装。

2、配置Docker地址

在IDEA的设置中找到Docker,进行如下配置:

Engine API URL配置tcp://ip:2375,如果开启了CA证书则是https://ip:2375

Certificates folder为可选项,如果开启了CA证书需要配置(一般公网IP的服务器才会开启CA证书。如果是自己在本地搭建的Docker可以忽略此配置)

点击apply后如果出现了Connection successful则标识连接成功。

3、创建一个SpringBoot项目

此项目无特殊要求,但maven配置文件中需要加入如下内容:

    <build>
        <plugins>
            <plugin>
                <!-- docker 必须加 否则报no main manifest attribute, in /xxx.jar错-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                    <mainClass>cn.vantee.VanteeApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

4、创建Dockerfile

在项目的src同级,创建一个名为Dockerfile的文件(无后缀)

5、编辑Dockerfile

# 镜像基于java:8
FROM java:8

# 作者
MAINTAINER rayfoo@qq.compp

#创建项目日志存放的文件夹
RUN mkdir -p /app

# 匿名挂载目录
VOLUME /app

# 项目暴露的端口号
EXPOSE 10000

# 添加环境变量
ENV JAVA_OPTS="-Xms512m -Xmx512m"

# 将jar包添加到容器中 /app/目录并更名为xxx.jar
ADD ./target/Vantee-Admin-0.0.1.jar /app/Vantee-Admin-0.0.1.jar

# 运行jar包命令 "nohup" "&" 可省略
# -Djava.security.egd=file:/dev/./urandom 加速tomcat启动
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/Vantee-Admin-0.0.1.jar", "--spring.profiles.active=dev", "--server.port=10000"]

6、修改运行配置

6.1、打开运行设置,编辑配置

6.2、点击左上角的,添加一个Dockerfile

6.3、按需指定Dockerfile文件,配置镜像名称、版本、容器名称、启动参数等信息 点击apply保存

7、运行

确认配置无误后,点击Run或Debug即可运行。

点击Run或Debug后,可以点击services查看容器构建、运行情况,以及项目的日志。

8、通过ssh查看日志

# 语法
docker logs -f -t --tail 行数 容器名

# 案例
docker logs -f -t --tail 200 spring-boot-demo

9、常见错误

Deploying 'vantee-admin Dockerfile: Vantee-Admin/Dockerfile'...
Building image...
Preparing build context archive...
[==================================================>]16/16 files
Done

Sending build context to Docker daemon...
[==================================================>] 27.31MB
Done

Step 1/6 : FROM java:8
Error response from daemon: Get https://registry-1.docker.io/v2/: x509: certificate has expired or is not yet valid
Failed to deploy 'vantee-admin Dockerfile: Vantee-Admin/Dockerfile': Can't retrieve image ID from build stream

解决:检查服务器是否配置阿里云镜像加速,是否正确配置DNS。

原文地址:https://www.cnblogs.com/zhangruifeng/p/14772932.html