springboot構(gòu)建docker鏡像并推送到阿里云
1.構(gòu)建springboot項(xiàng)目
工程目錄如下

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>
<!-- 指定啟動(dòng)類 -->
<mainClass>com.fandf.test.Application</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<!-- 命名空間/倉(cāng)庫(kù)名稱:鏡像版本號(hào)-->
<name>fandf/${project.name}:${project.version}</name>
<alias>${project.name}</alias>
<build>
<!-- 指定dockerfile文件的位置-->
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
<buildOptions>
<!-- 網(wǎng)絡(luò)的配置,與宿主主機(jī)共端口號(hào)-->
<network>host</network>
</buildOptions>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>docker-exec</id>
<!-- 綁定mvn deploy階段,當(dāng)執(zhí)行mvn deploy時(shí) 就會(huì)執(zhí)行docker build 和docker push-->
<phase>deploy</phase>
<goals>
<goal>build</goal>
<!-- <goal>push</goal>-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>插件如果無(wú)法下載,可引入依賴
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.40.1</version>
</dependency>2.編寫Dockerfile
# 基礎(chǔ)鏡像使用Java FROM java:8 # 作者 MAINTAINER fandongfeng # VOLUME 指定了臨時(shí)文件目錄為/tmp。 # 其效果是在主機(jī) /var/lib/docker 目錄下創(chuàng)建了一個(gè)臨時(shí)文件,并鏈接到容器的/tmp VOLUME /tmp # 將jar包添加到容器中并更名為app.jar # 此處可以把具體的jar包名稱寫出來(lái),我這里直接用*號(hào)代替了 ADD target/*.jar app.jar # 指定容器需要映射到主機(jī)的端口 EXPOSE 9091 ENTRYPOINT ["java","-jar","app.jar"]
項(xiàng)目maven plugins會(huì)有docker插件,點(diǎn)擊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.推送鏡像到阿里云
登錄阿里云管控臺(tái) https://cr.console.aliyun.com/cn-chengdu/instances
選擇容器鏡像服務(wù)

新建鏡像倉(cāng)庫(kù)


按照提示上傳鏡像
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>
刪除掉本地鏡像,從阿里云拉取鏡像并運(yùn)行
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
到此這篇關(guān)于springboot構(gòu)建docker鏡像并推送到阿里云的文章就介紹到這了,更多相關(guān)springboot docker鏡像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合docker部署實(shí)現(xiàn)兩種構(gòu)建Docker鏡像方式
- Springboot打包為Docker鏡像并部署的實(shí)現(xiàn)
- SpringBoot打包docker鏡像發(fā)布的詳細(xì)步驟
- Docker 部署 SpringBoot 項(xiàng)目整合 Redis 鏡像做訪問計(jì)數(shù)示例代碼
- 詳解SpringBoot構(gòu)建Docker鏡像的3種方式
- SpringBoot創(chuàng)建Docker鏡像的方法步驟
- Springboot微服務(wù)打包Docker鏡像流程解析
- 一步步教你把SpringBoot項(xiàng)目打包成Docker鏡像
相關(guān)文章
Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用
今天小編就為大家分享一篇關(guān)于Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Spring?Boot?Admin?監(jiān)控指標(biāo)接入Grafana可視化的實(shí)例詳解
Spring Boot Admin2 自帶有部分監(jiān)控圖表,如圖,有線程、內(nèi)存Heap和內(nèi)存Non Heap,這篇文章主要介紹了Spring?Boot?Admin?監(jiān)控指標(biāo)接入Grafana可視化,需要的朋友可以參考下2022-11-11
Spring中@Async注解實(shí)現(xiàn)異步調(diào)詳解
在本篇文章里小編給大家分享的是關(guān)于Spring中@Async注解實(shí)現(xiàn)異步調(diào)詳解內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-04-04

