Angular向Docker Engine API发送的HTTP请求(拉取镜像时的进度输出)
创始人
2024-10-30 03:30:19
0

要向Docker Engine API发送HTTP请求并获取拉取镜像时的进度输出,可以使用Angular的HttpClient模块来发送请求并监听进度事件。以下是一个示例代码:

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpEventType } from '@angular/common/http';

@Component({
  selector: 'app-image-pull',
  templateUrl: './image-pull.component.html',
  styleUrls: ['./image-pull.component.css']
})
export class ImagePullComponent implements OnInit {

  constructor(private http: HttpClient) { }

  ngOnInit() {
  }

  pullImage() {
    const imageName = 'nginx'; // 镜像名称

    this.http.post('http://localhost:2375/images/create', {
      fromImage: imageName
    }, { responseType: 'text', reportProgress: true }).subscribe(event => {
      if (event.type === HttpEventType.DownloadProgress) {
        const progress = Math.round(100 * event.loaded / event.total);
        console.log(`Progress: ${progress}%`);
      } else if (event.type === HttpEventType.Response) {
        console.log('Image pulled successfully!');
      }
    }, error => {
      console.error('Error pulling image:', error);
    });
  }
}

上述代码中,我们使用HttpClient的post方法来发送POST请求到Docker Engine API的/images/create端点,并在请求中传递镜像名称。我们设置responseType: 'text'以便获取文本响应。

在订阅请求的响应事件时,我们可以通过HttpEventType.DownloadProgress来检查进度事件,然后根据已加载和总大小计算进度百分比并输出到控制台。当响应事件的类型为HttpEventType.Response时,表示镜像已成功拉取。

请注意,你需要将http://localhost:2375替换为你的Docker Engine API的地址。

希望以上信息能对你有所帮助!

相关内容

热门资讯

经核实!aapoker辅助插件... 经核实!aapoker辅助插件工具(透视)青橙竞技游戏辅助(果然真的是有辅助安装)-哔哩哔哩1)青橙...
据报道!wepoker辅助器安... 据报道!wepoker辅助器安装包(透视)开心联盟辅助器免费(一直是有辅助工具)-哔哩哔哩;1、金币...
刚刚!aapoker脚本怎么用... 刚刚!aapoker脚本怎么用(透视)决战卡五星辅助看牌器(总是存在有辅助辅助器)-哔哩哔哩在进入决...
透视私人局!aapoker透视... 透视私人局!aapoker透视脚本(透视)潮汕雀友百度贴吧辅助软件(原来有辅助工具)-哔哩哔哩1、操...
此事引发网友热议!wpk系统是... 此事引发网友热议!wpk系统是否存在作必弊行为(透视)德州扑克游戏辅助(竟然真的有辅助神器)-哔哩哔...
针对!hhpoker是正品吗(... 针对!hhpoker是正品吗(透视)乐享麻将连连看辅助(一贯是真的辅助安装)-哔哩哔哩乐享麻将连连看...
透视安装!hhpoker万能辅... 透视安装!hhpoker万能辅助器(透视)阿拉游戏中心辅助工具(果然真的有辅助工具)-哔哩哔哩1、阿...
据通报!wpk辅助哪里买(透视... 据通报!wpk辅助哪里买(透视)创思维激k辅助器下载(原来是真的辅助辅助器)-哔哩哔哩1、创思维激k...
透视插件!hhpoker可以控... 透视插件!hhpoker可以控制牌吗(透视)朋友局辅助器(都是是真的辅助神器)-哔哩哔哩1、朋友局辅...
透视透视挂!哈糖大菠萝怎么挂(... 透视透视挂!哈糖大菠萝怎么挂(透视)福建天天开心辅助器(本来真的是有辅助平台)-哔哩哔哩福建天天开心...