Skip to content

Commit 842e816

Browse files
rchodavaKostyaSha
authored andcommitted
Allow an explicit Dockerfile location string to be specified to the build command (docker-java#825)
* When supplying a TAR stream to the build command, API consumers just want to pass a String location of the Dockerfile within the TAR stream they are supplying * Add netty test for building with an explicitly specified Dockerfile location within a tar
1 parent dc87e0c commit 842e816

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/main/java/com/github/dockerjava/api/command/BuildImageCmd.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public interface BuildImageCmd extends AsyncDockerCmd<BuildImageCmd, BuildRespon
117117

118118
BuildImageCmd withDockerfile(File dockerfile);
119119

120+
BuildImageCmd withDockerfilePath(String dockerfilePath);
121+
120122
BuildImageCmd withNoCache(Boolean noCache);
121123

122124
BuildImageCmd withRemove(Boolean rm);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class BuildImageCmdImpl extends AbstrAsyncDockerCmd<BuildImageCmd, BuildR
3838

3939
private File dockerFile;
4040

41+
private String dockerFilePath;
42+
4143
private File baseDirectory;
4244

4345
private String cpusetcpus;
@@ -119,7 +121,9 @@ public Boolean hasPullEnabled() {
119121

120122
@Override
121123
public String getPathToDockerfile() {
122-
if (baseDirectory != null && dockerFile != null) {
124+
if (dockerFilePath != null) {
125+
return dockerFilePath;
126+
} else if (baseDirectory != null && dockerFile != null) {
123127
return FilePathUtil.relativize(baseDirectory, dockerFile);
124128
} else {
125129
return null;
@@ -287,6 +291,13 @@ public BuildImageCmdImpl withDockerfile(File dockerfile) {
287291
return this;
288292
}
289293

294+
@Override
295+
public BuildImageCmd withDockerfilePath(String dockerfilePath) {
296+
checkNotNull(dockerfilePath, "dockerfilePath is null");
297+
this.dockerFilePath = dockerfilePath;
298+
return this;
299+
}
300+
290301
@Override
291302
public BuildImageCmdImpl withTarInputStream(InputStream tarInputStream) {
292303
checkNotNull(tarInputStream, "tarInputStream is null");

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ public void buildImageFromTar() throws Exception {
8383
assertThat(response, containsString("Successfully executed testrun.sh"));
8484
}
8585

86+
@Test
87+
public void buildImageFromTarWithDockerfileNotInBaseDirectory() throws Exception {
88+
File baseDir = fileFromBuildTestResource("dockerfileNotInBaseDirectory");
89+
Collection<File> files = FileUtils.listFiles(baseDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
90+
File tarFile = CompressArchiveUtil.archiveTARFiles(baseDir, files, UUID.randomUUID().toString());
91+
String response = dockerfileBuild(new FileInputStream(tarFile), "dockerfileFolder/Dockerfile");
92+
assertThat(response, containsString("Successfully executed testrun.sh"));
93+
}
94+
8695
@Test
8796
public void onBuild() throws Exception {
8897
File baseDir = fileFromBuildTestResource("ONBUILD/parent");
@@ -123,6 +132,11 @@ public void addFolder() throws Exception {
123132
assertThat(response, containsString("Successfully executed testAddFolder.sh"));
124133
}
125134

135+
private String dockerfileBuild(InputStream tarInputStream, String dockerFilePath) throws Exception {
136+
137+
return execBuild(dockerClient.buildImageCmd().withTarInputStream(tarInputStream).withDockerfilePath(dockerFilePath));
138+
}
139+
126140
private String dockerfileBuild(InputStream tarInputStream) throws Exception {
127141

128142
return execBuild(dockerClient.buildImageCmd().withTarInputStream(tarInputStream));

src/test/java/com/github/dockerjava/netty/exec/BuildImageCmdExecTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ public void buildImageFromTar() throws Exception {
8888
assertThat(response, containsString("Successfully executed testrun.sh"));
8989
}
9090

91+
@Test
92+
public void buildImageFromTarWithDockerfileNotInBaseDirectory() throws Exception {
93+
File baseDir = fileFromBuildTestResource("dockerfileNotInBaseDirectory");
94+
Collection<File> files = FileUtils.listFiles(baseDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
95+
File tarFile = CompressArchiveUtil.archiveTARFiles(baseDir, files, UUID.randomUUID().toString());
96+
String response = dockerfileBuild(new FileInputStream(tarFile), "dockerfileFolder/Dockerfile");
97+
assertThat(response, containsString("Successfully executed testrun.sh"));
98+
}
99+
91100
@Test
92101
public void onBuild() throws Exception {
93102
File baseDir = fileFromBuildTestResource("ONBUILD/parent");
@@ -128,6 +137,11 @@ public void addFolder() throws Exception {
128137
assertThat(response, containsString("Successfully executed testAddFolder.sh"));
129138
}
130139

140+
private String dockerfileBuild(InputStream tarInputStream, String dockerFilePath) throws Exception {
141+
142+
return execBuild(dockerClient.buildImageCmd().withTarInputStream(tarInputStream).withDockerfilePath(dockerFilePath));
143+
}
144+
131145
private String dockerfileBuild(InputStream tarInputStream) throws Exception {
132146

133147
return execBuild(dockerClient.buildImageCmd().withTarInputStream(tarInputStream));

0 commit comments

Comments
 (0)