mirror of
https://github.com/containers/podman.git
synced 2025-09-26 00:06:04 +08:00
do not commit default volumes from container
when performing a container commit, we should not add the default list of volumes for a container to the resulting image. it will cause the resulting image to crash when run subsequently. Signed-off-by: baude <bbaude@redhat.com> Closes: #699 Approved by: mheon
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -120,7 +121,17 @@ func commitCmd(c *cli.Context) error {
|
|||||||
Changes: c.StringSlice("change"),
|
Changes: c.StringSlice("change"),
|
||||||
Author: c.String("author"),
|
Author: c.String("author"),
|
||||||
}
|
}
|
||||||
newImage, err := ctr.Commit(getContext(), reference, options)
|
var createArtifact createConfig
|
||||||
|
artifact, err := ctr.GetArtifact("create-config")
|
||||||
|
if err == nil {
|
||||||
|
if err := json.Unmarshal(artifact, &createArtifact); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mounts := getMounts(createArtifact.Volumes, true)
|
||||||
|
command := createArtifact.Command
|
||||||
|
entryPoint := createArtifact.Entrypoint
|
||||||
|
newImage, err := ctr.Commit(getContext(), reference, options, strings.Split(mounts, ","), command, entryPoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ type ContainerCommitOptions struct {
|
|||||||
|
|
||||||
// Commit commits the changes between a container and its image, creating a new
|
// Commit commits the changes between a container and its image, creating a new
|
||||||
// image
|
// image
|
||||||
func (c *Container) Commit(ctx context.Context, destImage string, options ContainerCommitOptions) (*image.Image, error) {
|
func (c *Container) Commit(ctx context.Context, destImage string, options ContainerCommitOptions, mounts, command, entryPoint []string) (*image.Image, error) {
|
||||||
if !c.batched {
|
if !c.batched {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
@ -74,11 +74,15 @@ func (c *Container) Commit(ctx context.Context, destImage string, options Contai
|
|||||||
// add it to the resulting image.
|
// add it to the resulting image.
|
||||||
|
|
||||||
// Entrypoint - always set this first or cmd will get wiped out
|
// Entrypoint - always set this first or cmd will get wiped out
|
||||||
importBuilder.SetEntrypoint(c.Spec().Process.Args)
|
if len(entryPoint) > 0 {
|
||||||
|
importBuilder.SetEntrypoint(entryPoint)
|
||||||
|
}
|
||||||
|
|
||||||
// Cmd
|
// Cmd
|
||||||
// We cannot differentiate between cmd and entrypoint here
|
if len(command) > 0 {
|
||||||
// so we assign args to both
|
importBuilder.SetCmd(command)
|
||||||
importBuilder.SetCmd(c.Spec().Process.Args)
|
}
|
||||||
|
|
||||||
// Env
|
// Env
|
||||||
for _, e := range c.config.Spec.Process.Env {
|
for _, e := range c.config.Spec.Process.Env {
|
||||||
splitEnv := strings.Split(e, "=")
|
splitEnv := strings.Split(e, "=")
|
||||||
@ -96,8 +100,10 @@ func (c *Container) Commit(ctx context.Context, destImage string, options Contai
|
|||||||
// User
|
// User
|
||||||
importBuilder.SetUser(c.User())
|
importBuilder.SetUser(c.User())
|
||||||
// Volumes
|
// Volumes
|
||||||
for _, v := range c.config.Spec.Mounts {
|
for _, v := range mounts {
|
||||||
importBuilder.AddVolume(v.Source)
|
if v != "" {
|
||||||
|
importBuilder.AddVolume(v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Workdir
|
// Workdir
|
||||||
importBuilder.SetWorkDir(c.Spec().Process.Cwd)
|
importBuilder.SetWorkDir(c.Spec().Process.Cwd)
|
||||||
|
@ -110,4 +110,26 @@ var _ = Describe("Podman commit", func() {
|
|||||||
check.WaitWithDefaultTimeout()
|
check.WaitWithDefaultTimeout()
|
||||||
Expect(check.ExitCode()).To(Equal(0))
|
Expect(check.ExitCode()).To(Equal(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman commit with volume mounts", func() {
|
||||||
|
s := podmanTest.Podman([]string{"run", "--name", "test1", "-v", "/tmp:/foo", "alpine", "date"})
|
||||||
|
s.WaitWithDefaultTimeout()
|
||||||
|
Expect(s.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
c := podmanTest.Podman([]string{"commit", "test1", "newimage"})
|
||||||
|
c.WaitWithDefaultTimeout()
|
||||||
|
Expect(c.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
inspect := podmanTest.Podman([]string{"inspect", "newimage"})
|
||||||
|
inspect.WaitWithDefaultTimeout()
|
||||||
|
Expect(inspect.ExitCode()).To(Equal(0))
|
||||||
|
image := inspect.InspectImageJSON()
|
||||||
|
_, ok := image[0].ContainerConfig.Volumes["/tmp"]
|
||||||
|
Expect(ok).To(BeTrue())
|
||||||
|
|
||||||
|
r := podmanTest.Podman([]string{"run", "newimage"})
|
||||||
|
r.WaitWithDefaultTimeout()
|
||||||
|
Expect(r.ExitCode()).To(Equal(0))
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user