IDEA中进行Docker项目部署的实现步骤
这篇文章主要介绍了IDEA中进行Docker项目部署的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
现在大部分项目已经开始部署在Docker上面了,可是部署环节还是有点麻烦,所以本文想讲解一下如何使用IDEA一键部署。
Docker配置
修改配置文件
1 2 3 4 5 6 7 8 9 | 打开Docker的配置文件: vim /usr/lib/systemd/system/docker .service 将下面这行注释掉: # 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 重新加载配置文件和启动: systemctl daemon-reload systemctl start docker |
如下图所示:
IDEA配置Docker
安装Docker插件
配置Docker信息
在设置中进行Docker配置,需要配置API URL,下面出现Connection successful即可:
项目搭建
新建一个springboot项目
通过IDEA搭建项目,什么都不需要选择,一直下一步就可以了:
修改pom文件
最主要的就是两点:
1.properties标签中添加
1 | < docker.image.prefix >demo</ docker.image.prefix > |
2.添加新的plugin标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < plugin > < groupId >com.spotify</ groupId > < artifactId >docker-maven-plugin</ artifactId > < version >1.2.1</ version > < configuration > < imageName >${docker.image.prefix}/${project.artifactId}</ imageName > < dockerDirectory ></ dockerDirectory > < resources > < resource > < targetPath >/</ targetPath > < directory >${project.build.directory}</ directory > < include >${project.build.finalName}.jar</ include > </ resource > </ resources > </ configuration > </ plugin > |
下面是完整的pom文件:
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <? 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.2.1.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.example</ groupId > < artifactId >demo</ artifactId > < version >0.0.1</ version > < name >demo</ name > < description >Demo project for Spring Boot</ description > < properties > < java.version >1.8</ java.version > < docker.image.prefix >demo</ docker.image.prefix > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > < exclusions > < exclusion > < groupId >org.junit.vintage</ groupId > < artifactId >junit-vintage-engine</ artifactId > </ exclusion > </ exclusions > </ dependency > </ dependencies > < build > < 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.1</ version > < configuration > < imageName >${docker.image.prefix}/${project.artifactId}</ imageName > < dockerDirectory ></ dockerDirectory > < resources > < resource > < targetPath >/</ targetPath > < directory >${project.build.directory}</ directory > < include >${project.build.finalName}.jar</ include > </ resource > </ resources > </ configuration > </ plugin > </ plugins > </ build > </ project > |
新建Dockerfile文件
需要在根目录下新建Dockerfile文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #指定基础镜像,在其上进行定制 FROM java:8 #维护者信息 MAINTAINER zhouzhaodong <xiuaiba@163.com> #这里的 /tmp 目录就会在运行时自动挂载为匿名卷,任何向 /tmp 中写入的信息都不会记录进容器存储层 VOLUME /tmp #复制上下文目录下的target/demo-1.0.0.jar 到容器里 COPY target /demo-0 .0.1.jar demo-1.0.0.jar #bash方式执行,使demo-1.0.0.jar可访问 #RUN新建立一层,在其上执行这些命令,执行结束后, commit 这一层的修改,构成新的镜像。 RUN bash -c "touch /demo-1.0.0.jar" #声明运行时容器提供服务端口,这只是一个声明,在运行时并不会因为这个声明应用就会开启这个端口的服务 EXPOSE 8080 #指定容器启动程序及参数 <ENTRYPOINT> "<CMD>" ENTRYPOINT [ "java" , "-jar" , "demo-1.0.0.jar" ] |
新建controller文件
别忘记在pom文件中添加web依赖。
1 2 3 4 5 6 7 8 9 | @RestController public class testController { @RequestMapping ( "/" ) public String test(){ return "test Docker" ; } } |
maven打包
install打包:
生成jar包:
新建配置
新建Dockerfile运行配置:
添加如下信息,找到自己写的Dockerfile文件,配置端口映射:
生成Docker镜像并运行
直接运行刚才新建的Dockerfile运行配置即可:
运行成功后log窗口会出现项目运行的信息:
访问对应的地址即可显示我们输入的信息:
以上就是本文的全部内容,希望对大家的学习有所帮助