Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public BuildImageCmdImpl withDockerfile(File dockerfile) {
this.dockerFile = dockerfile;

try {
withTarInputStream(new Dockerfile(dockerfile).parse().buildDockerFolderTar(baseDirectory));
withTarInputStream(new Dockerfile(dockerfile, baseDirectory).parse().buildDockerFolderTar());
} catch (IOException e) {
// we just created the file this should never happen.
throw new RuntimeException(e);
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/com/github/dockerjava/core/dockerfile/Dockerfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class Dockerfile {

public final File dockerFile;

public Dockerfile(File dockerFile) {
private final File baseDirectory;

public Dockerfile(File dockerFile, File baseDirectory) {
if (!dockerFile.exists()) {
throw new IllegalStateException(String.format("Dockerfile %s does not exist", dockerFile.getAbsolutePath()));
}
Expand All @@ -43,6 +44,15 @@ public Dockerfile(File dockerFile) {

this.dockerFile = dockerFile;

if (!baseDirectory.exists()) {
throw new IllegalStateException(String.format("Base directory %s does not exist", baseDirectory.getAbsolutePath()));
}

if (!baseDirectory.isDirectory()) {
throw new IllegalStateException(String.format("Base directory %s is not a directory", baseDirectory.getAbsolutePath()));
}

this.baseDirectory = baseDirectory;
}

private static class LineTransformer implements Function<String, Optional<? extends DockerfileStatement>> {
Expand Down Expand Up @@ -76,7 +86,7 @@ public Iterable<DockerfileStatement> getStatements() throws IOException {

public List<String> getIgnores() throws IOException {
List<String> ignores = new ArrayList<String>();
File dockerIgnoreFile = new File(getDockerFolder(), ".dockerignore");
File dockerIgnoreFile = new File(baseDirectory, ".dockerignore");
if (dockerIgnoreFile.exists()) {
int lineNumber = 0;
List<String> dockerIgnoreFileContent = FileUtils.readLines(dockerIgnoreFile);
Expand All @@ -102,10 +112,6 @@ public ScannedResult parse() throws IOException {
return new ScannedResult();
}

public File getDockerFolder() {
return dockerFile.getParentFile();
}

/**
* Result of scanning / parsing a docker file.
*/
Expand All @@ -116,7 +122,7 @@ public class ScannedResult {
final List<File> filesToAdd = new ArrayList<File>();

public InputStream buildDockerFolderTar() {
return buildDockerFolderTar(getDockerFolder());
return buildDockerFolderTar(baseDirectory);
}

public InputStream buildDockerFolderTar(File directory) {
Expand Down Expand Up @@ -180,7 +186,7 @@ public ScannedResult() throws IOException {
"Dockerfile is excluded by pattern '%s' in .dockerignore file", matchingIgnorePattern));
}

Collection<File> filesInBuildContext = FileUtils.listFiles(getDockerFolder(), TrueFileFilter.INSTANCE,
Collection<File> filesInBuildContext = FileUtils.listFiles(baseDirectory, TrueFileFilter.INSTANCE,
TrueFileFilter.INSTANCE);

for (File f : filesInBuildContext) {
Expand Down Expand Up @@ -217,7 +223,7 @@ private List<String> matchingIgnorePatterns(String fileName) {
* will be respected.
*/
private String effectiveMatchingIgnorePattern(File file) {
String relativeFilename = FilePathUtil.relativize(getDockerFolder(), file);
String relativeFilename = FilePathUtil.relativize(baseDirectory, file);

List<String> matchingPattern = matchingIgnorePatterns(relativeFilename);

Expand All @@ -242,6 +248,6 @@ private String effectiveMatchingIgnorePattern(File file) {
}

return lastMatchingPattern;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.InspectImageResponse;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.api.model.Ports.Binding;
import com.github.dockerjava.client.AbstractDockerClientTest;
import com.github.dockerjava.core.util.CompressArchiveUtil;
Expand Down Expand Up @@ -319,4 +318,20 @@ public void testBuildArgs() throws Exception {

assertThat(inspectImageResponse.getConfig().getLabels().get("test"), equalTo("abc"));
}

public void testDockerfileNotInBaseDirectory() throws Exception {
File baseDirectory = getResource("testDockerfileNotInBaseDirectory");
File dockerfile = getResource("testDockerfileNotInBaseDirectory/dockerfileFolder/Dockerfile");
BuildImageCmd command = dockerClient.buildImageCmd()
.withBaseDirectory(baseDirectory)
.withDockerfile(dockerfile);

String response = execBuild(command);

assertThat(response, containsString("Successfully executed testrun.sh"));
}

private File getResource(String path) {
return new File(Thread.currentThread().getContextClassLoader().getResource(path).getFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public String apply(File file) {
public void testAddMultipleFiles() throws IOException {
File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("testAddMultipleFiles")
.getFile());
Dockerfile dockerfile = new Dockerfile(new File(baseDir, "Dockerfile"));
Dockerfile dockerfile = new Dockerfile(new File(baseDir, "Dockerfile"), baseDir);
Dockerfile.ScannedResult result = dockerfile.parse();
Collection<String> filesToAdd = transform(result.filesToAdd, TO_FILE_NAMES);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testAllItems() throws IOException {

for (File child : root.listFiles()) {
if (new File(child, "Dockerfile").exists()) {
Dockerfile dockerfile = new Dockerfile(new File(child, "Dockerfile"));
Dockerfile dockerfile = new Dockerfile(new File(child, "Dockerfile"), baseDir);
dockerfiles.put(child.getName(), dockerfile);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ubuntu:latest

ADD testrunFolder/testrun.sh /tmp/

RUN cp /tmp/testrun.sh /usr/local/bin/ && chmod +x /usr/local/bin/testrun.sh

CMD ["testrun.sh"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

echo "Successfully executed testrun.sh"