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:
parent
a10a86d437
commit
31c0039022
2 changed files with 31 additions and 4 deletions
|
@ -119,6 +119,12 @@ func (b *Builder) Run(context io.Reader) (string, error) {
|
|||
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")
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
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))
|
||||
return b.image, nil
|
||||
}
|
||||
|
|
|
@ -731,6 +731,31 @@ func TestBuildContextCleanup(t *testing.T) {
|
|||
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) {
|
||||
name := "testbuildcmd"
|
||||
expected := "[/bin/echo Hello World]"
|
||||
|
|
Loading…
Reference in a new issue