云原生之容器编排实践-SpringBoot应用Docker化

简介: 云原生之容器编排实践-SpringBoot应用Docker化
+关注继续查看

`5E3C]3H$FC(ES[2IT)0@KE.png

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第6天,点击查看活动详情


背景


前面我们已经通过 IDEADocker 插件连接到了 Docker 服务。这里我们借助 DockerfileMaven 打包插件实现一键部署 Spring Boot 应用到远程 Docker 容器。


创建应用


  • 创建一个SpringBoot的空项目

略。。

  • 写一个简单的接口


package com.heartsuit.cloudnative.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Author Heartsuit
 * @Date 2022-08-22
 */
@RestController
@Slf4j
public class HelloController {
    @Value("${server.port}")
    private String port;
    @GetMapping("/hi")
    public String hello() {
        return "Hi " + port;
    }
    @GetMapping("/hello")
    public String hello(@RequestParam String name) {
        log.info("Parameter: {}", name);
        return "Hello " + name + ", I am on port: " + port;
    }
}
  • 编写Dockerfile

在项目根目录新增 Dockerfile 文件,写入以下内容。



FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

说明:


  1. FROM:基于openjdk8
  2. VOLUME:临时数据卷目录
  3. COPY:拷贝target目录下的jar,命名为app.jar
  4. ENTRYPOINT:配置启动命令
  • 增加打包插件 编辑 pom.xml ,在插件部分添加以下内容。


<plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.0</version>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <dockerHost>http://k8s0:2375</dockerHost>
                    <imageName>heartsuit/${project.artifactId}</imageName>
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                    </imageTags>
                    <forceTags>true</forceTags>
                    <dockerDirectory>${project.basedir}</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

Note:源码已上传 GitHubgithub.com/heartsuit/c…


运行容器应用


一种方式是,直接在打开的 Dockerfile 中,点击运行,会将应用直接在容器中部署启动。


W8TCLMSE[$5GW9FQ6DUXJSI.png

一键部署应用到容器


执行 mvn package 命令,或者通过 Maven 插件的 package 命令完成一键打包与部署到容器的操作。


If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!

目录
相关文章
|
13天前
|
Cloud Native 容器
云原生容器Clouder认证:基于容器搭建企业级应用—课时9:实验练习和参加考试
云原生容器Clouder认证:基于容器搭建企业级应用—课时9:实验练习和参加考试
45 0
|
13天前
|
Cloud Native 安全 虚拟化
云原生容器Clouder认证:基于容器搭建企业级应用—课时8:课程总结
云原生容器Clouder认证:基于容器搭建企业级应用—课时8:课程总结
27 0
|
13天前
|
Cloud Native 关系型数据库 数据库
云原生容器Clouder认证:基于容器搭建企业级应用—课时7:运行容器应用
云原生容器Clouder认证:基于容器搭建企业级应用—课时7:运行容器应用
38 0
|
13天前
|
Cloud Native 安全 数据安全/隐私保护
云原生容器Clouder认证:基于容器搭建企业级应用—课时6:发布容器镜像
云原生容器Clouder认证:基于容器搭建企业级应用—课时6:发布容器镜像
26 0
|
13天前
|
运维 Cloud Native Linux
云原生容器Clouder认证:基于容器搭建企业级应用—课时5:制作容器镜像
云原生容器Clouder认证:基于容器搭建企业级应用—课时5:制作容器镜像
24 0
|
13天前
|
弹性计算 Cloud Native Linux
云原生容器Clouder认证:基于容器搭建企业级应用—课时4:容器如何使用
云原生容器Clouder认证:基于容器搭建企业级应用—课时4:容器如何使用
28 0
|
13天前
|
Cloud Native Linux 虚拟化
云原生容器Clouder认证:基于容器搭建企业级应用—课时3:容器是什么
云原生容器Clouder认证:基于容器搭建企业级应用—课时3:容器是什么
23 0
|
13天前
|
运维 Cloud Native 虚拟化
云原生容器Clouder认证:基于容器搭建企业级应用—课时2:为什么需要容器
云原生容器Clouder认证:基于容器搭建企业级应用—课时2:为什么需要容器
33 0
|
13天前
|
Cloud Native 容器
云原生容器Clouder认证:基于容器搭建企业级应用—课时1:课程及场景概述
云原生容器Clouder认证:基于容器搭建企业级应用—课时1:课程及场景概述
25 0
|
15天前
|
Cloud Native 容器
云原生容器Clouder认证:容器应用与集群管理—课时11:实验练习与参加考试
云原生容器Clouder认证:容器应用与集群管理—课时11:实验练习与参加考试
28 0
相关产品
云迁移中心
推荐文章
更多