当前位置 主页 > 服务器问题 > Linux/apache问题 >

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

    栏目:Linux/apache问题 时间:2019-11-29 09:58

    现在大部分项目已经开始部署在Docker上面了,可是部署环节还是有点麻烦,所以本文想讲解一下如何使用IDEA一键部署。

    Docker配置

    修改配置文件

    打开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标签中添加

    <docker.image.prefix>demo</docker.image.prefix>

    2.添加新的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>

    下面是完整的pom文件:

    <?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>