gpt4 book ai didi

IDEA中进行Docker项目部署的实现步骤

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章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项目部署的实现步骤

idea配置docker 。

安装docker插件 。

IDEA中进行Docker项目部署的实现步骤

配置docker信息 。

在设置中进行docker配置,需要配置api url,下面出现connection successful即可:

IDEA中进行Docker项目部署的实现步骤

项目搭建 。

新建一个springboot项目 。

通过idea搭建项目,什么都不需要选择,一直下一步就可以了:

IDEA中进行Docker项目部署的实现步骤

修改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打包:

IDEA中进行Docker项目部署的实现步骤

生成jar包:

IDEA中进行Docker项目部署的实现步骤

新建配置 。

新建dockerfile运行配置:

IDEA中进行Docker项目部署的实现步骤

添加如下信息,找到自己写的dockerfile文件,配置端口映射:

IDEA中进行Docker项目部署的实现步骤

生成docker镜像并运行 。

直接运行刚才新建的dockerfile运行配置即可:

IDEA中进行Docker项目部署的实现步骤

运行成功后log窗口会出现项目运行的信息:

IDEA中进行Docker项目部署的实现步骤

访问对应的地址即可显示我们输入的信息:

IDEA中进行Docker项目部署的实现步骤

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:https://segmentfault.com/a/1190000021052749 。

最后此篇关于IDEA中进行Docker项目部署的实现步骤的文章就讲到这里了,如果你想了解更多关于IDEA中进行Docker项目部署的实现步骤的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com