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
4 changes: 3 additions & 1 deletion src/main/java/com/github/dockerjava/api/DockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public CopyFileFromContainerCmd copyFileFromContainerCmd(

public CommitCmd commitCmd(String containerId);

public BuildImageCmd buildImageCmd(File dockerFolder);
public BuildImageCmd buildImageCmd();

public BuildImageCmd buildImageCmd(File dockerFileOrFolder);

public BuildImageCmd buildImageCmd(InputStream tarInputStream);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public interface BuildImageCmd extends DockerCmd<BuildImageCmd.Response>{

public boolean isQuiet();

public String getPathToDockerfile();

public AuthConfigurations getBuildAuthConfigs();


public BuildImageCmd withBaseDirectory(File baseDirectory);

public BuildImageCmd withDockerfile(File dockerfile);

public BuildImageCmd withTarInputStream(InputStream tarInputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ public CommitCmd commitCmd(String containerId) {
.createCommitCmdExec(), containerId);
}

@Override
public BuildImageCmd buildImageCmd() {
return new BuildImageCmdImpl(getDockerCmdExecFactory()
.createBuildImageCmdExec());
}

@Override
public BuildImageCmd buildImageCmd(File dockerFileOrFolder) {
return new BuildImageCmdImpl(getDockerCmdExecFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.core.dockerfile.Dockerfile;
import com.google.common.base.Optional;

/**
*
Expand All @@ -24,6 +25,8 @@ public class BuildImageCmdImpl extends AbstrDockerCmd<BuildImageCmd, BuildImageC
private boolean remove = true;
private boolean quiet;
private AuthConfigurations buildAuthConfigs;
private File dockerFile;
private File baseDirectory;

public BuildImageCmdImpl(BuildImageCmd.Exec exec) {
super(exec);
Expand All @@ -33,10 +36,13 @@ public BuildImageCmdImpl(BuildImageCmd.Exec exec, File dockerFileOrFolder) {
super(exec);
checkNotNull(dockerFileOrFolder, "dockerFolder is null");

if( dockerFileOrFolder.isDirectory() )
withDockerfile( new File(dockerFileOrFolder, "Dockerfile"));
else
withDockerfile( dockerFileOrFolder);
if( dockerFileOrFolder.isDirectory() ) {
withBaseDirectory(dockerFileOrFolder);
withDockerfile(new File(dockerFileOrFolder, "Dockerfile"));
}
else {
withDockerfile(dockerFileOrFolder);
}
}

public BuildImageCmdImpl(BuildImageCmd.Exec exec, InputStream tarInputStream) {
Expand All @@ -55,12 +61,20 @@ public BuildImageCmdImpl withDockerfile(File dockerfile) {
checkNotNull(dockerfile);
if( !dockerfile.exists() )
throw new IllegalArgumentException("Dockerfile does not exist");
if( !dockerfile.isFile() )
throw new IllegalArgumentException("Not a directory");

if( baseDirectory == null )
withBaseDirectory(dockerfile.getParentFile());


this.dockerFile = dockerfile;

try {
withTarInputStream(
new Dockerfile(dockerfile)
.parse()
.buildDockerFolderTar() );
.buildDockerFolderTar(baseDirectory) );
} catch (IOException e) {
// we just created the file this should never happen.
throw new RuntimeException(e);
Expand Down Expand Up @@ -102,12 +116,24 @@ public boolean isQuiet() {
return quiet;
}

@Override
@Override
public String getPathToDockerfile() {
int baseLen = baseDirectory.getAbsolutePath().length();
return dockerFile.getAbsolutePath().substring(baseLen+1);
}

@Override
public AuthConfigurations getBuildAuthConfigs() {
return buildAuthConfigs;
}

@Override
@Override
public BuildImageCmd withBaseDirectory(File baseDirectory) {
this.baseDirectory = baseDirectory;
return this;
}

@Override
public BuildImageCmdImpl withNoCache() {
return withNoCache(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,18 @@ public class ScannedResult {
final List<File> filesToAdd = new ArrayList<File>();

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

public InputStream buildDockerFolderTar(File directory) {

// ARCHIVE TAR
File dockerFolderTar = null;

try {
String archiveNameWithOutExtension = UUID.randomUUID().toString();

dockerFolderTar = CompressArchiveUtil.archiveTARFiles(getDockerFolder(),
dockerFolderTar = CompressArchiveUtil.archiveTARFiles(directory,
filesToAdd,
archiveNameWithOutExtension);
return FileUtils.openInputStream(dockerFolderTar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public BuildImageCmdExec(WebTarget baseResource) {
@Override
protected ResponseImpl execute(BuildImageCmd command) {
WebTarget webResource = getBaseResource().path("/build");
String dockerFilePath = command.getPathToDockerfile();

if(command.getTag() != null) {
webResource = webResource.queryParam("t", command.getTag());
Expand All @@ -48,6 +49,9 @@ protected ResponseImpl execute(BuildImageCmd command) {
if (command.isQuiet()) {
webResource = webResource.queryParam("q", "true");
}
if( dockerFilePath != null && !"Dockerfile".equals(dockerFilePath)) {
webResource = webResource.queryParam("dockerfile", dockerFilePath);
}


webResource.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.CHUNKED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ public void testNginxDockerfileBuilder() {
equalTo("Guillaume J. Charmes \"[email protected]\""));
}

@Test
public void testNonstandard1() {
File baseDir = new File(Thread.currentThread().getContextClassLoader()
.getResource("nonstandard/subdirectory/Dockerfile-nonstandard").getFile());

InputStream response = dockerClient.buildImageCmd(baseDir).withNoCache().exec();

String fullLog = asString(response);
assertThat(fullLog, containsString("Successfully built"));
}

@Test
public void testNonstandard2() {
File baseDir = new File(Thread.currentThread().getContextClassLoader()
.getResource("nonstandard").getFile());
File dockerFile = new File(Thread.currentThread().getContextClassLoader()
.getResource("nonstandard/subdirectory/Dockerfile-nonstandard").getFile());


InputStream response = dockerClient.buildImageCmd()
.withBaseDirectory(baseDir)
.withDockerfile(dockerFile)
.withNoCache().exec();

String fullLog = asString(response);
assertThat(fullLog, containsString("Successfully built"));
}

@Test
public void testDockerBuilderAddUrl() {
File baseDir = new File(Thread.currentThread().getContextClassLoader()
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/nonstandard/subdirectory/Dockerfile-nonstandard
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Nginx
#
# VERSION 0.0.1

FROM ubuntu:latest
MAINTAINER Guillaume J. Charmes "[email protected]"

# make sure the package repository is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update

RUN apt-get install -y inotify-tools nginx apache2 openssh-server