docker跨平台部署java应用,13.Manifest实现多CPU架构Docker镜像(Docker 镜像跨平台使用)...

本文介绍了Docker的manifest文件及其清单列表(manifestlist)的概念,它们用于管理和存储不同操作系统和架构的镜像信息。manifestlist允许用户通过单一名称访问多架构镜像。文章详细阐述了manifestlist的处理流程,包括如何开启Docker客户端的manifest功能,创建并推送manifestlist到Dockerhub,以及如何检查和使用这些多架构镜像。
摘要由CSDN通过智能技术生成

1、manifest是什么,干什么用?

manifest是一个文件,这个文件包含了有关于镜像信息,如层、大小和摘要。docker manifest命令还向用户提供附加信息,比如构建镜像的操作系统和体系结构。而manifest list是一个镜像清单列表,用于存放多个不同os/arch的镜像信息。我们可以创建一个manifest list来指向两个镜像(一个linux 64位和一个指向arm64位的镜像),然后对用户提供一个唯一的镜像名称。从Docker registry v2.3和Docker 1.10 开始,Docker hub就可以pull multi architecture Docker镜像了。

一个镜像的manifest文件信息如下:

一个manifest list的例子如下:

注意:manifest的功能目前仅仅作用于docker 官方的镜像仓库。

总结:简单的说manifest list就是多个manifest的一个集合,通过列表方式来管理。

2、manifest list处理流程:

3320301

manifest-2.jpg

3320301

docker-manifest.png

3、开启docker子命令manifest功能:

manifest是做为docker客户端的子命令存在,不过这个子命令目前处在实验性中一般没有开启。我们需要手动开始这个子命令的功能。开启过程如下:

1)、编辑配置文件config.json应用实验性功能:

docker 的默认配置文件config.json是在$HOME目录下的.docker目录下。编辑config.json文件,若目录和文件不存在手动创建。

2)、编辑守护进程配置文件daemon.json开启实验性功能:

编辑daemon.json,若目录和文件不存在手动创建

3)、重启docker:

开启docker的实验性功能后docker pull可以拉取指定平台镜像如下:

4、使用manifest创建多CPU架构的镜像:

查看一个镜像的manifest文件信息

$docker manifest inspect java

查看一个镜像的manifest文件的详细信息包括cpu平台架构等信息

$docker manifest inspect --verbose java

这里准备好了两个不同CPU架构的镜像如下:

这里的镜像是自己在docker hub上创建的仓库

xxx/public_docker:nginx-arm64

xxx/public_docker:nginx-x86

将上面两个镜像推到docker hub上面

1)、创建一个manifest list列表:

创建一个自定义命名的镜像名的manifest list,然后用该列表关联仓库里面的两个不同架构的镜像

$docker manifest create xxx/public_docker:nginx-v1 xxx/public_docker:nginx-arm64 xxx/public_docker:nginx-x86

2)、将创建好的manifest list 推到仓库中:

$docker manifest push xxx/public_docker:nginx-v1

3)、查看仓库中创建好的manifest list:

$docker manifest inspect xxx/public_docker:nginx-v1

可以使用 Java 代码来获取 Docker 镜像manifest。以下是一个示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; import org.apache.commons.codec.binary.Hex; import org.apache.commons.io.IOUtils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class DockerManifest { public static void main(String[] args) throws NoSuchAlgorithmException, IOException { String imageName = "nginx"; // 镜像名称 String imageTag = "latest"; // 镜像标签 String manifest = getManifest(imageName, imageTag); System.out.println("Manifest: " + manifest); } public static String getManifest(String imageName, String imageTag) throws NoSuchAlgorithmException, IOException { String imageReference = imageName + ":" + imageTag; String encodedReference = Base64.getUrlEncoder().encodeToString(imageReference.getBytes()); String endpoint = "https://registry-1.docker.io/v2"; String path = "/" + imageName + "/manifests/" + imageTag; String url = endpoint + path; MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(("GET\n\n\napplication/vnd.docker.distribution.manifest.v2+json\n" + "Accept: application/vnd.docker.distribution.manifest.v2+json\n" + "Docker-Content-Digest:" + encodedReference + "\n" + path).getBytes(StandardCharsets.UTF_8)); String digest = Hex.encodeHexString(md.digest()); // 发送请求 OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .header("Accept", "application/vnd.docker.distribution.manifest.v2+json") .header("Authorization", "Bearer " + getAuthToken()) .header("Docker-Content-Digest", encodedReference) .header("Content-Type", "application/vnd.docker.distribution.manifest.v2+json") .header("Digest", "sha256:" + digest) .build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); Object json = gson.fromJson(responseBody, Object.class); return gson.toJson(json); } private static String getAuthToken() throws IOException { String endpoint = "https://auth.docker.io/token"; String scope = "repository:library/*:pull"; String service = "registry.docker.io"; String url = endpoint + "?scope=" + scope + "&service=" + service; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); Gson gson = new Gson(); AuthTokenResponse authTokenResponse = gson.fromJson(responseBody, AuthTokenResponse.class); return authTokenResponse.getToken(); } static class AuthTokenResponse { private String token; public String getToken() { return token; } public void setToken(String token) { this.token = token; } } } ``` 该代码使用了 okhttp 和 Gson 库来发送 HTTP 请求并解析 JSON 响应。您需要将 `imageName` 和 `imageTag` 替换为您自己的值。执行该代码后,将输出 Docker 镜像manifest
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值