Build Docker Image From Go Code

后端 未结 6 1646
鱼传尺愫
鱼传尺愫 2020-12-03 15:29

I\'m trying to build a Docker image using the Docker API and Docker Go libraries (https://github.com/docker/engine-api/). Code example:

package main
import (         


        
6条回答
  •  失恋的感觉
    2020-12-03 15:53

    The Docker package has a function for creating a TAR from a file path. It's whats used by the CLI. It's not in the client package so it need to be installed separately:

    import (
        "github.com/mitchellh/go-homedir"
        "github.com/docker/docker/pkg/archive"
    )
    
    func GetContext(filePath string) io.Reader {
        // Use homedir.Expand to resolve paths like '~/repos/myrepo'
        filePath, _ := homedir.Expand(filePath)
        ctx, _ := archive.TarWithOptions(filePath, &archive.TarOptions{})
        return ctx
    }
    
    cli.ImageBuild(context.Background(), GetContext("~/repos/myrepo"), types.ImageBuildOptions{...})
    

提交回复
热议问题