12 "github.com/docker/distribution/reference"
13 "github.com/docker/docker/api/types/container"
14 "github.com/docker/docker/builder/dockerfile"
15 "github.com/docker/docker/builder/remotecontext"
16 "github.com/docker/docker/dockerversion"
17 "github.com/docker/docker/image"
18 "github.com/docker/docker/layer"
19 "github.com/docker/docker/pkg/archive"
20 "github.com/docker/docker/pkg/progress"
21 "github.com/docker/docker/pkg/streamformatter"
22 "github.com/pkg/errors"
25 // ImportImage imports an image, getting the archived layer data either from
26 // inConfig (if src is "-"), or from a URI specified in src. Progress output is
27 // written to outStream. Repository and tag names can optionally be given in
28 // the repo and tag arguments, respectively.
29 func (daemon *Daemon) ImportImage(src string, repository, platform string, tag string, msg string, inConfig io.ReadCloser, outStream io.Writer, changes []string) error {
33 newRef reference.Named
36 // Default the platform if not supplied.
38 platform = runtime.GOOS
43 newRef, err = reference.ParseNormalizedNamed(repository)
47 if _, isCanonical := newRef.(reference.Canonical); isCanonical {
48 return errors.New("cannot import digest reference")
52 newRef, err = reference.WithTag(newRef, tag)
59 config, err := dockerfile.BuildFromConfig(&container.Config{}, changes)
67 if len(strings.Split(src, "://")) == 1 {
70 u, err := url.Parse(src)
75 resp, err = remotecontext.GetWithStatusError(u.String())
79 outStream.Write(streamformatter.FormatStatus("", "Downloading from %s", u))
80 progressOutput := streamformatter.NewJSONProgressOutput(outStream, true)
81 rc = progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Importing")
86 msg = "Imported from " + src
89 inflatedLayerData, err := archive.DecompressStream(rc)
93 l, err := daemon.stores[platform].layerStore.Register(inflatedLayerData, "", layer.Platform(platform))
97 defer layer.ReleaseAndLog(daemon.stores[platform].layerStore, l)
99 created := time.Now().UTC()
100 imgConfig, err := json.Marshal(&image.Image{
101 V1Image: image.V1Image{
102 DockerVersion: dockerversion.Version,
104 Architecture: runtime.GOARCH,
109 RootFS: &image.RootFS{
111 DiffIDs: []layer.DiffID{l.DiffID()},
113 History: []image.History{{
122 id, err := daemon.stores[platform].imageStore.Create(imgConfig)
127 // FIXME: connect with commit code and call refstore directly
129 if err := daemon.TagImageWithReference(id, platform, newRef); err != nil {
134 daemon.LogImageEvent(id.String(), id.String(), "import")
135 outStream.Write(streamformatter.FormatStatus("", id.String()))