image_routes.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package image // import "github.com/docker/docker/api/server/router/image"
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/containerd/containerd/platforms"
  12. "github.com/distribution/reference"
  13. "github.com/docker/docker/api"
  14. "github.com/docker/docker/api/server/httputils"
  15. "github.com/docker/docker/api/types"
  16. "github.com/docker/docker/api/types/backend"
  17. "github.com/docker/docker/api/types/filters"
  18. imagetypes "github.com/docker/docker/api/types/image"
  19. "github.com/docker/docker/api/types/registry"
  20. "github.com/docker/docker/api/types/versions"
  21. "github.com/docker/docker/builder/remotecontext"
  22. "github.com/docker/docker/dockerversion"
  23. "github.com/docker/docker/errdefs"
  24. "github.com/docker/docker/image"
  25. "github.com/docker/docker/pkg/ioutils"
  26. "github.com/docker/docker/pkg/progress"
  27. "github.com/docker/docker/pkg/streamformatter"
  28. "github.com/opencontainers/go-digest"
  29. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  30. "github.com/pkg/errors"
  31. )
  32. // Creates an image from Pull or from Import
  33. func (ir *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  34. if err := httputils.ParseForm(r); err != nil {
  35. return err
  36. }
  37. var (
  38. img = r.Form.Get("fromImage")
  39. repo = r.Form.Get("repo")
  40. tag = r.Form.Get("tag")
  41. comment = r.Form.Get("message")
  42. progressErr error
  43. output = ioutils.NewWriteFlusher(w)
  44. platform *ocispec.Platform
  45. )
  46. defer output.Close()
  47. w.Header().Set("Content-Type", "application/json")
  48. version := httputils.VersionFromContext(ctx)
  49. if versions.GreaterThanOrEqualTo(version, "1.32") {
  50. if p := r.FormValue("platform"); p != "" {
  51. sp, err := platforms.Parse(p)
  52. if err != nil {
  53. return err
  54. }
  55. platform = &sp
  56. }
  57. }
  58. if img != "" { // pull
  59. metaHeaders := map[string][]string{}
  60. for k, v := range r.Header {
  61. if strings.HasPrefix(k, "X-Meta-") {
  62. metaHeaders[k] = v
  63. }
  64. }
  65. // Special case: "pull -a" may send an image name with a
  66. // trailing :. This is ugly, but let's not break API
  67. // compatibility.
  68. imgName := strings.TrimSuffix(img, ":")
  69. ref, err := reference.ParseNormalizedNamed(imgName)
  70. if err != nil {
  71. return errdefs.InvalidParameter(err)
  72. }
  73. // TODO(thaJeztah) this could use a WithTagOrDigest() utility
  74. if tag != "" {
  75. // The "tag" could actually be a digest.
  76. var dgst digest.Digest
  77. dgst, err = digest.Parse(tag)
  78. if err == nil {
  79. ref, err = reference.WithDigest(reference.TrimNamed(ref), dgst)
  80. } else {
  81. ref, err = reference.WithTag(ref, tag)
  82. }
  83. if err != nil {
  84. return errdefs.InvalidParameter(err)
  85. }
  86. }
  87. if err := validateRepoName(ref); err != nil {
  88. return errdefs.Forbidden(err)
  89. }
  90. // For a pull it is not an error if no auth was given. Ignore invalid
  91. // AuthConfig to increase compatibility with the existing API.
  92. authConfig, _ := registry.DecodeAuthConfig(r.Header.Get(registry.AuthHeader))
  93. progressErr = ir.backend.PullImage(ctx, ref, platform, metaHeaders, authConfig, output)
  94. } else { // import
  95. src := r.Form.Get("fromSrc")
  96. tagRef, err := httputils.RepoTagReference(repo, tag)
  97. if err != nil {
  98. return errdefs.InvalidParameter(err)
  99. }
  100. if len(comment) == 0 {
  101. comment = "Imported from " + src
  102. }
  103. var layerReader io.ReadCloser
  104. defer r.Body.Close()
  105. if src == "-" {
  106. layerReader = r.Body
  107. } else {
  108. if len(strings.Split(src, "://")) == 1 {
  109. src = "http://" + src
  110. }
  111. u, err := url.Parse(src)
  112. if err != nil {
  113. return errdefs.InvalidParameter(err)
  114. }
  115. resp, err := remotecontext.GetWithStatusError(u.String())
  116. if err != nil {
  117. return err
  118. }
  119. output.Write(streamformatter.FormatStatus("", "Downloading from %s", u))
  120. progressOutput := streamformatter.NewJSONProgressOutput(output, true)
  121. layerReader = progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Importing")
  122. defer layerReader.Close()
  123. }
  124. var id image.ID
  125. id, progressErr = ir.backend.ImportImage(ctx, tagRef, platform, comment, layerReader, r.Form["changes"])
  126. if progressErr == nil {
  127. output.Write(streamformatter.FormatStatus("", id.String()))
  128. }
  129. }
  130. if progressErr != nil {
  131. if !output.Flushed() {
  132. return progressErr
  133. }
  134. _, _ = output.Write(streamformatter.FormatError(progressErr))
  135. }
  136. return nil
  137. }
  138. func (ir *imageRouter) postImagesPush(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  139. metaHeaders := map[string][]string{}
  140. for k, v := range r.Header {
  141. if strings.HasPrefix(k, "X-Meta-") {
  142. metaHeaders[k] = v
  143. }
  144. }
  145. if err := httputils.ParseForm(r); err != nil {
  146. return err
  147. }
  148. var authConfig *registry.AuthConfig
  149. if authEncoded := r.Header.Get(registry.AuthHeader); authEncoded != "" {
  150. // the new format is to handle the authConfig as a header. Ignore invalid
  151. // AuthConfig to increase compatibility with the existing API.
  152. authConfig, _ = registry.DecodeAuthConfig(authEncoded)
  153. } else {
  154. // the old format is supported for compatibility if there was no authConfig header
  155. var err error
  156. authConfig, err = registry.DecodeAuthConfigBody(r.Body)
  157. if err != nil {
  158. return errors.Wrap(err, "bad parameters and missing X-Registry-Auth")
  159. }
  160. }
  161. output := ioutils.NewWriteFlusher(w)
  162. defer output.Close()
  163. w.Header().Set("Content-Type", "application/json")
  164. img := vars["name"]
  165. tag := r.Form.Get("tag")
  166. var ref reference.Named
  167. // Tag is empty only in case PushOptions.All is true.
  168. if tag != "" {
  169. r, err := httputils.RepoTagReference(img, tag)
  170. if err != nil {
  171. return errdefs.InvalidParameter(err)
  172. }
  173. ref = r
  174. } else {
  175. r, err := reference.ParseNormalizedNamed(img)
  176. if err != nil {
  177. return errdefs.InvalidParameter(err)
  178. }
  179. ref = r
  180. }
  181. if err := ir.backend.PushImage(ctx, ref, metaHeaders, authConfig, output); err != nil {
  182. if !output.Flushed() {
  183. return err
  184. }
  185. _, _ = output.Write(streamformatter.FormatError(err))
  186. }
  187. return nil
  188. }
  189. func (ir *imageRouter) getImagesGet(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  190. if err := httputils.ParseForm(r); err != nil {
  191. return err
  192. }
  193. w.Header().Set("Content-Type", "application/x-tar")
  194. output := ioutils.NewWriteFlusher(w)
  195. defer output.Close()
  196. var names []string
  197. if name, ok := vars["name"]; ok {
  198. names = []string{name}
  199. } else {
  200. names = r.Form["names"]
  201. }
  202. if err := ir.backend.ExportImage(ctx, names, output); err != nil {
  203. if !output.Flushed() {
  204. return err
  205. }
  206. _, _ = output.Write(streamformatter.FormatError(err))
  207. }
  208. return nil
  209. }
  210. func (ir *imageRouter) postImagesLoad(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  211. if err := httputils.ParseForm(r); err != nil {
  212. return err
  213. }
  214. quiet := httputils.BoolValueOrDefault(r, "quiet", true)
  215. w.Header().Set("Content-Type", "application/json")
  216. output := ioutils.NewWriteFlusher(w)
  217. defer output.Close()
  218. if err := ir.backend.LoadImage(ctx, r.Body, output, quiet); err != nil {
  219. _, _ = output.Write(streamformatter.FormatError(err))
  220. }
  221. return nil
  222. }
  223. type missingImageError struct{}
  224. func (missingImageError) Error() string {
  225. return "image name cannot be blank"
  226. }
  227. func (missingImageError) InvalidParameter() {}
  228. func (ir *imageRouter) deleteImages(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  229. if err := httputils.ParseForm(r); err != nil {
  230. return err
  231. }
  232. name := vars["name"]
  233. if strings.TrimSpace(name) == "" {
  234. return missingImageError{}
  235. }
  236. force := httputils.BoolValue(r, "force")
  237. prune := !httputils.BoolValue(r, "noprune")
  238. list, err := ir.backend.ImageDelete(ctx, name, force, prune)
  239. if err != nil {
  240. return err
  241. }
  242. return httputils.WriteJSON(w, http.StatusOK, list)
  243. }
  244. func (ir *imageRouter) getImagesByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  245. img, err := ir.backend.GetImage(ctx, vars["name"], backend.GetImageOpts{Details: true})
  246. if err != nil {
  247. return err
  248. }
  249. imageInspect, err := ir.toImageInspect(img)
  250. if err != nil {
  251. return err
  252. }
  253. version := httputils.VersionFromContext(ctx)
  254. if versions.LessThan(version, "1.44") {
  255. imageInspect.VirtualSize = imageInspect.Size //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.
  256. if imageInspect.Created == "" {
  257. // backwards compatibility for Created not existing returning "0001-01-01T00:00:00Z"
  258. // https://github.com/moby/moby/issues/47368
  259. imageInspect.Created = time.Time{}.Format(time.RFC3339Nano)
  260. }
  261. }
  262. return httputils.WriteJSON(w, http.StatusOK, imageInspect)
  263. }
  264. func (ir *imageRouter) toImageInspect(img *image.Image) (*types.ImageInspect, error) {
  265. var repoTags, repoDigests []string
  266. for _, ref := range img.Details.References {
  267. switch ref.(type) {
  268. case reference.NamedTagged:
  269. repoTags = append(repoTags, reference.FamiliarString(ref))
  270. case reference.Canonical:
  271. repoDigests = append(repoDigests, reference.FamiliarString(ref))
  272. }
  273. }
  274. comment := img.Comment
  275. if len(comment) == 0 && len(img.History) > 0 {
  276. comment = img.History[len(img.History)-1].Comment
  277. }
  278. // Make sure we output empty arrays instead of nil.
  279. if repoTags == nil {
  280. repoTags = []string{}
  281. }
  282. if repoDigests == nil {
  283. repoDigests = []string{}
  284. }
  285. var created string
  286. if img.Created != nil {
  287. created = img.Created.Format(time.RFC3339Nano)
  288. }
  289. return &types.ImageInspect{
  290. ID: img.ID().String(),
  291. RepoTags: repoTags,
  292. RepoDigests: repoDigests,
  293. Parent: img.Parent.String(),
  294. Comment: comment,
  295. Created: created,
  296. Container: img.Container, //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.45.
  297. ContainerConfig: &img.ContainerConfig, //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.45.
  298. DockerVersion: img.DockerVersion,
  299. Author: img.Author,
  300. Config: img.Config,
  301. Architecture: img.Architecture,
  302. Variant: img.Variant,
  303. Os: img.OperatingSystem(),
  304. OsVersion: img.OSVersion,
  305. Size: img.Details.Size,
  306. GraphDriver: types.GraphDriverData{
  307. Name: img.Details.Driver,
  308. Data: img.Details.Metadata,
  309. },
  310. RootFS: rootFSToAPIType(img.RootFS),
  311. Metadata: imagetypes.Metadata{
  312. LastTagTime: img.Details.LastUpdated,
  313. },
  314. }, nil
  315. }
  316. func rootFSToAPIType(rootfs *image.RootFS) types.RootFS {
  317. var layers []string
  318. for _, l := range rootfs.DiffIDs {
  319. layers = append(layers, l.String())
  320. }
  321. return types.RootFS{
  322. Type: rootfs.Type,
  323. Layers: layers,
  324. }
  325. }
  326. func (ir *imageRouter) getImagesJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  327. if err := httputils.ParseForm(r); err != nil {
  328. return err
  329. }
  330. imageFilters, err := filters.FromJSON(r.Form.Get("filters"))
  331. if err != nil {
  332. return err
  333. }
  334. version := httputils.VersionFromContext(ctx)
  335. if versions.LessThan(version, "1.41") {
  336. // NOTE: filter is a shell glob string applied to repository names.
  337. filterParam := r.Form.Get("filter")
  338. if filterParam != "" {
  339. imageFilters.Add("reference", filterParam)
  340. }
  341. }
  342. var sharedSize bool
  343. if versions.GreaterThanOrEqualTo(version, "1.42") {
  344. // NOTE: Support for the "shared-size" parameter was added in API 1.42.
  345. sharedSize = httputils.BoolValue(r, "shared-size")
  346. }
  347. images, err := ir.backend.Images(ctx, imagetypes.ListOptions{
  348. All: httputils.BoolValue(r, "all"),
  349. Filters: imageFilters,
  350. SharedSize: sharedSize,
  351. })
  352. if err != nil {
  353. return err
  354. }
  355. useNone := versions.LessThan(version, "1.43")
  356. withVirtualSize := versions.LessThan(version, "1.44")
  357. for _, img := range images {
  358. if useNone {
  359. if len(img.RepoTags) == 0 && len(img.RepoDigests) == 0 {
  360. img.RepoTags = append(img.RepoTags, "<none>:<none>")
  361. img.RepoDigests = append(img.RepoDigests, "<none>@<none>")
  362. }
  363. } else {
  364. if img.RepoTags == nil {
  365. img.RepoTags = []string{}
  366. }
  367. if img.RepoDigests == nil {
  368. img.RepoDigests = []string{}
  369. }
  370. }
  371. if withVirtualSize {
  372. img.VirtualSize = img.Size //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.
  373. }
  374. }
  375. return httputils.WriteJSON(w, http.StatusOK, images)
  376. }
  377. func (ir *imageRouter) getImagesHistory(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  378. history, err := ir.backend.ImageHistory(ctx, vars["name"])
  379. if err != nil {
  380. return err
  381. }
  382. return httputils.WriteJSON(w, http.StatusOK, history)
  383. }
  384. func (ir *imageRouter) postImagesTag(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  385. if err := httputils.ParseForm(r); err != nil {
  386. return err
  387. }
  388. ref, err := httputils.RepoTagReference(r.Form.Get("repo"), r.Form.Get("tag"))
  389. if ref == nil || err != nil {
  390. return errdefs.InvalidParameter(err)
  391. }
  392. refName := reference.FamiliarName(ref)
  393. if refName == string(digest.Canonical) {
  394. return errdefs.InvalidParameter(errors.New("refusing to create an ambiguous tag using digest algorithm as name"))
  395. }
  396. img, err := ir.backend.GetImage(ctx, vars["name"], backend.GetImageOpts{})
  397. if err != nil {
  398. return errdefs.NotFound(err)
  399. }
  400. if err := ir.backend.TagImage(ctx, img.ID(), ref); err != nil {
  401. return err
  402. }
  403. w.WriteHeader(http.StatusCreated)
  404. return nil
  405. }
  406. func (ir *imageRouter) getImagesSearch(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  407. if err := httputils.ParseForm(r); err != nil {
  408. return err
  409. }
  410. var limit int
  411. if r.Form.Get("limit") != "" {
  412. var err error
  413. limit, err = strconv.Atoi(r.Form.Get("limit"))
  414. if err != nil || limit < 0 {
  415. return errdefs.InvalidParameter(errors.Wrap(err, "invalid limit specified"))
  416. }
  417. }
  418. searchFilters, err := filters.FromJSON(r.Form.Get("filters"))
  419. if err != nil {
  420. return err
  421. }
  422. // For a search it is not an error if no auth was given. Ignore invalid
  423. // AuthConfig to increase compatibility with the existing API.
  424. authConfig, _ := registry.DecodeAuthConfig(r.Header.Get(registry.AuthHeader))
  425. headers := http.Header{}
  426. for k, v := range r.Header {
  427. k = http.CanonicalHeaderKey(k)
  428. if strings.HasPrefix(k, "X-Meta-") {
  429. headers[k] = v
  430. }
  431. }
  432. headers.Set("User-Agent", dockerversion.DockerUserAgent(ctx))
  433. res, err := ir.searcher.Search(ctx, searchFilters, r.Form.Get("term"), limit, authConfig, headers)
  434. if err != nil {
  435. return err
  436. }
  437. return httputils.WriteJSON(w, http.StatusOK, res)
  438. }
  439. func (ir *imageRouter) postImagesPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  440. if err := httputils.ParseForm(r); err != nil {
  441. return err
  442. }
  443. pruneFilters, err := filters.FromJSON(r.Form.Get("filters"))
  444. if err != nil {
  445. return err
  446. }
  447. pruneReport, err := ir.backend.ImagesPrune(ctx, pruneFilters)
  448. if err != nil {
  449. return err
  450. }
  451. return httputils.WriteJSON(w, http.StatusOK, pruneReport)
  452. }
  453. // validateRepoName validates the name of a repository.
  454. func validateRepoName(name reference.Named) error {
  455. familiarName := reference.FamiliarName(name)
  456. if familiarName == api.NoBaseImageSpecifier {
  457. return fmt.Errorf("'%s' is a reserved name", familiarName)
  458. }
  459. return nil
  460. }