container_routes.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. package container // import "github.com/docker/docker/api/server/router/container"
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strconv"
  8. "syscall"
  9. "github.com/docker/docker/api/server/httputils"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/api/types/backend"
  12. "github.com/docker/docker/api/types/container"
  13. "github.com/docker/docker/api/types/filters"
  14. "github.com/docker/docker/api/types/versions"
  15. containerpkg "github.com/docker/docker/container"
  16. "github.com/docker/docker/errdefs"
  17. "github.com/docker/docker/pkg/ioutils"
  18. "github.com/docker/docker/pkg/signal"
  19. "github.com/pkg/errors"
  20. "github.com/sirupsen/logrus"
  21. "golang.org/x/net/context"
  22. "golang.org/x/net/websocket"
  23. )
  24. func (s *containerRouter) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  25. if err := httputils.ParseForm(r); err != nil {
  26. return err
  27. }
  28. if err := httputils.CheckForJSON(r); err != nil {
  29. return err
  30. }
  31. // TODO: remove pause arg, and always pause in backend
  32. pause := httputils.BoolValue(r, "pause")
  33. version := httputils.VersionFromContext(ctx)
  34. if r.FormValue("pause") == "" && versions.GreaterThanOrEqualTo(version, "1.13") {
  35. pause = true
  36. }
  37. config, _, _, err := s.decoder.DecodeConfig(r.Body)
  38. if err != nil && err != io.EOF { //Do not fail if body is empty.
  39. return err
  40. }
  41. commitCfg := &backend.CreateImageConfig{
  42. Pause: pause,
  43. Repo: r.Form.Get("repo"),
  44. Tag: r.Form.Get("tag"),
  45. Author: r.Form.Get("author"),
  46. Comment: r.Form.Get("comment"),
  47. Config: config,
  48. Changes: r.Form["changes"],
  49. }
  50. imgID, err := s.backend.CreateImageFromContainer(r.Form.Get("container"), commitCfg)
  51. if err != nil {
  52. return err
  53. }
  54. return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{ID: imgID})
  55. }
  56. func (s *containerRouter) getContainersJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  57. if err := httputils.ParseForm(r); err != nil {
  58. return err
  59. }
  60. filter, err := filters.FromJSON(r.Form.Get("filters"))
  61. if err != nil {
  62. return err
  63. }
  64. config := &types.ContainerListOptions{
  65. All: httputils.BoolValue(r, "all"),
  66. Size: httputils.BoolValue(r, "size"),
  67. Since: r.Form.Get("since"),
  68. Before: r.Form.Get("before"),
  69. Filters: filter,
  70. }
  71. if tmpLimit := r.Form.Get("limit"); tmpLimit != "" {
  72. limit, err := strconv.Atoi(tmpLimit)
  73. if err != nil {
  74. return err
  75. }
  76. config.Limit = limit
  77. }
  78. containers, err := s.backend.Containers(config)
  79. if err != nil {
  80. return err
  81. }
  82. return httputils.WriteJSON(w, http.StatusOK, containers)
  83. }
  84. func (s *containerRouter) getContainersStats(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  85. if err := httputils.ParseForm(r); err != nil {
  86. return err
  87. }
  88. stream := httputils.BoolValueOrDefault(r, "stream", true)
  89. if !stream {
  90. w.Header().Set("Content-Type", "application/json")
  91. }
  92. config := &backend.ContainerStatsConfig{
  93. Stream: stream,
  94. OutStream: w,
  95. Version: httputils.VersionFromContext(ctx),
  96. }
  97. return s.backend.ContainerStats(ctx, vars["name"], config)
  98. }
  99. func (s *containerRouter) getContainersLogs(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  100. if err := httputils.ParseForm(r); err != nil {
  101. return err
  102. }
  103. // Args are validated before the stream starts because when it starts we're
  104. // sending HTTP 200 by writing an empty chunk of data to tell the client that
  105. // daemon is going to stream. By sending this initial HTTP 200 we can't report
  106. // any error after the stream starts (i.e. container not found, wrong parameters)
  107. // with the appropriate status code.
  108. stdout, stderr := httputils.BoolValue(r, "stdout"), httputils.BoolValue(r, "stderr")
  109. if !(stdout || stderr) {
  110. return errdefs.InvalidParameter(errors.New("Bad parameters: you must choose at least one stream"))
  111. }
  112. containerName := vars["name"]
  113. logsConfig := &types.ContainerLogsOptions{
  114. Follow: httputils.BoolValue(r, "follow"),
  115. Timestamps: httputils.BoolValue(r, "timestamps"),
  116. Since: r.Form.Get("since"),
  117. Until: r.Form.Get("until"),
  118. Tail: r.Form.Get("tail"),
  119. ShowStdout: stdout,
  120. ShowStderr: stderr,
  121. Details: httputils.BoolValue(r, "details"),
  122. }
  123. msgs, tty, err := s.backend.ContainerLogs(ctx, containerName, logsConfig)
  124. if err != nil {
  125. return err
  126. }
  127. // if has a tty, we're not muxing streams. if it doesn't, we are. simple.
  128. // this is the point of no return for writing a response. once we call
  129. // WriteLogStream, the response has been started and errors will be
  130. // returned in band by WriteLogStream
  131. httputils.WriteLogStream(ctx, w, msgs, logsConfig, !tty)
  132. return nil
  133. }
  134. func (s *containerRouter) getContainersExport(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  135. return s.backend.ContainerExport(vars["name"], w)
  136. }
  137. type bodyOnStartError struct{}
  138. func (bodyOnStartError) Error() string {
  139. return "starting container with non-empty request body was deprecated since API v1.22 and removed in v1.24"
  140. }
  141. func (bodyOnStartError) InvalidParameter() {}
  142. func (s *containerRouter) postContainersStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  143. // If contentLength is -1, we can assumed chunked encoding
  144. // or more technically that the length is unknown
  145. // https://golang.org/src/pkg/net/http/request.go#L139
  146. // net/http otherwise seems to swallow any headers related to chunked encoding
  147. // including r.TransferEncoding
  148. // allow a nil body for backwards compatibility
  149. version := httputils.VersionFromContext(ctx)
  150. var hostConfig *container.HostConfig
  151. // A non-nil json object is at least 7 characters.
  152. if r.ContentLength > 7 || r.ContentLength == -1 {
  153. if versions.GreaterThanOrEqualTo(version, "1.24") {
  154. return bodyOnStartError{}
  155. }
  156. if err := httputils.CheckForJSON(r); err != nil {
  157. return err
  158. }
  159. c, err := s.decoder.DecodeHostConfig(r.Body)
  160. if err != nil {
  161. return err
  162. }
  163. hostConfig = c
  164. }
  165. if err := httputils.ParseForm(r); err != nil {
  166. return err
  167. }
  168. checkpoint := r.Form.Get("checkpoint")
  169. checkpointDir := r.Form.Get("checkpoint-dir")
  170. if err := s.backend.ContainerStart(vars["name"], hostConfig, checkpoint, checkpointDir); err != nil {
  171. return err
  172. }
  173. w.WriteHeader(http.StatusNoContent)
  174. return nil
  175. }
  176. func (s *containerRouter) postContainersStop(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  177. if err := httputils.ParseForm(r); err != nil {
  178. return err
  179. }
  180. var seconds *int
  181. if tmpSeconds := r.Form.Get("t"); tmpSeconds != "" {
  182. valSeconds, err := strconv.Atoi(tmpSeconds)
  183. if err != nil {
  184. return err
  185. }
  186. seconds = &valSeconds
  187. }
  188. if err := s.backend.ContainerStop(vars["name"], seconds); err != nil {
  189. return err
  190. }
  191. w.WriteHeader(http.StatusNoContent)
  192. return nil
  193. }
  194. func (s *containerRouter) postContainersKill(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  195. if err := httputils.ParseForm(r); err != nil {
  196. return err
  197. }
  198. var sig syscall.Signal
  199. name := vars["name"]
  200. // If we have a signal, look at it. Otherwise, do nothing
  201. if sigStr := r.Form.Get("signal"); sigStr != "" {
  202. var err error
  203. if sig, err = signal.ParseSignal(sigStr); err != nil {
  204. return errdefs.InvalidParameter(err)
  205. }
  206. }
  207. if err := s.backend.ContainerKill(name, uint64(sig)); err != nil {
  208. var isStopped bool
  209. if errdefs.IsConflict(err) {
  210. isStopped = true
  211. }
  212. // Return error that's not caused because the container is stopped.
  213. // Return error if the container is not running and the api is >= 1.20
  214. // to keep backwards compatibility.
  215. version := httputils.VersionFromContext(ctx)
  216. if versions.GreaterThanOrEqualTo(version, "1.20") || !isStopped {
  217. return errors.Wrapf(err, "Cannot kill container: %s", name)
  218. }
  219. }
  220. w.WriteHeader(http.StatusNoContent)
  221. return nil
  222. }
  223. func (s *containerRouter) postContainersRestart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  224. if err := httputils.ParseForm(r); err != nil {
  225. return err
  226. }
  227. var seconds *int
  228. if tmpSeconds := r.Form.Get("t"); tmpSeconds != "" {
  229. valSeconds, err := strconv.Atoi(tmpSeconds)
  230. if err != nil {
  231. return err
  232. }
  233. seconds = &valSeconds
  234. }
  235. if err := s.backend.ContainerRestart(vars["name"], seconds); err != nil {
  236. return err
  237. }
  238. w.WriteHeader(http.StatusNoContent)
  239. return nil
  240. }
  241. func (s *containerRouter) postContainersPause(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. if err := s.backend.ContainerPause(vars["name"]); err != nil {
  246. return err
  247. }
  248. w.WriteHeader(http.StatusNoContent)
  249. return nil
  250. }
  251. func (s *containerRouter) postContainersUnpause(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  252. if err := httputils.ParseForm(r); err != nil {
  253. return err
  254. }
  255. if err := s.backend.ContainerUnpause(vars["name"]); err != nil {
  256. return err
  257. }
  258. w.WriteHeader(http.StatusNoContent)
  259. return nil
  260. }
  261. func (s *containerRouter) postContainersWait(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  262. // Behavior changed in version 1.30 to handle wait condition and to
  263. // return headers immediately.
  264. version := httputils.VersionFromContext(ctx)
  265. legacyBehaviorPre130 := versions.LessThan(version, "1.30")
  266. legacyRemovalWaitPre134 := false
  267. // The wait condition defaults to "not-running".
  268. waitCondition := containerpkg.WaitConditionNotRunning
  269. if !legacyBehaviorPre130 {
  270. if err := httputils.ParseForm(r); err != nil {
  271. return err
  272. }
  273. switch container.WaitCondition(r.Form.Get("condition")) {
  274. case container.WaitConditionNextExit:
  275. waitCondition = containerpkg.WaitConditionNextExit
  276. case container.WaitConditionRemoved:
  277. waitCondition = containerpkg.WaitConditionRemoved
  278. legacyRemovalWaitPre134 = versions.LessThan(version, "1.34")
  279. }
  280. }
  281. // Note: the context should get canceled if the client closes the
  282. // connection since this handler has been wrapped by the
  283. // router.WithCancel() wrapper.
  284. waitC, err := s.backend.ContainerWait(ctx, vars["name"], waitCondition)
  285. if err != nil {
  286. return err
  287. }
  288. w.Header().Set("Content-Type", "application/json")
  289. if !legacyBehaviorPre130 {
  290. // Write response header immediately.
  291. w.WriteHeader(http.StatusOK)
  292. if flusher, ok := w.(http.Flusher); ok {
  293. flusher.Flush()
  294. }
  295. }
  296. // Block on the result of the wait operation.
  297. status := <-waitC
  298. // With API < 1.34, wait on WaitConditionRemoved did not return
  299. // in case container removal failed. The only way to report an
  300. // error back to the client is to not write anything (i.e. send
  301. // an empty response which will be treated as an error).
  302. if legacyRemovalWaitPre134 && status.Err() != nil {
  303. return nil
  304. }
  305. var waitError *container.ContainerWaitOKBodyError
  306. if status.Err() != nil {
  307. waitError = &container.ContainerWaitOKBodyError{Message: status.Err().Error()}
  308. }
  309. return json.NewEncoder(w).Encode(&container.ContainerWaitOKBody{
  310. StatusCode: int64(status.ExitCode()),
  311. Error: waitError,
  312. })
  313. }
  314. func (s *containerRouter) getContainersChanges(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  315. changes, err := s.backend.ContainerChanges(vars["name"])
  316. if err != nil {
  317. return err
  318. }
  319. return httputils.WriteJSON(w, http.StatusOK, changes)
  320. }
  321. func (s *containerRouter) getContainersTop(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  322. if err := httputils.ParseForm(r); err != nil {
  323. return err
  324. }
  325. procList, err := s.backend.ContainerTop(vars["name"], r.Form.Get("ps_args"))
  326. if err != nil {
  327. return err
  328. }
  329. return httputils.WriteJSON(w, http.StatusOK, procList)
  330. }
  331. func (s *containerRouter) postContainerRename(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  332. if err := httputils.ParseForm(r); err != nil {
  333. return err
  334. }
  335. name := vars["name"]
  336. newName := r.Form.Get("name")
  337. if err := s.backend.ContainerRename(name, newName); err != nil {
  338. return err
  339. }
  340. w.WriteHeader(http.StatusNoContent)
  341. return nil
  342. }
  343. func (s *containerRouter) postContainerUpdate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  344. if err := httputils.ParseForm(r); err != nil {
  345. return err
  346. }
  347. if err := httputils.CheckForJSON(r); err != nil {
  348. return err
  349. }
  350. var updateConfig container.UpdateConfig
  351. decoder := json.NewDecoder(r.Body)
  352. if err := decoder.Decode(&updateConfig); err != nil {
  353. return err
  354. }
  355. hostConfig := &container.HostConfig{
  356. Resources: updateConfig.Resources,
  357. RestartPolicy: updateConfig.RestartPolicy,
  358. }
  359. name := vars["name"]
  360. resp, err := s.backend.ContainerUpdate(name, hostConfig)
  361. if err != nil {
  362. return err
  363. }
  364. return httputils.WriteJSON(w, http.StatusOK, resp)
  365. }
  366. func (s *containerRouter) postContainersCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  367. if err := httputils.ParseForm(r); err != nil {
  368. return err
  369. }
  370. if err := httputils.CheckForJSON(r); err != nil {
  371. return err
  372. }
  373. name := r.Form.Get("name")
  374. config, hostConfig, networkingConfig, err := s.decoder.DecodeConfig(r.Body)
  375. if err != nil {
  376. return err
  377. }
  378. version := httputils.VersionFromContext(ctx)
  379. adjustCPUShares := versions.LessThan(version, "1.19")
  380. // When using API 1.24 and under, the client is responsible for removing the container
  381. if hostConfig != nil && versions.LessThan(version, "1.25") {
  382. hostConfig.AutoRemove = false
  383. }
  384. ccr, err := s.backend.ContainerCreate(types.ContainerCreateConfig{
  385. Name: name,
  386. Config: config,
  387. HostConfig: hostConfig,
  388. NetworkingConfig: networkingConfig,
  389. AdjustCPUShares: adjustCPUShares,
  390. })
  391. if err != nil {
  392. return err
  393. }
  394. return httputils.WriteJSON(w, http.StatusCreated, ccr)
  395. }
  396. func (s *containerRouter) deleteContainers(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  397. if err := httputils.ParseForm(r); err != nil {
  398. return err
  399. }
  400. name := vars["name"]
  401. config := &types.ContainerRmConfig{
  402. ForceRemove: httputils.BoolValue(r, "force"),
  403. RemoveVolume: httputils.BoolValue(r, "v"),
  404. RemoveLink: httputils.BoolValue(r, "link"),
  405. }
  406. if err := s.backend.ContainerRm(name, config); err != nil {
  407. return err
  408. }
  409. w.WriteHeader(http.StatusNoContent)
  410. return nil
  411. }
  412. func (s *containerRouter) postContainersResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  413. if err := httputils.ParseForm(r); err != nil {
  414. return err
  415. }
  416. height, err := strconv.Atoi(r.Form.Get("h"))
  417. if err != nil {
  418. return errdefs.InvalidParameter(err)
  419. }
  420. width, err := strconv.Atoi(r.Form.Get("w"))
  421. if err != nil {
  422. return errdefs.InvalidParameter(err)
  423. }
  424. return s.backend.ContainerResize(vars["name"], height, width)
  425. }
  426. func (s *containerRouter) postContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  427. err := httputils.ParseForm(r)
  428. if err != nil {
  429. return err
  430. }
  431. containerName := vars["name"]
  432. _, upgrade := r.Header["Upgrade"]
  433. detachKeys := r.FormValue("detachKeys")
  434. hijacker, ok := w.(http.Hijacker)
  435. if !ok {
  436. return errdefs.InvalidParameter(errors.Errorf("error attaching to container %s, hijack connection missing", containerName))
  437. }
  438. setupStreams := func() (io.ReadCloser, io.Writer, io.Writer, error) {
  439. conn, _, err := hijacker.Hijack()
  440. if err != nil {
  441. return nil, nil, nil, err
  442. }
  443. // set raw mode
  444. conn.Write([]byte{})
  445. if upgrade {
  446. fmt.Fprintf(conn, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
  447. } else {
  448. fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
  449. }
  450. closer := func() error {
  451. httputils.CloseStreams(conn)
  452. return nil
  453. }
  454. return ioutils.NewReadCloserWrapper(conn, closer), conn, conn, nil
  455. }
  456. attachConfig := &backend.ContainerAttachConfig{
  457. GetStreams: setupStreams,
  458. UseStdin: httputils.BoolValue(r, "stdin"),
  459. UseStdout: httputils.BoolValue(r, "stdout"),
  460. UseStderr: httputils.BoolValue(r, "stderr"),
  461. Logs: httputils.BoolValue(r, "logs"),
  462. Stream: httputils.BoolValue(r, "stream"),
  463. DetachKeys: detachKeys,
  464. MuxStreams: true,
  465. }
  466. if err = s.backend.ContainerAttach(containerName, attachConfig); err != nil {
  467. logrus.Errorf("Handler for %s %s returned error: %v", r.Method, r.URL.Path, err)
  468. // Remember to close stream if error happens
  469. conn, _, errHijack := hijacker.Hijack()
  470. if errHijack == nil {
  471. statusCode := httputils.GetHTTPErrorStatusCode(err)
  472. statusText := http.StatusText(statusCode)
  473. fmt.Fprintf(conn, "HTTP/1.1 %d %s\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n%s\r\n", statusCode, statusText, err.Error())
  474. httputils.CloseStreams(conn)
  475. } else {
  476. logrus.Errorf("Error Hijacking: %v", err)
  477. }
  478. }
  479. return nil
  480. }
  481. func (s *containerRouter) wsContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  482. if err := httputils.ParseForm(r); err != nil {
  483. return err
  484. }
  485. containerName := vars["name"]
  486. var err error
  487. detachKeys := r.FormValue("detachKeys")
  488. done := make(chan struct{})
  489. started := make(chan struct{})
  490. version := httputils.VersionFromContext(ctx)
  491. setupStreams := func() (io.ReadCloser, io.Writer, io.Writer, error) {
  492. wsChan := make(chan *websocket.Conn)
  493. h := func(conn *websocket.Conn) {
  494. wsChan <- conn
  495. <-done
  496. }
  497. srv := websocket.Server{Handler: h, Handshake: nil}
  498. go func() {
  499. close(started)
  500. srv.ServeHTTP(w, r)
  501. }()
  502. conn := <-wsChan
  503. // In case version 1.28 and above, a binary frame will be sent.
  504. // See 28176 for details.
  505. if versions.GreaterThanOrEqualTo(version, "1.28") {
  506. conn.PayloadType = websocket.BinaryFrame
  507. }
  508. return conn, conn, conn, nil
  509. }
  510. attachConfig := &backend.ContainerAttachConfig{
  511. GetStreams: setupStreams,
  512. Logs: httputils.BoolValue(r, "logs"),
  513. Stream: httputils.BoolValue(r, "stream"),
  514. DetachKeys: detachKeys,
  515. UseStdin: true,
  516. UseStdout: true,
  517. UseStderr: true,
  518. MuxStreams: false, // TODO: this should be true since it's a single stream for both stdout and stderr
  519. }
  520. err = s.backend.ContainerAttach(containerName, attachConfig)
  521. close(done)
  522. select {
  523. case <-started:
  524. if err != nil {
  525. logrus.Errorf("Error attaching websocket: %s", err)
  526. } else {
  527. logrus.Debug("websocket connection was closed by client")
  528. }
  529. return nil
  530. default:
  531. }
  532. return err
  533. }
  534. func (s *containerRouter) postContainersPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  535. if err := httputils.ParseForm(r); err != nil {
  536. return err
  537. }
  538. pruneFilters, err := filters.FromJSON(r.Form.Get("filters"))
  539. if err != nil {
  540. return errdefs.InvalidParameter(err)
  541. }
  542. pruneReport, err := s.backend.ContainersPrune(ctx, pruneFilters)
  543. if err != nil {
  544. return err
  545. }
  546. return httputils.WriteJSON(w, http.StatusOK, pruneReport)
  547. }