Skip to content

Commit 60fb248

Browse files
author
Marcus Linke
committed
Support wildcards in Dockerfile. Fix issue#129
1 parent d1ea20f commit 60fb248

File tree

6 files changed

+100
-31
lines changed

6 files changed

+100
-31
lines changed

src/main/java/com/github/dockerjava/core/command/BuildImageCmdImpl.java

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
* Build an image from Dockerfile.
3232
*
3333
*/
34-
public class BuildImageCmdImpl extends AbstrDockerCmd<BuildImageCmd, InputStream> implements BuildImageCmd {
34+
public class BuildImageCmdImpl extends
35+
AbstrDockerCmd<BuildImageCmd, InputStream> implements BuildImageCmd {
3536

3637
private static final Pattern ADD_OR_COPY_PATTERN = Pattern
3738
.compile("^(ADD|COPY)\\s+(.*)\\s+(.*)$");
@@ -62,8 +63,8 @@ public BuildImageCmdImpl(BuildImageCmd.Exec exec, InputStream tarInputStream) {
6263
super(exec);
6364
Preconditions.checkNotNull(tarInputStream, "tarInputStream is null");
6465
withTarInputStream(tarInputStream);
65-
}
66-
66+
}
67+
6768
@Override
6869
public InputStream getTarInputStream() {
6970
return tarInputStream;
@@ -75,7 +76,7 @@ public BuildImageCmdImpl withTarInputStream(InputStream tarInputStream) {
7576
this.tarInputStream = tarInputStream;
7677
return this;
7778
}
78-
79+
7980
@Override
8081
public BuildImageCmdImpl withTag(String tag) {
8182
Preconditions.checkNotNull(tag, "Tag is null");
@@ -113,7 +114,7 @@ public BuildImageCmdImpl withNoCache(boolean noCache) {
113114
this.noCache = noCache;
114115
return this;
115116
}
116-
117+
117118
@Override
118119
public BuildImageCmdImpl withRemove() {
119120
return withRemove(true);
@@ -124,7 +125,7 @@ public BuildImageCmdImpl withRemove(boolean rm) {
124125
this.remove = rm;
125126
return this;
126127
}
127-
128+
128129
@Override
129130
public BuildImageCmdImpl withQuiet() {
130131
return withQuiet(true);
@@ -142,7 +143,7 @@ public void close() throws IOException {
142143
if (tarFile != null) {
143144
FileUtils.deleteQuietly(tarFile);
144145
}
145-
146+
146147
tarInputStream.close();
147148
}
148149

@@ -152,8 +153,7 @@ public String toString() {
152153
.append(tag != null ? "-t " + tag + " " : "")
153154
.append(noCache ? "--nocache=true " : "")
154155
.append(quiet ? "--quiet=true " : "")
155-
.append(!remove ? "--rm=false " : "")
156-
.toString();
156+
.append(!remove ? "--rm=false " : "").toString();
157157
}
158158

159159
protected File buildDockerFolderTar(File dockerFolder) {
@@ -182,23 +182,30 @@ protected File buildDockerFolderTar(File dockerFolder) {
182182
File dockerIgnoreFile = new File(dockerFolder, ".dockerignore");
183183
if (dockerIgnoreFile.exists()) {
184184
int lineNumber = 0;
185-
List<String> dockerIgnoreFileContent = FileUtils.readLines(dockerIgnoreFile);
186-
for (String pattern: dockerIgnoreFileContent) {
185+
List<String> dockerIgnoreFileContent = FileUtils
186+
.readLines(dockerIgnoreFile);
187+
for (String pattern : dockerIgnoreFileContent) {
187188
lineNumber++;
188189
pattern = pattern.trim();
189190
if (pattern.isEmpty()) {
190191
continue; // skip empty lines
191192
}
192193
pattern = FilenameUtils.normalize(pattern);
193194
try {
194-
// validate pattern and make sure we aren't excluding Dockerfile
195+
// validate pattern and make sure we aren't excluding
196+
// Dockerfile
195197
if (GoLangFileMatch.match(pattern, "Dockerfile")) {
196198
throw new DockerClientException(
197-
String.format("Dockerfile is excluded by pattern '%s' on line %s in .dockerignore file", pattern, lineNumber));
199+
String.format(
200+
"Dockerfile is excluded by pattern '%s' on line %s in .dockerignore file",
201+
pattern, lineNumber));
198202
}
199203
ignores.add(pattern);
200204
} catch (GoLangFileMatchException e) {
201-
throw new DockerClientException(String.format("Invalid pattern '%s' on line %s in .dockerignore file", pattern, lineNumber));
205+
throw new DockerClientException(
206+
String.format(
207+
"Invalid pattern '%s' on line %s in .dockerignore file",
208+
pattern, lineNumber));
202209
}
203210
}
204211
}
@@ -254,19 +261,26 @@ protected File buildDockerFolderTar(File dockerFolder) {
254261
src, dockerFolder));
255262
}
256263

257-
if (!src.exists()) {
258-
throw new DockerClientException(String.format(
259-
"Source file %s doesn't exist", src));
260-
}
264+
// if (!src.exists()) {
265+
// throw new DockerClientException(String.format(
266+
// "Source file %s doesn't exist", src));
267+
// }
261268
if (src.isDirectory()) {
262269
Collection<File> files = FileUtils.listFiles(src,
263-
new GoLangMatchFileFilter(src, ignores), TrueFileFilter.INSTANCE);
270+
new GoLangMatchFileFilter(src, ignores),
271+
TrueFileFilter.INSTANCE);
264272
filesToAdd.addAll(files);
265-
} else if (!GoLangFileMatch.match(ignores, CompressArchiveUtil.relativize(dockerFolder, src))){
273+
} else if (!src.exists()) {
274+
filesToAdd.addAll(resolveWildcards(src, ignores));
275+
} else if (!GoLangFileMatch.match(ignores,
276+
CompressArchiveUtil.relativize(dockerFolder,
277+
src))) {
266278
filesToAdd.add(src);
267279
} else {
268-
throw new DockerClientException(String.format(
269-
"Source file %s is excluded by .dockerignore file", src));
280+
throw new DockerClientException(
281+
String.format(
282+
"Source file %s is excluded by .dockerignore file",
283+
src));
270284
}
271285
}
272286
}
@@ -281,6 +295,27 @@ protected File buildDockerFolderTar(File dockerFolder) {
281295
}
282296
}
283297

298+
private Collection<File> resolveWildcards(File file, List<String> ignores) {
299+
List<File> filesToAdd = new ArrayList<File>();
300+
301+
File parent = file.getParentFile();
302+
if (parent != null) {
303+
if (parent.isDirectory()) {
304+
Collection<File> files = FileUtils.listFiles(parent,
305+
new GoLangMatchFileFilter(parent, ignores),
306+
TrueFileFilter.INSTANCE);
307+
filesToAdd.addAll(files);
308+
} else {
309+
filesToAdd.addAll(resolveWildcards(parent, ignores));
310+
}
311+
} else {
312+
throw new DockerClientException(String.format(
313+
"Source file %s doesn't exist", file));
314+
}
315+
316+
return filesToAdd;
317+
}
318+
284319
private String filterForEnvironmentVars(String extractedResource,
285320
Map<String, String> environmentMap) {
286321

src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,40 @@ public void testNginxDockerfileBuilder() {
7878
public void testDockerBuilderAddUrl() {
7979
File baseDir = new File(Thread.currentThread().getContextClassLoader()
8080
.getResource("testAddUrl").getFile());
81-
dockerfileBuild(baseDir, "Docker");
81+
String response = dockerfileBuild(baseDir);
82+
assertThat(response, containsString("Docker"));
8283
}
8384

8485
@Test
8586
public void testDockerBuilderAddFileInSubfolder() throws DockerException,
8687
IOException {
8788
File baseDir = new File(Thread.currentThread().getContextClassLoader()
8889
.getResource("testAddFileInSubfolder").getFile());
89-
dockerfileBuild(baseDir, "Successfully executed testrun.sh");
90+
String response = dockerfileBuild(baseDir);
91+
assertThat(response, containsString("Successfully executed testrun.sh"));
92+
}
93+
94+
@Test
95+
public void testDockerBuilderAddFilesViaWildcard() throws DockerException,
96+
IOException {
97+
File baseDir = new File(Thread.currentThread().getContextClassLoader()
98+
.getResource("testAddFilesViaWildcard").getFile());
99+
String response = dockerfileBuild(baseDir);
100+
assertThat(response, containsString("Successfully executed testinclude1.sh"));
101+
assertThat(response, not(containsString("Successfully executed testinclude2.sh")));
90102
}
91103

92104
@Test
93105
public void testDockerBuilderAddFolder() throws DockerException,
94106
IOException {
95107
File baseDir = new File(Thread.currentThread().getContextClassLoader()
96108
.getResource("testAddFolder").getFile());
97-
dockerfileBuild(baseDir, "Successfully executed testAddFolder.sh");
109+
String response = dockerfileBuild(baseDir);
110+
assertThat(response, containsString("Successfully executed testAddFolder.sh"));
98111
}
99112

100113

101-
private String dockerfileBuild(File baseDir, String expectedText) {
114+
private String dockerfileBuild(File baseDir) {
102115

103116
// Build image
104117
InputStream response = dockerClient.buildImageCmd(baseDir).withNoCache().exec();
@@ -123,9 +136,9 @@ private String dockerfileBuild(File baseDir, String expectedText) {
123136
InputStream logResponse = logContainer(container
124137
.getId());
125138

126-
assertThat(asString(logResponse), containsString(expectedText));
139+
//assertThat(asString(logResponse), containsString(expectedText));
127140

128-
return container.getId();
141+
return asString(logResponse);
129142
}
130143

131144

@@ -152,7 +165,8 @@ public void testDockerIgnore() throws DockerException,
152165
IOException {
153166
File baseDir = new File(Thread.currentThread().getContextClassLoader()
154167
.getResource("testDockerignore").getFile());
155-
dockerfileBuild(baseDir, "/tmp/a/a /tmp/a/c /tmp/a/d");
168+
String response = dockerfileBuild(baseDir);
169+
assertThat(response, containsString("/tmp/a/a /tmp/a/c /tmp/a/d"));
156170
}
157171

158172
@Test
@@ -201,7 +215,7 @@ public void testNetCatDockerfileBuilder() throws InterruptedException {
201215
public void testAddAndCopySubstitution () throws DockerException, IOException {
202216
File baseDir = new File(Thread.currentThread().getContextClassLoader()
203217
.getResource("testENVSubstitution").getFile());
204-
dockerfileBuild(baseDir, "testENVSubstitution successfully completed");
205-
218+
String response = dockerfileBuild(baseDir);
219+
assertThat(response, containsString("testENVSubstitution successfully completed"));
206220
}
207221
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM ubuntu:latest
2+
3+
# Copy testrun.sh files into the container
4+
5+
ADD ./folder*/* /tmp/
6+
7+
RUN cp /tmp/*.sh /usr/local/bin/ && chmod +x /usr/local/bin/*.sh
8+
9+
CMD ["testrun.sh"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
/usr/local/bin/testinclude1.sh
4+
/usr/local/bin/testinclude2.sh
5+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
echo "Successfully executed testinclude1.sh"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
echo "Successfully executed testinclude2.sh"

0 commit comments

Comments
 (0)