1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

clean up the context when a build fails

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
unclejack 2014-09-15 16:01:23 +03:00
parent a10a86d437
commit 31c0039022
2 changed files with 31 additions and 4 deletions

View file

@ -119,6 +119,12 @@ func (b *Builder) Run(context io.Reader) (string, error) {
return "", err return "", err
} }
defer func() {
if err := os.RemoveAll(b.contextPath); err != nil {
log.Debugf("[BUILDER] failed to remove temporary context: %s", err)
}
}()
filename := path.Join(b.contextPath, "Dockerfile") filename := path.Join(b.contextPath, "Dockerfile")
fi, err := os.Stat(filename) fi, err := os.Stat(filename)
@ -164,10 +170,6 @@ func (b *Builder) Run(context io.Reader) (string, error) {
return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?\n") return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?\n")
} }
if err := os.RemoveAll(b.contextPath); err != nil {
log.Debugf("[BUILDER] failed to remove temporary context: %s", err)
}
fmt.Fprintf(b.OutStream, "Successfully built %s\n", utils.TruncateID(b.image)) fmt.Fprintf(b.OutStream, "Successfully built %s\n", utils.TruncateID(b.image))
return b.image, nil return b.image, nil
} }

View file

@ -731,6 +731,31 @@ func TestBuildContextCleanup(t *testing.T) {
logDone("build - verify context cleanup works properly") logDone("build - verify context cleanup works properly")
} }
func TestBuildContextCleanupFailedBuild(t *testing.T) {
name := "testbuildcontextcleanup"
defer deleteImages(name)
entries, err := ioutil.ReadDir("/var/lib/docker/tmp")
if err != nil {
t.Fatalf("failed to list contents of tmp dir: %s", err)
}
_, err = buildImage(name,
`FROM scratch
RUN /non/existing/command`,
true)
if err == nil {
t.Fatalf("expected build to fail, but it didn't")
}
entriesFinal, err := ioutil.ReadDir("/var/lib/docker/tmp")
if err != nil {
t.Fatalf("failed to list contents of tmp dir: %s", err)
}
if err = compareDirectoryEntries(entries, entriesFinal); err != nil {
t.Fatalf("context should have been deleted, but wasn't")
}
logDone("build - verify context cleanup works properly after a failed build")
}
func TestBuildCmd(t *testing.T) { func TestBuildCmd(t *testing.T) {
name := "testbuildcmd" name := "testbuildcmd"
expected := "[/bin/echo Hello World]" expected := "[/bin/echo Hello World]"