文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

springboot构建docker镜像并推送到阿里云

2023-05-19 05:34

关注

1.构建springboot项目

工程目录如下

image.png

UserController

package com.fandf.test.controller;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
  
@RestController  
@RequestMapping("/api/v1/user")  
public class UserController {  
    @GetMapping("name")  
    public String getName() {  
        return "zhangsan";  
    }  
}

application.yml

server:  
  port: 9001

pom.xml

<?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">  
    <parent>  
        <artifactId>SpringCloudLearnin</artifactId>  
        <groupId>com.fandf</groupId>  
        <version>1.0.0</version>  
    </parent>  
    <modelVersion>4.0.0</modelVersion>  
    <artifactId>fdf-test</artifactId>  
    <properties>  
        <maven.compiler.source>8</maven.compiler.source>  
        <maven.compiler.target>8</maven.compiler.target>  
    </properties>  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
    </dependencies>  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
                <executions>  
                    <execution>  
                        <goals>  
                            <goal>repackage</goal>  
                        </goals>  
                    </execution>  
                </executions>  
                <configuration>  
                    <!-- 指定启动类 -->  
                    <mainClass>com.fandf.test.Application</mainClass>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>io.fabric8</groupId>  
                <artifactId>docker-maven-plugin</artifactId>  
                <configuration>  
                    <images>  
                        <image>  
                            <!-- 命名空间/仓库名称:镜像版本号-->  
                            <name>fandf/${project.name}:${project.version}</name>  
                            <alias>${project.name}</alias>  
                            <build>  
                            <!-- 指定dockerfile文件的位置-->  
                                <dockerFile>${project.basedir}/Dockerfile</dockerFile>  
                                <buildOptions>  
                                    <!-- 网络的配置,与宿主主机共端口号-->  
                                    <network>host</network>  
                                </buildOptions>  
                            </build>  
                        </image>  
                    </images>  
                </configuration>  
                <executions>  
                    <execution>  
                        <id>docker-exec</id>  
                        <!-- 绑定mvn deploy阶段,当执行mvn deploy时 就会执行docker build 和docker push-->  
                        <phase>deploy</phase>  
                        <goals>  
                            <goal>build</goal>  
                        <!-- <goal>push</goal>-->  
                        </goals>  
                    </execution>  
                </executions>  
            </plugin>  
        </plugins>  
    </build>  
</project>

插件如果无法下载,可引入依赖

<dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.40.1</version>
</dependency>

2.编写Dockerfile

# 基础镜像使用Java
FROM java:8
# 作者
MAINTAINER fandongfeng
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为app.jar
# 此处可以把具体的jar包名称写出来,我这里直接用*号代替了
ADD target/*.jar app.jar
# 指定容器需要映射到主机的端口
EXPOSE 9091
ENTRYPOINT ["java","-jar","app.jar"]

项目maven plugins会有docker插件,点击docekr build

image.png

镜像已被上传到本地

C:\Users\Administrator>docker images
REPOSITORY                           TAG                                                     IMAGE ID       CREATED             SIZE
fandf/fdf-test                       1.0.0                                                   1a4b935b4d57   About an hour ago   684MB
java                                 8                                                       d23bdf5b1b1b   6 years ago         643MB

3.推送镜像到阿里云

登录阿里云管控台 https://cr.console.aliyun.com/cn-chengdu/instances
选择容器镜像服务

image.png

新建镜像仓库

image.png

image.png

按照提示上传镜像

C:\Users\Administrator>docker login --username=17602117026 registry.cn-chengdu.aliyuncs.com
Password:
Login Succeeded
Logging in with your password grants your terminal complete access to your account.
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/
C:\Users\Administrator>docker images
REPOSITORY                           TAG                                                     IMAGE ID       CREATED             SIZE
fandf/fdf-test                       1.0.0                                                   1a4b935b4d57   About an hour ago   684MB
java                                 8                                                       d23bdf5b1b1b   6 years ago         643MB
C:\Users\Administrator>docker tag 1a4b935b4d57 registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
C:\Users\Administrator>docker push registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
The push refers to repository [registry.cn-chengdu.aliyuncs.com/fandf/k8s-test]
83d067b86ae9: Pushed
35c20f26d188: Pushed
c3fe59dd9556: Pushed
6ed1a81ba5b6: Pushed
a3483ce177ce: Pushed
ce6c8756685b: Pushed
30339f20ced0: Pushed
0eb22bfb707d: Pushed
a2ae92ffcd29: Pushed
1.0.0: digest: sha256:44a137e54a48e52252ff9ce64714de8311d206f1ffaf0f6ad39dd01ab1fe0455 size: 2212
C:\Users\Administrator>docker images
REPOSITORY                                        TAG                                                     IMAGE ID       CREATED             SIZE
fandf/fdf-test                                    1.0.0                                                   1a4b935b4d57   About an hour ago   684MB
registry.cn-chengdu.aliyuncs.com/fandf/k8s-test   1.0.0                                                   1a4b935b4d57   About an hour ago   684MB
java                                              8                                                       d23bdf5b1b1b   6 years ago         643MB
C:\Users\Administrator>

删除掉本地镜像,从阿里云拉取镜像并运行

C:\Users\Administrator>docker images
REPOSITORY                                        TAG                                                     IMAGE ID       CREATED         SIZE
fandf/fdf-test                                    1.0.0                                                   1a4b935b4d57   2 hours ago     684MB
registry.cn-chengdu.aliyuncs.com/fandf/k8s-test   1.0.0                                                   1a4b935b4d57   2 hours ago     684MB
java                                              8                                                       d23bdf5b1b1b   6 years ago     643MB
C:\Users\Administrator>docker rmi registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
Untagged: registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
Untagged: registry.cn-chengdu.aliyuncs.com/fandf/k8s-test@sha256:44a137e54a48e52252ff9ce64714de8311d206f1ffaf0f6ad39dd01ab1fe0455
C:\Users\Administrator>docker images
REPOSITORY                           TAG                                                     IMAGE ID       CREATED         SIZE
fandf/fdf-test                       1.0.0                                                   1a4b935b4d57   3 hours ago     684MB
java                                 8                                                       d23bdf5b1b1b   6 years ago     643MB
C:\Users\Administrator>docker pull registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
1.0.0: Pulling from fandf/k8s-test
Digest: sha256:44a137e54a48e52252ff9ce64714de8311d206f1ffaf0f6ad39dd01ab1fe0455
Status: Downloaded newer image for registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
C:\Users\Administrator>docker images
REPOSITORY                                        TAG                                                     IMAGE ID       CREATED         SIZE
fandf/fdf-test                                    1.0.0                                                   1a4b935b4d57   3 hours ago     684MB
registry.cn-chengdu.aliyuncs.com/fandf/k8s-test   1.0.0                                                   1a4b935b4d57   3 hours ago     684MB
java                                              8                                                       d23bdf5b1b1b   6 years ago     643MB
C:\Users\Administrator>docker run -d -p 9001:9001 registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
495b53c2b820bccd7ec34737a58604838cdbe8e0030b9eed57c63d5f81404bd2
C:\Users\Administrator>curl 127.0.0.1:9001/api/v1/user/name
zhangsan

到此这篇关于springboot构建docker镜像并推送到阿里云的文章就介绍到这了,更多相关springboot docker镜像内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯