Skip to content
Closed
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 @@ -286,7 +286,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
28 changes: 16 additions & 12 deletions src/main/java/com/github/dockerjava/core/dockerfile/Dockerfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,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 @@ -45,6 +46,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 @@ -78,7 +88,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 @@ -104,10 +114,6 @@ public ScannedResult parse() throws IOException {
return new ScannedResult();
}

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

/**
* Result of scanning / parsing a docker file.
*/
Expand All @@ -120,7 +126,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 @@ -215,7 +221,7 @@ private List<String> matchingIgnorePatterns(String fileName) {
* like "!Dockerfile" 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 Down Expand Up @@ -245,14 +251,12 @@ private void processAddStatement(DockerfileStatement.Add add) throws IOException

for (String resource : add.getFileResources()) {

File dockerFolder = getDockerFolder();

File src = new File(resource);
if (!src.isAbsolute()) {
src = new File(dockerFolder, resource);
src = new File(baseDirectory, resource);
} else {
throw new DockerClientException(String.format("Source file %s must be relative to %s", src,
dockerFolder));
baseDirectory));
}

if (src.exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.github.dockerjava.core.command;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import com.github.dockerjava.api.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.model.*;
import com.github.dockerjava.client.AbstractDockerClientTest;
import com.github.dockerjava.core.CompressArchiveUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.testng.ITestResult;
import org.testng.annotations.*;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -15,27 +20,8 @@
import java.util.Collection;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.github.dockerjava.api.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.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.client.AbstractDockerClientTest;
import com.github.dockerjava.core.CompressArchiveUtil;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

@Test(groups = "integration")
public class BuildImageCmdImplTest extends AbstractDockerClientTest {
Expand Down Expand Up @@ -290,4 +276,21 @@ public void testBuildFromPrivateRegistry() throws Exception {
LOG.info("Image Inspect: {}", inspectImageResponse.toString());

}

@Test
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"