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