package main
import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func createDockerTarFile(dockerfile string) (io.Reader, error) {
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
f, err := os.Open(dockerfile)
if err != nil {
return nil, err
}
defer f.Close()
body, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
hdr := &tar.Header{
Name: f.Name(),
Mode: 0600,
Size: int64(len(body)),
}
tw.WriteHeader(hdr)
_,err = tw.Write(body)
if err != nil {
return nil, err
}
return &buf, nil
}
func BuildImage(cli *client.Client, dockerfile, project, imageName string,tagVersion string) error {
dockerBuildContext, err := createDockerTarFile(dockerfile)
if err != nil {
return err
}
tags := fmt.Sprintf("%s:%s",imageName,tagVersion)
buildOptions := types.ImageBuildOptions{
Dockerfile: "Dockerfile", // optional, is the default
Tags: []string{tags},
Labels: map[string]string{
project: "project",
},
}
output, err := cli.ImageBuild(context.Background(), dockerBuildContext, buildOptions)
if err != nil {
return err
}
body, err := ioutil.ReadAll(output.Body)
if err != nil {
return err
}
fmt.Printf("Build resource image output: %v\n", string(body))
if strings.Contains(string(body), "error") {
return fmt.Errorf("build image to docker error")
}
return nil
}
func main() {
//ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
err = BuildImage(cli, "Dockerfile", "test", "nginx","latest")
if err != nil {
panic(err)
}
}
#dockerfile文件內容
FROM nginx
#構建二進制文件
go build -o build dockerBuildTools.go
#執行結果
sudo ./build
Build resource image output: {"stream":"Step 1/2 : FROM nginx"}
{"stream":"\n"}
{"status":"Pulling from library/nginx","id":"latest"}
{"status":"Pulling fs layer","progressDetail":{},"id":"33847f680f63"}
{"status":"Pulling fs layer","progressDetail":{},"id":"dbb907d5159d"}
{"status":"Pulling fs layer","progressDetail":{},"id":"8a268f30c42a"}
{"status":"Pulling fs layer","progressDetail":{},"id":"b10cf527a02d"}
{"status":"Pulling fs layer","progressDetail":{},"id":"c90b090c213b"}
{"status":"Pulling fs layer","progressDetail":{},"id":"1f41b2f2bf94"}
{"status":"Waiting","progressDetail":{},"id":"b10cf527a02d"}
{"status":"Waiting","progressDetail":{},"id":"c90b090c213b"}
{"status":"Downloading","progressDetail":{"current":601,"total":601},"progress":"[==================================================\u003e] 601B/601B","id":"8a268f30c42a"}
{"status":"Verifying Checksum","progressDetail":{},"id":"8a268f30c42a"}
{"status":"Download complete","progressDetail":{},"id":"8a268f30c42a"}
{"status":"Downloading","progressDetail":{"current":277744,"total":27145795},"progress":"[\u003e ] 277.7kB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":277743,"total":26596573},"progress":"[\u003e ] 277.7kB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":556272,"total":27145795},"progress":"[=\u003e ] 556.3kB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":834800,"total":27145795},"progress":"[=\u003e ] 834.8kB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":556271,"total":26596573},"progress":"[=\u003e ] 556.3kB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":834799,"total":26596573},"progress":"[=\u003e ] 834.8kB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":1108406,"total":27145795},"progress":"[==\u003e ] 1.108MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":2775478,"total":27145795},"progress":"[=====\u003e ] 2.775MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":1113327,"total":26596573},"progress":"[==\u003e ] 1.113MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":3058102,"total":27145795},"progress":"[=====\u003e ] 3.058MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":1391855,"total":26596573},"progress":"[==\u003e ] 1.392MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":4180406,"total":27145795},"progress":"[=======\u003e ] 4.18MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":2775477,"total":26596573},"progress":"[=====\u003e ] 2.775MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":5302710,"total":27145795},"progress":"[=========\u003e ] 5.303MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":658,"total":893},"progress":"[====================================\u003e ] 658B/893B","id":"b10cf527a02d"}
{"status":"Downloading","progressDetail":{"current":893,"total":893},"progress":"[==================================================\u003e] 893B/893B","id":"b10cf527a02d"}
{"status":"Verifying Checksum","progressDetail":{},"id":"b10cf527a02d"}
{"status":"Download complete","progressDetail":{},"id":"b10cf527a02d"}
{"status":"Downloading","progressDetail":{"current":6142390,"total":27145795},"progress":"[===========\u003e ] 6.142MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":3054005,"total":26596573},"progress":"[=====\u003e ] 3.054MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":7256502,"total":27145795},"progress":"[=============\u003e ] 7.257MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":8653238,"total":27145795},"progress":"[===============\u003e ] 8.653MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":9775542,"total":27145795},"progress":"[==================\u003e ] 9.776MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":3598773,"total":26596573},"progress":"[======\u003e ] 3.599MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":4422069,"total":26596573},"progress":"[========\u003e ] 4.422MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":10054070,"total":27145795},"progress":"[==================\u003e ] 10.05MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":13158838,"total":27145795},"progress":"[========================\u003e ] 13.16MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":4688309,"total":26596573},"progress":"[========\u003e ] 4.688MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":6638005,"total":26596573},"progress":"[============\u003e ] 6.638MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":13711798,"total":27145795},"progress":"[=========================\u003e ] 13.71MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":15116726,"total":27145795},"progress":"[===========================\u003e ] 15.12MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":7739829,"total":26596573},"progress":"[==============\u003e ] 7.74MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":8018357,"total":26596573},"progress":"[===============\u003e ] 8.018MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":15677878,"total":27145795},"progress":"[============================\u003e ] 15.68MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":19036598,"total":27145795},"progress":"[===================================\u003e ] 19.04MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":658,"total":665},"progress":"[=================================================\u003e ] 658B/665B","id":"c90b090c213b"}
{"status":"Downloading","progressDetail":{"current":665,"total":665},"progress":"[==================================================\u003e] 665B/665B","id":"c90b090c213b"}
{"status":"Verifying Checksum","progressDetail":{},"id":"c90b090c213b"}
{"status":"Download complete","progressDetail":{},"id":"c90b090c213b"}
{"status":"Downloading","progressDetail":{"current":19593654,"total":27145795},"progress":"[====================================\u003e ] 19.59MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":8296885,"total":26596573},"progress":"[===============\u003e ] 8.297MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":21273014,"total":27145795},"progress":"[=======================================\u003e ] 21.27MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":22673846,"total":27145795},"progress":"[=========================================\u003e ] 22.67MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":11332021,"total":26596573},"progress":"[=====================\u003e ] 11.33MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":23509430,"total":27145795},"progress":"[===========================================\u003e ] 23.51MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":12155317,"total":26596573},"progress":"[======================\u003e ] 12.16MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":24066486,"total":27145795},"progress":"[============================================\u003e ] 24.07MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":12433845,"total":26596573},"progress":"[=======================\u003e ] 12.43MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":25737654,"total":27145795},"progress":"[===============================================\u003e ] 25.74MB/27.15MB","id":"33847f680f63"}
{"status":"Verifying Checksum","progressDetail":{},"id":"33847f680f63"}
{"status":"Download complete","progressDetail":{},"id":"33847f680f63"}
{"status":"Extracting","progressDetail":{"current":294912,"total":27145795},"progress":"[\u003e ] 294.9kB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":15464885,"total":26596573},"progress":"[=============================\u003e ] 15.46MB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":4718592,"total":27145795},"progress":"[========\u003e ] 4.719MB/27.15MB","id":"33847f680f63"}
{"status":"Extracting","progressDetail":{"current":9437184,"total":27145795},"progress":"[=================\u003e ] 9.437MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":16021941,"total":26596573},"progress":"[==============================\u003e ] 16.02MB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":13860864,"total":27145795},"progress":"[=========================\u003e ] 13.86MB/27.15MB","id":"33847f680f63"}
{"status":"Extracting","progressDetail":{"current":18284544,"total":27145795},"progress":"[=================================\u003e ] 18.28MB/27.15MB","id":"33847f680f63"}
{"status":"Extracting","progressDetail":{"current":23003136,"total":27145795},"progress":"[==========================================\u003e ] 23MB/27.15MB","id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":657,"total":1394},"progress":"[=======================\u003e ] 657B/1.394kB","id":"1f41b2f2bf94"}
{"status":"Downloading","progressDetail":{"current":1394,"total":1394},"progress":"[==================================================\u003e] 1.394kB/1.394kB","id":"1f41b2f2bf94"}
{"status":"Verifying Checksum","progressDetail":{},"id":"1f41b2f2bf94"}
{"status":"Downloading","progressDetail":{"current":16578997,"total":26596573},"progress":"[===============================\u003e ] 16.58MB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":25952256,"total":27145795},"progress":"[===============================================\u003e ] 25.95MB/27.15MB","id":"33847f680f63"}
{"status":"Extracting","progressDetail":{"current":27145795,"total":27145795},"progress":"[==================================================\u003e] 27.15MB/27.15MB","id":"33847f680f63"}
{"status":"Pull complete","progressDetail":{},"id":"33847f680f63"}
{"status":"Downloading","progressDetail":{"current":17402293,"total":26596573},"progress":"[================================\u003e ] 17.4MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":18782645,"total":26596573},"progress":"[===================================\u003e ] 18.78MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":19618229,"total":26596573},"progress":"[====================================\u003e ] 19.62MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":19896757,"total":26596573},"progress":"[=====================================\u003e ] 19.9MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":21277109,"total":26596573},"progress":"[=======================================\u003e ] 21.28MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":21834165,"total":26596573},"progress":"[=========================================\u003e ] 21.83MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":23218613,"total":26596573},"progress":"[===========================================\u003e ] 23.22MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":24328629,"total":26596573},"progress":"[=============================================\u003e ] 24.33MB/26.6MB","id":"dbb907d5159d"}
{"status":"Downloading","progressDetail":{"current":25708981,"total":26596573},"progress":"[================================================\u003e ] 25.71MB/26.6MB","id":"dbb907d5159d"}
{"status":"Download complete","progressDetail":{},"id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":294912,"total":26596573},"progress":"[\u003e ] 294.9kB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":5013504,"total":26596573},"progress":"[=========\u003e ] 5.014MB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":10321920,"total":26596573},"progress":"[===================\u003e ] 10.32MB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":15630336,"total":26596573},"progress":"[=============================\u003e ] 15.63MB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":20348928,"total":26596573},"progress":"[======================================\u003e ] 20.35MB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":24477696,"total":26596573},"progress":"[==============================================\u003e ] 24.48MB/26.6MB","id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":26596573,"total":26596573},"progress":"[==================================================\u003e] 26.6MB/26.6MB","id":"dbb907d5159d"}
{"status":"Pull complete","progressDetail":{},"id":"dbb907d5159d"}
{"status":"Extracting","progressDetail":{"current":601,"total":601},"progress":"[==================================================\u003e] 601B/601B","id":"8a268f30c42a"}
{"status":"Extracting","progressDetail":{"current":601,"total":601},"progress":"[==================================================\u003e] 601B/601B","id":"8a268f30c42a"}
{"status":"Pull complete","progressDetail":{},"id":"8a268f30c42a"}
{"status":"Extracting","progressDetail":{"current":893,"total":893},"progress":"[==================================================\u003e] 893B/893B","id":"b10cf527a02d"}
{"status":"Extracting","progressDetail":{"current":893,"total":893},"progress":"[==================================================\u003e] 893B/893B","id":"b10cf527a02d"}
{"status":"Pull complete","progressDetail":{},"id":"b10cf527a02d"}
{"status":"Extracting","progressDetail":{"current":665,"total":665},"progress":"[==================================================\u003e] 665B/665B","id":"c90b090c213b"}
{"status":"Extracting","progressDetail":{"current":665,"total":665},"progress":"[==================================================\u003e] 665B/665B","id":"c90b090c213b"}
{"status":"Pull complete","progressDetail":{},"id":"c90b090c213b"}
{"status":"Extracting","progressDetail":{"current":1394,"total":1394},"progress":"[==================================================\u003e] 1.394kB/1.394kB","id":"1f41b2f2bf94"}
{"status":"Extracting","progressDetail":{"current":1394,"total":1394},"progress":"[==================================================\u003e] 1.394kB/1.394kB","id":"1f41b2f2bf94"}
{"status":"Pull complete","progressDetail":{},"id":"1f41b2f2bf94"}
{"status":"Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90"}
{"status":"Status: Downloaded newer image for nginx:latest"}
{"stream":" ---\u003e 08b152afcfae\n"}
{"stream":"Step 2/2 : LABEL test=project"}
{"stream":"\n"}
{"stream":" ---\u003e Running in 3932cb0bcd73\n"}
{"stream":" ---\u003e 51f1213145c6\n"}
{"aux":{"ID":"sha256:51f1213145c652b8b3c31c11a405cfd69e36577287f6aa781c225229c8b82d0d"}}
{"stream":"Successfully built 51f1213145c6\n"}
{"stream":"Successfully tagged nginx:latest\n"}
調用docker sdk的ImageBuild方法構建docker鏡像
最新推荐文章于 2024-07-22 17:35:02 发布