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

1.构建springboot项目

工程目录如下


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


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;

/**
* @author fandongfeng
*/
@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


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


镜像已被上传到本地

C:UsersAdministrator>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
选择容器镜像服务


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


新建镜像仓库


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



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


按照提示上传镜像

C:UsersAdministrator>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:UsersAdministrator>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:UsersAdministrator>docker tag 1a4b935b4d57 registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0

C:UsersAdministrator>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:UsersAdministrator>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:UsersAdministrator>

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

C:UsersAdministrator>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:UsersAdministrator>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:UsersAdministrator>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:UsersAdministrator>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:UsersAdministrator>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:UsersAdministrator>docker run -d -p 9001:9001 registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
495b53c2b820bccd7ec34737a58604838cdbe8e0030b9eed57c63d5f81404bd2

C:UsersAdministrator>curl 127.0.0.1:9001/api/v1/user/name
zhangsan


原文始发于微信公众号(好好学技术):springboot构建docker镜像并推送到阿里云

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/219542.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!