podman
42 строки · 1.1 Кб
1package docker
2
3import (
4"context"
5"errors"
6"fmt"
7"net/http"
8)
9
10// StopContainer stops a container, killing it after the given timeout (in
11// seconds).
12//
13// See https://goo.gl/R9dZcV for more details.
14func (c *Client) StopContainer(id string, timeout uint) error {
15return c.stopContainer(id, timeout, doOptions{})
16}
17
18// StopContainerWithContext stops a container, killing it after the given
19// timeout (in seconds). The context can be used to cancel the stop
20// container request.
21//
22// See https://goo.gl/R9dZcV for more details.
23func (c *Client) StopContainerWithContext(id string, timeout uint, ctx context.Context) error {
24return c.stopContainer(id, timeout, doOptions{context: ctx})
25}
26
27func (c *Client) stopContainer(id string, timeout uint, opts doOptions) error {
28path := fmt.Sprintf("/containers/%s/stop?t=%d", id, timeout)
29resp, err := c.do(http.MethodPost, path, opts)
30if err != nil {
31var e *Error
32if errors.As(err, &e) && e.Status == http.StatusNotFound {
33return &NoSuchContainer{ID: id}
34}
35return err
36}
37defer resp.Body.Close()
38if resp.StatusCode == http.StatusNotModified {
39return &ContainerNotRunning{ID: id}
40}
41return nil
42}
43