image.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. package local
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "github.com/Sirupsen/logrus"
  12. "github.com/docker/distribution/digest"
  13. "github.com/docker/docker/api/server/httputils"
  14. "github.com/docker/docker/api/types"
  15. "github.com/docker/docker/builder"
  16. "github.com/docker/docker/builder/dockerfile"
  17. "github.com/docker/docker/daemon/daemonbuilder"
  18. derr "github.com/docker/docker/errors"
  19. "github.com/docker/docker/pkg/archive"
  20. "github.com/docker/docker/pkg/chrootarchive"
  21. "github.com/docker/docker/pkg/ioutils"
  22. "github.com/docker/docker/pkg/progress"
  23. "github.com/docker/docker/pkg/streamformatter"
  24. "github.com/docker/docker/pkg/ulimit"
  25. "github.com/docker/docker/reference"
  26. "github.com/docker/docker/runconfig"
  27. "github.com/docker/docker/utils"
  28. "golang.org/x/net/context"
  29. )
  30. func (s *router) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  31. if err := httputils.ParseForm(r); err != nil {
  32. return err
  33. }
  34. if err := httputils.CheckForJSON(r); err != nil {
  35. return err
  36. }
  37. cname := r.Form.Get("container")
  38. pause := httputils.BoolValue(r, "pause")
  39. version := httputils.VersionFromContext(ctx)
  40. if r.FormValue("pause") == "" && version.GreaterThanOrEqualTo("1.13") {
  41. pause = true
  42. }
  43. c, _, err := runconfig.DecodeContainerConfig(r.Body)
  44. if err != nil && err != io.EOF { //Do not fail if body is empty.
  45. return err
  46. }
  47. if c == nil {
  48. c = &runconfig.Config{}
  49. }
  50. if !s.daemon.Exists(cname) {
  51. return derr.ErrorCodeNoSuchContainer.WithArgs(cname)
  52. }
  53. newConfig, err := dockerfile.BuildFromConfig(c, r.Form["changes"])
  54. if err != nil {
  55. return err
  56. }
  57. commitCfg := &types.ContainerCommitConfig{
  58. Pause: pause,
  59. Repo: r.Form.Get("repo"),
  60. Tag: r.Form.Get("tag"),
  61. Author: r.Form.Get("author"),
  62. Comment: r.Form.Get("comment"),
  63. Config: newConfig,
  64. MergeConfigs: true,
  65. }
  66. imgID, err := s.daemon.Commit(cname, commitCfg)
  67. if err != nil {
  68. return err
  69. }
  70. return httputils.WriteJSON(w, http.StatusCreated, &types.ContainerCommitResponse{
  71. ID: string(imgID),
  72. })
  73. }
  74. // Creates an image from Pull or from Import
  75. func (s *router) postImagesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  76. if err := httputils.ParseForm(r); err != nil {
  77. return err
  78. }
  79. var (
  80. image = r.Form.Get("fromImage")
  81. repo = r.Form.Get("repo")
  82. tag = r.Form.Get("tag")
  83. message = r.Form.Get("message")
  84. )
  85. authEncoded := r.Header.Get("X-Registry-Auth")
  86. authConfig := &types.AuthConfig{}
  87. if authEncoded != "" {
  88. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  89. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  90. // for a pull it is not an error if no auth was given
  91. // to increase compatibility with the existing api it is defaulting to be empty
  92. authConfig = &types.AuthConfig{}
  93. }
  94. }
  95. var (
  96. err error
  97. output = ioutils.NewWriteFlusher(w)
  98. )
  99. defer output.Close()
  100. w.Header().Set("Content-Type", "application/json")
  101. if image != "" { //pull
  102. // Special case: "pull -a" may send an image name with a
  103. // trailing :. This is ugly, but let's not break API
  104. // compatibility.
  105. image = strings.TrimSuffix(image, ":")
  106. var ref reference.Named
  107. ref, err = reference.ParseNamed(image)
  108. if err == nil {
  109. if tag != "" {
  110. // The "tag" could actually be a digest.
  111. var dgst digest.Digest
  112. dgst, err = digest.ParseDigest(tag)
  113. if err == nil {
  114. ref, err = reference.WithDigest(ref, dgst)
  115. } else {
  116. ref, err = reference.WithTag(ref, tag)
  117. }
  118. }
  119. if err == nil {
  120. metaHeaders := map[string][]string{}
  121. for k, v := range r.Header {
  122. if strings.HasPrefix(k, "X-Meta-") {
  123. metaHeaders[k] = v
  124. }
  125. }
  126. err = s.daemon.PullImage(ref, metaHeaders, authConfig, output)
  127. }
  128. }
  129. } else { //import
  130. var newRef reference.Named
  131. if repo != "" {
  132. var err error
  133. newRef, err = reference.ParseNamed(repo)
  134. if err != nil {
  135. return err
  136. }
  137. if _, isCanonical := newRef.(reference.Canonical); isCanonical {
  138. return errors.New("cannot import digest reference")
  139. }
  140. if tag != "" {
  141. newRef, err = reference.WithTag(newRef, tag)
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. }
  147. src := r.Form.Get("fromSrc")
  148. // 'err' MUST NOT be defined within this block, we need any error
  149. // generated from the download to be available to the output
  150. // stream processing below
  151. var newConfig *runconfig.Config
  152. newConfig, err = dockerfile.BuildFromConfig(&runconfig.Config{}, r.Form["changes"])
  153. if err != nil {
  154. return err
  155. }
  156. err = s.daemon.ImportImage(src, newRef, message, r.Body, output, newConfig)
  157. }
  158. if err != nil {
  159. if !output.Flushed() {
  160. return err
  161. }
  162. sf := streamformatter.NewJSONStreamFormatter()
  163. output.Write(sf.FormatError(err))
  164. }
  165. return nil
  166. }
  167. func (s *router) postImagesPush(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  168. metaHeaders := map[string][]string{}
  169. for k, v := range r.Header {
  170. if strings.HasPrefix(k, "X-Meta-") {
  171. metaHeaders[k] = v
  172. }
  173. }
  174. if err := httputils.ParseForm(r); err != nil {
  175. return err
  176. }
  177. authConfig := &types.AuthConfig{}
  178. authEncoded := r.Header.Get("X-Registry-Auth")
  179. if authEncoded != "" {
  180. // the new format is to handle the authConfig as a header
  181. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  182. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  183. // to increase compatibility to existing api it is defaulting to be empty
  184. authConfig = &types.AuthConfig{}
  185. }
  186. } else {
  187. // the old format is supported for compatibility if there was no authConfig header
  188. if err := json.NewDecoder(r.Body).Decode(authConfig); err != nil {
  189. return fmt.Errorf("Bad parameters and missing X-Registry-Auth: %v", err)
  190. }
  191. }
  192. ref, err := reference.ParseNamed(vars["name"])
  193. if err != nil {
  194. return err
  195. }
  196. tag := r.Form.Get("tag")
  197. if tag != "" {
  198. // Push by digest is not supported, so only tags are supported.
  199. ref, err = reference.WithTag(ref, tag)
  200. if err != nil {
  201. return err
  202. }
  203. }
  204. output := ioutils.NewWriteFlusher(w)
  205. defer output.Close()
  206. w.Header().Set("Content-Type", "application/json")
  207. if err := s.daemon.PushImage(ref, metaHeaders, authConfig, output); err != nil {
  208. if !output.Flushed() {
  209. return err
  210. }
  211. sf := streamformatter.NewJSONStreamFormatter()
  212. output.Write(sf.FormatError(err))
  213. }
  214. return nil
  215. }
  216. func (s *router) getImagesGet(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  217. if err := httputils.ParseForm(r); err != nil {
  218. return err
  219. }
  220. w.Header().Set("Content-Type", "application/x-tar")
  221. output := ioutils.NewWriteFlusher(w)
  222. defer output.Close()
  223. var names []string
  224. if name, ok := vars["name"]; ok {
  225. names = []string{name}
  226. } else {
  227. names = r.Form["names"]
  228. }
  229. if err := s.daemon.ExportImage(names, output); err != nil {
  230. if !output.Flushed() {
  231. return err
  232. }
  233. sf := streamformatter.NewJSONStreamFormatter()
  234. output.Write(sf.FormatError(err))
  235. }
  236. return nil
  237. }
  238. func (s *router) postImagesLoad(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  239. return s.daemon.LoadImage(r.Body, w)
  240. }
  241. func (s *router) deleteImages(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  242. if err := httputils.ParseForm(r); err != nil {
  243. return err
  244. }
  245. name := vars["name"]
  246. if strings.TrimSpace(name) == "" {
  247. return fmt.Errorf("image name cannot be blank")
  248. }
  249. force := httputils.BoolValue(r, "force")
  250. prune := !httputils.BoolValue(r, "noprune")
  251. list, err := s.daemon.ImageDelete(name, force, prune)
  252. if err != nil {
  253. return err
  254. }
  255. return httputils.WriteJSON(w, http.StatusOK, list)
  256. }
  257. func (s *router) getImagesByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  258. imageInspect, err := s.daemon.LookupImage(vars["name"])
  259. if err != nil {
  260. return err
  261. }
  262. return httputils.WriteJSON(w, http.StatusOK, imageInspect)
  263. }
  264. func (s *router) postBuild(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  265. var (
  266. authConfigs = map[string]types.AuthConfig{}
  267. authConfigsEncoded = r.Header.Get("X-Registry-Config")
  268. buildConfig = &dockerfile.Config{}
  269. )
  270. if authConfigsEncoded != "" {
  271. authConfigsJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authConfigsEncoded))
  272. if err := json.NewDecoder(authConfigsJSON).Decode(&authConfigs); err != nil {
  273. // for a pull it is not an error if no auth was given
  274. // to increase compatibility with the existing api it is defaulting
  275. // to be empty.
  276. }
  277. }
  278. w.Header().Set("Content-Type", "application/json")
  279. version := httputils.VersionFromContext(ctx)
  280. output := ioutils.NewWriteFlusher(w)
  281. defer output.Close()
  282. sf := streamformatter.NewJSONStreamFormatter()
  283. errf := func(err error) error {
  284. // Do not write the error in the http output if it's still empty.
  285. // This prevents from writing a 200(OK) when there is an internal error.
  286. if !output.Flushed() {
  287. return err
  288. }
  289. _, err = w.Write(sf.FormatError(errors.New(utils.GetErrorMessage(err))))
  290. if err != nil {
  291. logrus.Warnf("could not write error response: %v", err)
  292. }
  293. return nil
  294. }
  295. if httputils.BoolValue(r, "forcerm") && version.GreaterThanOrEqualTo("1.12") {
  296. buildConfig.Remove = true
  297. } else if r.FormValue("rm") == "" && version.GreaterThanOrEqualTo("1.12") {
  298. buildConfig.Remove = true
  299. } else {
  300. buildConfig.Remove = httputils.BoolValue(r, "rm")
  301. }
  302. if httputils.BoolValue(r, "pull") && version.GreaterThanOrEqualTo("1.16") {
  303. buildConfig.Pull = true
  304. }
  305. repoAndTags, err := sanitizeRepoAndTags(r.Form["t"])
  306. if err != nil {
  307. return errf(err)
  308. }
  309. buildConfig.DockerfileName = r.FormValue("dockerfile")
  310. buildConfig.Verbose = !httputils.BoolValue(r, "q")
  311. buildConfig.UseCache = !httputils.BoolValue(r, "nocache")
  312. buildConfig.ForceRemove = httputils.BoolValue(r, "forcerm")
  313. buildConfig.MemorySwap = httputils.Int64ValueOrZero(r, "memswap")
  314. buildConfig.Memory = httputils.Int64ValueOrZero(r, "memory")
  315. buildConfig.CPUShares = httputils.Int64ValueOrZero(r, "cpushares")
  316. buildConfig.CPUPeriod = httputils.Int64ValueOrZero(r, "cpuperiod")
  317. buildConfig.CPUQuota = httputils.Int64ValueOrZero(r, "cpuquota")
  318. buildConfig.CPUSetCpus = r.FormValue("cpusetcpus")
  319. buildConfig.CPUSetMems = r.FormValue("cpusetmems")
  320. buildConfig.CgroupParent = r.FormValue("cgroupparent")
  321. if r.Form.Get("shmsize") != "" {
  322. shmSize, err := strconv.ParseInt(r.Form.Get("shmsize"), 10, 64)
  323. if err != nil {
  324. return errf(err)
  325. }
  326. buildConfig.ShmSize = &shmSize
  327. }
  328. if i := runconfig.IsolationLevel(r.FormValue("isolation")); i != "" {
  329. if !runconfig.IsolationLevel.IsValid(i) {
  330. return errf(fmt.Errorf("Unsupported isolation: %q", i))
  331. }
  332. buildConfig.Isolation = i
  333. }
  334. var buildUlimits = []*ulimit.Ulimit{}
  335. ulimitsJSON := r.FormValue("ulimits")
  336. if ulimitsJSON != "" {
  337. if err := json.NewDecoder(strings.NewReader(ulimitsJSON)).Decode(&buildUlimits); err != nil {
  338. return errf(err)
  339. }
  340. buildConfig.Ulimits = buildUlimits
  341. }
  342. var buildArgs = map[string]string{}
  343. buildArgsJSON := r.FormValue("buildargs")
  344. if buildArgsJSON != "" {
  345. if err := json.NewDecoder(strings.NewReader(buildArgsJSON)).Decode(&buildArgs); err != nil {
  346. return errf(err)
  347. }
  348. buildConfig.BuildArgs = buildArgs
  349. }
  350. remoteURL := r.FormValue("remote")
  351. // Currently, only used if context is from a remote url.
  352. // Look at code in DetectContextFromRemoteURL for more information.
  353. createProgressReader := func(in io.ReadCloser) io.ReadCloser {
  354. progressOutput := sf.NewProgressOutput(output, true)
  355. return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", remoteURL)
  356. }
  357. var (
  358. context builder.ModifiableContext
  359. dockerfileName string
  360. )
  361. context, dockerfileName, err = daemonbuilder.DetectContextFromRemoteURL(r.Body, remoteURL, createProgressReader)
  362. if err != nil {
  363. return errf(err)
  364. }
  365. defer func() {
  366. if err := context.Close(); err != nil {
  367. logrus.Debugf("[BUILDER] failed to remove temporary context: %v", err)
  368. }
  369. }()
  370. uidMaps, gidMaps := s.daemon.GetUIDGIDMaps()
  371. defaultArchiver := &archive.Archiver{
  372. Untar: chrootarchive.Untar,
  373. UIDMaps: uidMaps,
  374. GIDMaps: gidMaps,
  375. }
  376. docker := &daemonbuilder.Docker{
  377. Daemon: s.daemon,
  378. OutOld: output,
  379. AuthConfigs: authConfigs,
  380. Archiver: defaultArchiver,
  381. }
  382. b, err := dockerfile.NewBuilder(buildConfig, docker, builder.DockerIgnoreContext{ModifiableContext: context}, nil)
  383. if err != nil {
  384. return errf(err)
  385. }
  386. b.Stdout = &streamformatter.StdoutFormatter{Writer: output, StreamFormatter: sf}
  387. b.Stderr = &streamformatter.StderrFormatter{Writer: output, StreamFormatter: sf}
  388. if closeNotifier, ok := w.(http.CloseNotifier); ok {
  389. finished := make(chan struct{})
  390. defer close(finished)
  391. go func() {
  392. select {
  393. case <-finished:
  394. case <-closeNotifier.CloseNotify():
  395. logrus.Infof("Client disconnected, cancelling job: build")
  396. b.Cancel()
  397. }
  398. }()
  399. }
  400. if len(dockerfileName) > 0 {
  401. b.DockerfileName = dockerfileName
  402. }
  403. imgID, err := b.Build()
  404. if err != nil {
  405. return errf(err)
  406. }
  407. for _, rt := range repoAndTags {
  408. if err := s.daemon.TagImage(rt, imgID); err != nil {
  409. return errf(err)
  410. }
  411. }
  412. return nil
  413. }
  414. // sanitizeRepoAndTags parses the raw "t" parameter received from the client
  415. // to a slice of repoAndTag.
  416. // It also validates each repoName and tag.
  417. func sanitizeRepoAndTags(names []string) ([]reference.Named, error) {
  418. var (
  419. repoAndTags []reference.Named
  420. // This map is used for deduplicating the "-t" parameter.
  421. uniqNames = make(map[string]struct{})
  422. )
  423. for _, repo := range names {
  424. if repo == "" {
  425. continue
  426. }
  427. ref, err := reference.ParseNamed(repo)
  428. if err != nil {
  429. return nil, err
  430. }
  431. ref = reference.WithDefaultTag(ref)
  432. if _, isCanonical := ref.(reference.Canonical); isCanonical {
  433. return nil, errors.New("build tag cannot contain a digest")
  434. }
  435. nameWithTag := ref.String()
  436. if _, exists := uniqNames[nameWithTag]; !exists {
  437. uniqNames[nameWithTag] = struct{}{}
  438. repoAndTags = append(repoAndTags, ref)
  439. }
  440. }
  441. return repoAndTags, nil
  442. }
  443. func (s *router) getImagesJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  444. if err := httputils.ParseForm(r); err != nil {
  445. return err
  446. }
  447. // FIXME: The filter parameter could just be a match filter
  448. images, err := s.daemon.Images(r.Form.Get("filters"), r.Form.Get("filter"), httputils.BoolValue(r, "all"))
  449. if err != nil {
  450. return err
  451. }
  452. return httputils.WriteJSON(w, http.StatusOK, images)
  453. }
  454. func (s *router) getImagesHistory(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  455. name := vars["name"]
  456. history, err := s.daemon.ImageHistory(name)
  457. if err != nil {
  458. return err
  459. }
  460. return httputils.WriteJSON(w, http.StatusOK, history)
  461. }
  462. func (s *router) postImagesTag(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  463. if err := httputils.ParseForm(r); err != nil {
  464. return err
  465. }
  466. repo := r.Form.Get("repo")
  467. tag := r.Form.Get("tag")
  468. newTag, err := reference.WithName(repo)
  469. if err != nil {
  470. return err
  471. }
  472. if tag != "" {
  473. if newTag, err = reference.WithTag(newTag, tag); err != nil {
  474. return err
  475. }
  476. }
  477. if err := s.daemon.TagImage(newTag, vars["name"]); err != nil {
  478. return err
  479. }
  480. w.WriteHeader(http.StatusCreated)
  481. return nil
  482. }
  483. func (s *router) getImagesSearch(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  484. if err := httputils.ParseForm(r); err != nil {
  485. return err
  486. }
  487. var (
  488. config *types.AuthConfig
  489. authEncoded = r.Header.Get("X-Registry-Auth")
  490. headers = map[string][]string{}
  491. )
  492. if authEncoded != "" {
  493. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  494. if err := json.NewDecoder(authJSON).Decode(&config); err != nil {
  495. // for a search it is not an error if no auth was given
  496. // to increase compatibility with the existing api it is defaulting to be empty
  497. config = &types.AuthConfig{}
  498. }
  499. }
  500. for k, v := range r.Header {
  501. if strings.HasPrefix(k, "X-Meta-") {
  502. headers[k] = v
  503. }
  504. }
  505. query, err := s.daemon.SearchRegistryForImages(r.Form.Get("term"), config, headers)
  506. if err != nil {
  507. return err
  508. }
  509. return httputils.WriteJSON(w, http.StatusOK, query.Results)
  510. }