Java開發提高十倍生產力:Idea遠程一鍵部署springboot到Docker

     Idea是Java開發利器,springboot是Java生態中最流行的微服務框架,docker是時下最火的容器技術,那麼它們結合在一塊兒會產生什麼化學反應呢?java

1、開發前準備

1. Docker的安裝能夠參考https://docs.docker.com/install/

2. 配置docker遠程鏈接端口

vi /usr/lib/systemd/system/docker.service
複製代碼

找到 ExecStart,在最後面添加 -H tcp://0.0.0.0:2375,以下圖所示web

3. 重啓docker

systemctl daemon-reload
 systemctl start docker
複製代碼

4. 開放端口

firewall-cmd --zone=public --add-port=2375/tcp --permanent  
複製代碼

5. Idea安裝插件,重啓

6. 鏈接遠程docker

   (1) 編輯配置

   (2) 填遠程docker地址

   (3) 鏈接成功,會列出遠程docker容器和鏡像

2、新建項目

1. 建立springboot項目

     項目結構圖spring

  (1) 配置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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>docker-demo</groupId>
    <artifactId>com.demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <docker.image.prefix>com.demo</docker.image.prefix>
         <java.version>1.8</java.version>
    </properties>
    <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.0.0</version>
           <configuration>
              <dockerDirectory>src/main/docker</dockerDirectory>
              <resources>
                <resource>
                    <targetPath>/</targetPath>
                    <directory>${project.build.directory}</directory>
                    <include>${project.build.finalName}.jar</include>
                </resource>
              </resources>
           </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                 <execution>
                     <phase>package</phase>
                    <configuration>
                        <tasks>
                            <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"></copy>
                        </tasks>
                     </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    </execution>
            </executions>
        </plugin>

       </plugins>
    </build>
<dependencies>
    <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>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>
</project>
複製代碼

  (2) 在src/main目錄下建立docker目錄,並建立Dockerfile文件

FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
複製代碼

  (3) 在resource目錄下建立application.properties文件

logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990
複製代碼

  (4) 建立DockerApplication文件

@SpringBootApplication
public class DockerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DockerApplication.class, args);
    }
}
複製代碼

  (5) 建立DockerController文件

@RestController
public class DockerController {
    static Log log = LogFactory.getLog(DockerController.class);

    @RequestMapping("/")
    public String index() {
        log.info("Hello Docker!");
        return "Hello Docker!";
    }
}
複製代碼

  (6) 增長配置

  命令解釋
      Image tag : 指定鏡像名稱和tag,鏡像名稱爲 docker-demotag1.1
      Bind ports : 綁定宿主機端口到容器內部端口。格式爲[宿主機端口]:[容器內部端口]
      Bind mounts : 將宿主機目錄掛到到容器內部目錄中。格式爲[宿主機目錄]:[容器內部目錄]。這個springboot項目會將日誌打印在容器 /home/developer/app/logs/ 目錄下,將宿主機目錄掛載到容器內部目錄後,那麼日誌就會持久化容器外部的宿主機目錄中。docker

  (7) Maven打包

  (8) 運行

先pull基礎鏡像,而後再打包鏡像,並將鏡像部署到遠程docker運行

這裏咱們能夠看到鏡像名稱爲docker-demo:1.1,docker容器爲docker-server

  (9) 運行成功

  (10) 瀏覽器訪問

  (11) 日誌查看

  自此經過idea 部署springboot項目到docker成功!不可思議,部署一個Javaweb項目居然如此簡單方便!apache

做者:陶章好
連接:juejin.im/post/5d0262…
來源:掘金
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。瀏覽器

以爲對您有幫助請點 "贊"

相關文章
相關標籤/搜索