Skip to content

Instantly share code, notes, and snippets.

@pauldotknopf
Created January 15, 2018 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauldotknopf/b6e86dbd10cd310b22c59db3c13f197f to your computer and use it in GitHub Desktop.
Save pauldotknopf/b6e86dbd10cd310b22c59db3c13f197f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"path/filepath"
"runtime"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
_ "github.com/docker/docker/daemon/graphdriver/register"
"github.com/docker/docker/distribution"
"github.com/docker/docker/distribution/xfer"
"github.com/docker/docker/image"
"github.com/docker/docker/layer"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/progress"
"github.com/docker/docker/plugin"
"github.com/docker/docker/registry"
"golang.org/x/net/context"
)
type progressWriter struct {
}
func (writer progressWriter) WriteProgress(prog progress.Progress) error {
fmt.Println(prog.Message)
return nil
}
func main() {
registryService, err := registry.NewService(registry.ServiceOptions{})
if err != nil {
return
}
mappings, err := idtools.NewIDMappings("pknopf", "users")
if err != nil {
return
}
pluginStore := plugin.NewStore()
layerStore, err := layer.NewStoreFromOptions(layer.StoreOptions{
StorePath: "/var/lib/darch",
MetadataStorePathTemplate: filepath.Join("/var/lib/darch", "image", "%s", "layerdb"),
GraphDriver: "vfs",
GraphDriverOptions: []string{},
IDMappings: mappings,
PluginGetter: pluginStore,
ExperimentalEnabled: false,
OS: runtime.GOOS,
})
if err != nil {
return
}
imageRoot := filepath.Join("/var/lib/darch", "image", "vfs")
ifs, err := image.NewFSStoreBackend(filepath.Join(imageRoot, "imagedb"))
if err != nil {
return
}
imageStore, err := image.NewImageStore(ifs, runtime.GOOS, layerStore)
if err != nil {
return
}
ref, err := reference.ParseNamed("docker.io/library/ubuntu:latest")
if err != nil {
return
}
progressWriter := progressWriter{}
imagePullConfig := distribution.ImagePullConfig{}
imagePullConfig.RegistryService = registryService
imagePullConfig.RequireSchema2 = true
imagePullConfig.AuthConfig = &types.AuthConfig{}
imagePullConfig.ProgressOutput = progressWriter
imagePullConfig.ImageStore = distribution.NewImageConfigStoreFromStore(imageStore)
lsMap := make(map[string]layer.Store)
lsMap[runtime.GOOS] = layerStore
imagePullConfig.DownloadManager = xfer.NewLayerDownloadManager(lsMap, 100)
distribution.Pull(context.Background(), ref, &imagePullConfig)
}
@pauldotknopf
Copy link
Author

This is some sample code that I wrote up that allows you to use an internal image/layer store, using dockerd internals. I will be using this for Darch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment