springboot構建docker鏡像并推送到阿里云
1.構建springboot項目
工程目錄如下

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>
<!-- 網(wǎng)絡的配置,與宿主主機共端口號-->
<network>host</network>
</buildOptions>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>docker-exec</id>
<!-- 綁定mvn deploy階段,當執(zhí)行mvn deploy時 就會執(zhí)行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 目錄下創(chuàng)建了一個臨時文件,并鏈接到容器的/tmp VOLUME /tmp # 將jar包添加到容器中并更名為app.jar # 此處可以把具體的jar包名稱寫出來,我這里直接用*號代替了 ADD target/*.jar app.jar # 指定容器需要映射到主機的端口 EXPOSE 9091 ENTRYPOINT ["java","-jar","app.jar"]
項目maven plugins會有docker插件,點擊docekr build

鏡像已被上傳到本地
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
選擇容器鏡像服務

新建鏡像倉庫


按照提示上傳鏡像
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鏡像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Junit 5中@ParameterizedTest與@EnumSource結合使用
今天小編就為大家分享一篇關于Junit 5中@ParameterizedTest與@EnumSource結合使用,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Spring?Boot?Admin?監(jiān)控指標接入Grafana可視化的實例詳解
Spring Boot Admin2 自帶有部分監(jiān)控圖表,如圖,有線程、內(nèi)存Heap和內(nèi)存Non Heap,這篇文章主要介紹了Spring?Boot?Admin?監(jiān)控指標接入Grafana可視化,需要的朋友可以參考下2022-11-11
Spring中@Async注解實現(xiàn)異步調(diào)詳解
在本篇文章里小編給大家分享的是關于Spring中@Async注解實現(xiàn)異步調(diào)詳解內(nèi)容,需要的朋友們可以學習下。2020-04-04

