-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
container_commit.go
178 lines (163 loc) · 5.03 KB
/
container_commit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//go:build !remote
package libpod
import (
"context"
"errors"
"fmt"
"strings"
"github.com/containers/buildah"
"github.com/containers/common/libimage"
is "github.com/containers/image/v5/storage"
"github.com/containers/image/v5/types"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/libpod/events"
"github.com/sirupsen/logrus"
)
// ContainerCommitOptions is a struct used to commit a container to an image
// It uses buildah's CommitOptions as a base. Long-term we might wish to
// decouple these because it includes duplicates of fields that are in, or
// could later be added, to buildah's CommitOptions, which gets confusing
type ContainerCommitOptions struct {
buildah.CommitOptions
Pause bool
IncludeVolumes bool
Author string
Message string
Changes []string // gets merged with CommitOptions.OverrideChanges
Squash bool // always used instead of CommitOptions.Squash
}
// Commit commits the changes between a container and its image, creating a new
// image
func (c *Container) Commit(ctx context.Context, destImage string, options ContainerCommitOptions) (*libimage.Image, error) {
if c.config.Rootfs != "" {
return nil, errors.New("cannot commit a container that uses an exploded rootfs")
}
if !c.batched {
c.lock.Lock()
defer c.lock.Unlock()
if err := c.syncContainer(); err != nil {
return nil, err
}
}
if c.state.State == define.ContainerStateRunning && options.Pause {
if err := c.pause(); err != nil {
return nil, fmt.Errorf("pausing container %q to commit: %w", c.ID(), err)
}
defer func() {
if err := c.unpause(); err != nil {
logrus.Errorf("Unpausing container %q: %v", c.ID(), err)
}
}()
}
builderOptions := buildah.ImportOptions{
Container: c.ID(),
SignaturePolicyPath: options.SignaturePolicyPath,
}
commitOptions := buildah.CommitOptions{
SignaturePolicyPath: options.SignaturePolicyPath,
ReportWriter: options.ReportWriter,
Squash: options.Squash,
SystemContext: c.runtime.imageContext,
PreferredManifestType: options.PreferredManifestType,
OverrideChanges: append(append([]string{}, options.Changes...), options.CommitOptions.OverrideChanges...),
OverrideConfig: options.CommitOptions.OverrideConfig,
}
importBuilder, err := buildah.ImportBuilder(ctx, c.runtime.store, builderOptions)
if err != nil {
return nil, err
}
importBuilder.Format = options.PreferredManifestType
if options.Author != "" {
importBuilder.SetMaintainer(options.Author)
}
if options.Message != "" {
importBuilder.SetComment(options.Message)
}
// We need to take meta we find in the current container and
// add it to the resulting image.
// Entrypoint - always set this first or cmd will get wiped out
importBuilder.SetEntrypoint(c.config.Entrypoint)
// Cmd
importBuilder.SetCmd(c.config.Command)
// Env
// TODO - this includes all the default environment vars as well
// Should we store the ENV we actually want in the spec separately?
if c.config.Spec.Process != nil {
for _, e := range c.config.Spec.Process.Env {
key, val, _ := strings.Cut(e, "=")
importBuilder.SetEnv(key, val)
}
}
// Expose ports
for _, p := range c.config.PortMappings {
importBuilder.SetPort(fmt.Sprintf("%d/%s", p.ContainerPort, p.Protocol))
}
for port, protocols := range c.config.ExposedPorts {
for _, protocol := range protocols {
importBuilder.SetPort(fmt.Sprintf("%d/%s", port, protocol))
}
}
// Labels
for k, v := range c.Labels() {
importBuilder.SetLabel(k, v)
}
// No stop signal
// User
if c.config.User != "" {
importBuilder.SetUser(c.config.User)
}
// Volumes
if options.IncludeVolumes {
for _, v := range c.config.UserVolumes {
if v != "" {
importBuilder.AddVolume(v)
}
}
} else {
// Only include anonymous named volumes added by the user by
// default.
for _, v := range c.config.NamedVolumes {
include := false
for _, userVol := range c.config.UserVolumes {
if userVol == v.Dest {
include = true
break
}
}
if include {
vol, err := c.runtime.GetVolume(v.Name)
if err != nil {
return nil, fmt.Errorf("volume %s used in container %s has been removed: %w", v.Name, c.ID(), err)
}
if vol.Anonymous() {
importBuilder.AddVolume(v.Dest)
}
}
}
}
// Workdir
importBuilder.SetWorkDir(c.config.Spec.Process.Cwd)
var commitRef types.ImageReference
if destImage != "" {
// Now resolve the name.
resolvedImageName, err := c.runtime.LibimageRuntime().ResolveName(destImage)
if err != nil {
return nil, err
}
imageRef, err := is.Transport.ParseStoreReference(c.runtime.store, resolvedImageName)
if err != nil {
return nil, fmt.Errorf("parsing target image name %q: %w", destImage, err)
}
commitRef = imageRef
}
id, _, _, err := importBuilder.Commit(ctx, commitRef, commitOptions)
if err != nil {
return nil, err
}
defer c.newContainerEvent(events.Commit)
img, _, err := c.runtime.libimageRuntime.LookupImage(id, nil)
if err != nil {
return nil, err
}
return img, nil
}