container_routes.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. package container // import "github.com/docker/docker/api/server/router/container"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strconv"
  9. "syscall"
  10. "github.com/docker/docker/api/server/httputils"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/api/types/backend"
  13. "github.com/docker/docker/api/types/container"
  14. "github.com/docker/docker/api/types/filters"
  15. "github.com/docker/docker/api/types/versions"
  16. containerpkg "github.com/docker/docker/container"
  17. "github.com/docker/docker/errdefs"
  18. "github.com/docker/docker/pkg/ioutils"
  19. "github.com/docker/docker/pkg/signal"
  20. "github.com/pkg/errors"
  21. "github.com/sirupsen/logrus"
  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. waitC, err := s.backend.ContainerWait(ctx, vars["name"], waitCondition)
  282. if err != nil {
  283. return err
  284. }
  285. w.Header().Set("Content-Type", "application/json")
  286. if !legacyBehaviorPre130 {
  287. // Write response header immediately.
  288. w.WriteHeader(http.StatusOK)
  289. if flusher, ok := w.(http.Flusher); ok {
  290. flusher.Flush()
  291. }
  292. }
  293. // Block on the result of the wait operation.
  294. status := <-waitC
  295. // With API < 1.34, wait on WaitConditionRemoved did not return
  296. // in case container removal failed. The only way to report an
  297. // error back to the client is to not write anything (i.e. send
  298. // an empty response which will be treated as an error).
  299. if legacyRemovalWaitPre134 && status.Err() != nil {
  300. return nil
  301. }
  302. var waitError *container.ContainerWaitOKBodyError
  303. if status.Err() != nil {
  304. waitError = &container.ContainerWaitOKBodyError{Message: status.Err().Error()}
  305. }
  306. return json.NewEncoder(w).Encode(&container.ContainerWaitOKBody{
  307. StatusCode: int64(status.ExitCode()),
  308. Error: waitError,
  309. })
  310. }
  311. func (s *containerRouter) getContainersChanges(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  312. changes, err := s.backend.ContainerChanges(vars["name"])
  313. if err != nil {
  314. return err
  315. }
  316. return httputils.WriteJSON(w, http.StatusOK, changes)
  317. }
  318. func (s *containerRouter) getContainersTop(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  319. if err := httputils.ParseForm(r); err != nil {
  320. return err
  321. }
  322. procList, err := s.backend.ContainerTop(vars["name"], r.Form.Get("ps_args"))
  323. if err != nil {
  324. return err
  325. }
  326. return httputils.WriteJSON(w, http.StatusOK, procList)
  327. }
  328. func (s *containerRouter) postContainerRename(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  329. if err := httputils.ParseForm(r); err != nil {
  330. return err
  331. }
  332. name := vars["name"]
  333. newName := r.Form.Get("name")
  334. if err := s.backend.ContainerRename(name, newName); err != nil {
  335. return err
  336. }
  337. w.WriteHeader(http.StatusNoContent)
  338. return nil
  339. }
  340. func (s *containerRouter) postContainerUpdate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  341. if err := httputils.ParseForm(r); err != nil {
  342. return err
  343. }
  344. if err := httputils.CheckForJSON(r); err != nil {
  345. return err
  346. }
  347. var updateConfig container.UpdateConfig
  348. decoder := json.NewDecoder(r.Body)
  349. if err := decoder.Decode(&updateConfig); err != nil {
  350. return err
  351. }
  352. hostConfig := &container.HostConfig{
  353. Resources: updateConfig.Resources,
  354. RestartPolicy: updateConfig.RestartPolicy,
  355. }
  356. name := vars["name"]
  357. resp, err := s.backend.ContainerUpdate(name, hostConfig)
  358. if err != nil {
  359. return err
  360. }
  361. return httputils.WriteJSON(w, http.StatusOK, resp)
  362. }
  363. func (s *containerRouter) postContainersCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  364. if err := httputils.ParseForm(r); err != nil {
  365. return err
  366. }
  367. if err := httputils.CheckForJSON(r); err != nil {
  368. return err
  369. }
  370. name := r.Form.Get("name")
  371. config, hostConfig, networkingConfig, err := s.decoder.DecodeConfig(r.Body)
  372. if err != nil {
  373. return err
  374. }
  375. version := httputils.VersionFromContext(ctx)
  376. adjustCPUShares := versions.LessThan(version, "1.19")
  377. // When using API 1.24 and under, the client is responsible for removing the container
  378. if hostConfig != nil && versions.LessThan(version, "1.25") {
  379. hostConfig.AutoRemove = false
  380. }
  381. if hostConfig != nil && versions.LessThan(version, "1.40") {
  382. // Ignore BindOptions.NonRecursive because it was added in API 1.40.
  383. for _, m := range hostConfig.Mounts {
  384. if bo := m.BindOptions; bo != nil {
  385. bo.NonRecursive = false
  386. }
  387. }
  388. // Ignore KernelMemoryTCP because it was added in API 1.40.
  389. hostConfig.KernelMemoryTCP = 0
  390. }
  391. // Ignore Capabilities because it was added in API 1.40.
  392. if hostConfig != nil && versions.LessThan(version, "1.40") {
  393. hostConfig.Capabilities = nil
  394. }
  395. ccr, err := s.backend.ContainerCreate(types.ContainerCreateConfig{
  396. Name: name,
  397. Config: config,
  398. HostConfig: hostConfig,
  399. NetworkingConfig: networkingConfig,
  400. AdjustCPUShares: adjustCPUShares,
  401. })
  402. if err != nil {
  403. return err
  404. }
  405. return httputils.WriteJSON(w, http.StatusCreated, ccr)
  406. }
  407. func (s *containerRouter) deleteContainers(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  408. if err := httputils.ParseForm(r); err != nil {
  409. return err
  410. }
  411. name := vars["name"]
  412. config := &types.ContainerRmConfig{
  413. ForceRemove: httputils.BoolValue(r, "force"),
  414. RemoveVolume: httputils.BoolValue(r, "v"),
  415. RemoveLink: httputils.BoolValue(r, "link"),
  416. }
  417. if err := s.backend.ContainerRm(name, config); err != nil {
  418. return err
  419. }
  420. w.WriteHeader(http.StatusNoContent)
  421. return nil
  422. }
  423. func (s *containerRouter) postContainersResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  424. if err := httputils.ParseForm(r); err != nil {
  425. return err
  426. }
  427. height, err := strconv.Atoi(r.Form.Get("h"))
  428. if err != nil {
  429. return errdefs.InvalidParameter(err)
  430. }
  431. width, err := strconv.Atoi(r.Form.Get("w"))
  432. if err != nil {
  433. return errdefs.InvalidParameter(err)
  434. }
  435. return s.backend.ContainerResize(vars["name"], height, width)
  436. }
  437. func (s *containerRouter) postContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  438. err := httputils.ParseForm(r)
  439. if err != nil {
  440. return err
  441. }
  442. containerName := vars["name"]
  443. _, upgrade := r.Header["Upgrade"]
  444. detachKeys := r.FormValue("detachKeys")
  445. hijacker, ok := w.(http.Hijacker)
  446. if !ok {
  447. return errdefs.InvalidParameter(errors.Errorf("error attaching to container %s, hijack connection missing", containerName))
  448. }
  449. setupStreams := func() (io.ReadCloser, io.Writer, io.Writer, error) {
  450. conn, _, err := hijacker.Hijack()
  451. if err != nil {
  452. return nil, nil, nil, err
  453. }
  454. // set raw mode
  455. conn.Write([]byte{})
  456. if upgrade {
  457. 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")
  458. } else {
  459. fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
  460. }
  461. closer := func() error {
  462. httputils.CloseStreams(conn)
  463. return nil
  464. }
  465. return ioutils.NewReadCloserWrapper(conn, closer), conn, conn, nil
  466. }
  467. attachConfig := &backend.ContainerAttachConfig{
  468. GetStreams: setupStreams,
  469. UseStdin: httputils.BoolValue(r, "stdin"),
  470. UseStdout: httputils.BoolValue(r, "stdout"),
  471. UseStderr: httputils.BoolValue(r, "stderr"),
  472. Logs: httputils.BoolValue(r, "logs"),
  473. Stream: httputils.BoolValue(r, "stream"),
  474. DetachKeys: detachKeys,
  475. MuxStreams: true,
  476. }
  477. if err = s.backend.ContainerAttach(containerName, attachConfig); err != nil {
  478. logrus.Errorf("Handler for %s %s returned error: %v", r.Method, r.URL.Path, err)
  479. // Remember to close stream if error happens
  480. conn, _, errHijack := hijacker.Hijack()
  481. if errHijack == nil {
  482. statusCode := httputils.GetHTTPErrorStatusCode(err)
  483. statusText := http.StatusText(statusCode)
  484. 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())
  485. httputils.CloseStreams(conn)
  486. } else {
  487. logrus.Errorf("Error Hijacking: %v", err)
  488. }
  489. }
  490. return nil
  491. }
  492. func (s *containerRouter) wsContainersAttach(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  493. if err := httputils.ParseForm(r); err != nil {
  494. return err
  495. }
  496. containerName := vars["name"]
  497. var err error
  498. detachKeys := r.FormValue("detachKeys")
  499. done := make(chan struct{})
  500. started := make(chan struct{})
  501. version := httputils.VersionFromContext(ctx)
  502. setupStreams := func() (io.ReadCloser, io.Writer, io.Writer, error) {
  503. wsChan := make(chan *websocket.Conn)
  504. h := func(conn *websocket.Conn) {
  505. wsChan <- conn
  506. <-done
  507. }
  508. srv := websocket.Server{Handler: h, Handshake: nil}
  509. go func() {
  510. close(started)
  511. srv.ServeHTTP(w, r)
  512. }()
  513. conn := <-wsChan
  514. // In case version 1.28 and above, a binary frame will be sent.
  515. // See 28176 for details.
  516. if versions.GreaterThanOrEqualTo(version, "1.28") {
  517. conn.PayloadType = websocket.BinaryFrame
  518. }
  519. return conn, conn, conn, nil
  520. }
  521. attachConfig := &backend.ContainerAttachConfig{
  522. GetStreams: setupStreams,
  523. Logs: httputils.BoolValue(r, "logs"),
  524. Stream: httputils.BoolValue(r, "stream"),
  525. DetachKeys: detachKeys,
  526. UseStdin: true,
  527. UseStdout: true,
  528. UseStderr: true,
  529. MuxStreams: false, // TODO: this should be true since it's a single stream for both stdout and stderr
  530. }
  531. err = s.backend.ContainerAttach(containerName, attachConfig)
  532. close(done)
  533. select {
  534. case <-started:
  535. if err != nil {
  536. logrus.Errorf("Error attaching websocket: %s", err)
  537. } else {
  538. logrus.Debug("websocket connection was closed by client")
  539. }
  540. return nil
  541. default:
  542. }
  543. return err
  544. }
  545. func (s *containerRouter) postContainersPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  546. if err := httputils.ParseForm(r); err != nil {
  547. return err
  548. }
  549. pruneFilters, err := filters.FromJSON(r.Form.Get("filters"))
  550. if err != nil {
  551. return errdefs.InvalidParameter(err)
  552. }
  553. pruneReport, err := s.backend.ContainersPrune(ctx, pruneFilters)
  554. if err != nil {
  555. return err
  556. }
  557. return httputils.WriteJSON(w, http.StatusOK, pruneReport)
  558. }