podman

Форк
0
80 строк · 2.2 Кб
1
package docker
2

3
import (
4
	"context"
5
	"encoding/json"
6
	"errors"
7
	"net/http"
8
	"strings"
9
)
10

11
// ErrContainerAlreadyExists is the error returned by CreateContainer when the
12
// container already exists.
13
var ErrContainerAlreadyExists = errors.New("container already exists")
14

15
// CreateContainerOptions specify parameters to the CreateContainer function.
16
//
17
// See https://goo.gl/tyzwVM for more details.
18
type CreateContainerOptions struct {
19
	Name             string
20
	Platform         string
21
	Config           *Config           `qs:"-"`
22
	HostConfig       *HostConfig       `qs:"-"`
23
	NetworkingConfig *NetworkingConfig `qs:"-"`
24
	Context          context.Context
25
}
26

27
// CreateContainer creates a new container, returning the container instance,
28
// or an error in case of failure.
29
//
30
// The returned container instance contains only the container ID. To get more
31
// details about the container after creating it, use InspectContainer.
32
//
33
// See https://goo.gl/tyzwVM for more details.
34
func (c *Client) CreateContainer(opts CreateContainerOptions) (*Container, error) {
35
	path := "/containers/create?" + queryString(opts)
36
	resp, err := c.do(
37
		http.MethodPost,
38
		path,
39
		doOptions{
40
			data: struct {
41
				*Config
42
				HostConfig       *HostConfig       `json:"HostConfig,omitempty" yaml:"HostConfig,omitempty" toml:"HostConfig,omitempty"`
43
				NetworkingConfig *NetworkingConfig `json:"NetworkingConfig,omitempty" yaml:"NetworkingConfig,omitempty" toml:"NetworkingConfig,omitempty"`
44
			}{
45
				opts.Config,
46
				opts.HostConfig,
47
				opts.NetworkingConfig,
48
			},
49
			context: opts.Context,
50
		},
51
	)
52

53
	var e *Error
54
	if errors.As(err, &e) {
55
		if e.Status == http.StatusNotFound && strings.Contains(e.Message, "No such image") {
56
			return nil, ErrNoSuchImage
57
		}
58
		if e.Status == http.StatusConflict {
59
			return nil, ErrContainerAlreadyExists
60
		}
61
		// Workaround for 17.09 bug returning 400 instead of 409.
62
		// See https://github.com/moby/moby/issues/35021
63
		if e.Status == http.StatusBadRequest && strings.Contains(e.Message, "Conflict.") {
64
			return nil, ErrContainerAlreadyExists
65
		}
66
	}
67

68
	if err != nil {
69
		return nil, err
70
	}
71
	defer resp.Body.Close()
72
	var container Container
73
	if err := json.NewDecoder(resp.Body).Decode(&container); err != nil {
74
		return nil, err
75
	}
76

77
	container.Name = opts.Name
78

79
	return &container, nil
80
}
81

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.