X Tutup
Skip to content

Commit 80679f4

Browse files
author
Marcus Thiesen
committed
Stop leaking tar files in temporary folder
The temporary tar files generated from the docker folder are preserved after the contents is send to the docker deamon. In order to prevent filling up the temporary directory those files must be deleted. As the API is based on InputStreams the easiest solution is to wrap the input stream an delete the file on close.
1 parent 60a8872 commit 80679f4

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/main/java/com/github/dockerjava/core/dockerfile/Dockerfile.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package com.github.dockerjava.core.dockerfile;
22

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.util.ArrayList;
7-
import java.util.Collection;
8-
import java.util.HashMap;
9-
import java.util.List;
10-
import java.util.Map;
11-
import java.util.UUID;
12-
13-
import org.apache.commons.io.FileUtils;
14-
import org.apache.commons.io.FilenameUtils;
15-
import org.apache.commons.io.filefilter.TrueFileFilter;
16-
173
import com.github.dockerjava.api.DockerClientException;
184
import com.github.dockerjava.core.CompressArchiveUtil;
195
import com.github.dockerjava.core.FilePathUtil;
@@ -24,6 +10,20 @@
2410
import com.google.common.base.Objects;
2511
import com.google.common.base.Optional;
2612
import com.google.common.collect.Collections2;
13+
import org.apache.commons.io.FileUtils;
14+
import org.apache.commons.io.FilenameUtils;
15+
import org.apache.commons.io.IOUtils;
16+
import org.apache.commons.io.filefilter.TrueFileFilter;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.ArrayList;
22+
import java.util.Collection;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.UUID;
2727

2828
/**
2929
* Parse a Dockerfile.
@@ -130,15 +130,29 @@ public InputStream buildDockerFolderTar() {
130130

131131
public InputStream buildDockerFolderTar(File directory) {
132132

133-
// ARCHIVE TAR
134133
File dockerFolderTar = null;
135134

136135
try {
137-
String archiveNameWithOutExtension = UUID.randomUUID().toString();
136+
final String archiveNameWithOutExtension = UUID.randomUUID().toString();
138137

139138
dockerFolderTar = CompressArchiveUtil.archiveTARFiles(directory, filesToAdd,
140139
archiveNameWithOutExtension);
141-
return FileUtils.openInputStream(dockerFolderTar);
140+
141+
final InputStream tarInputStream = FileUtils.openInputStream(dockerFolderTar);
142+
final File tarFile = dockerFolderTar;
143+
144+
return new InputStream() {
145+
@Override
146+
public int read() throws IOException {
147+
return tarInputStream.read();
148+
}
149+
150+
@Override
151+
public void close() throws IOException {
152+
IOUtils.closeQuietly(tarInputStream);
153+
FileUtils.deleteQuietly(tarFile);
154+
}
155+
};
142156

143157
} catch (IOException ex) {
144158
FileUtils.deleteQuietly(dockerFolderTar);

0 commit comments

Comments
 (0)
X Tutup