X Tutup
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
13 changes: 13 additions & 0 deletions src/main/java/com/github/dockerjava/api/command/BuildImageCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.BuildResponseItem;
import com.github.dockerjava.core.RemoteApiVersion;

/**
* Build an image from Dockerfile.
Expand Down Expand Up @@ -87,6 +89,12 @@ public interface BuildImageCmd extends AsyncDockerCmd<BuildImageCmd, BuildRespon
@CheckForNull
public String getCpusetcpus();

/**
* @since {@link RemoteApiVersion#VERSION_1_21}
*/
@CheckForNull
public Map<String, String> getBuildArgs();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since what API version?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like 1.21

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. 1.21

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add javadoc? Example:

/**
* See {@link #oomKilled}
*/
@CheckForNull

and reference to field
/**
* @since {@link RemoteApiVersion#VERSION_1_17}
*/
@CheckForNull


// setters

public BuildImageCmd withTag(String tag);
Expand Down Expand Up @@ -115,6 +123,11 @@ public interface BuildImageCmd extends AsyncDockerCmd<BuildImageCmd, BuildRespon

public BuildImageCmd withCpusetcpus(String cpusetcpus);

/**
* @since {@link RemoteApiVersion#VERSION_1_21}
*/
public BuildImageCmd withBuildArg(String key, String value);

// setters lib specific

public BuildImageCmd withBuildAuthConfigs(AuthConfigurations authConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.model.AuthConfigurations;
Expand Down Expand Up @@ -50,6 +52,8 @@ public class BuildImageCmdImpl extends AbstrAsyncDockerCmd<BuildImageCmd, BuildR

private URI remote;

private Map<String, String> buildArgs = new HashMap<String, String>();

public BuildImageCmdImpl(BuildImageCmd.Exec exec) {
super(exec);
}
Expand Down Expand Up @@ -138,6 +142,11 @@ public String getCpusetcpus() {
return cpusetcpus;
}

@Override
public Map<String, String> getBuildArgs() {
return buildArgs;
}

// getter lib specific

@Override
Expand Down Expand Up @@ -219,6 +228,12 @@ public BuildImageCmd withCpusetcpus(String cpusetcpus) {
return this;
}

@Override
public BuildImageCmd withBuildArg(String key, String value) {
this.buildArgs.put(key, value);
return this;
}

// lib specific

@Override
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/github/dockerjava/jaxrs/BuildImageCmdExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.slf4j.Logger;
Expand All @@ -20,9 +21,12 @@
import com.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
import com.github.dockerjava.jaxrs.async.POSTCallbackNotifier;

import java.io.IOException;

public class BuildImageCmdExec extends AbstrAsyncDockerCmdExec<BuildImageCmd, BuildResponseItem> implements
BuildImageCmd.Exec {
private static final Logger LOGGER = LoggerFactory.getLogger(BuildImageCmdExec.class);
private static final ObjectMapper MAPPER = new ObjectMapper();

public BuildImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
super(baseResource, dockerClientConfig);
Expand Down Expand Up @@ -88,6 +92,18 @@ protected AbstractCallbackNotifier<BuildResponseItem> callbackNotifier(BuildImag
webTarget = webTarget.queryParam("cpusetcpus", command.getCpusetcpus());
}

if (command.hasRemoveEnabled() == null || !command.hasRemoveEnabled()) {
webTarget = webTarget.queryParam("rm", "false");
}

if (command.getBuildArgs() != null) {
try {
webTarget = webTarget.queryParam("buildargs", MAPPER.writeValueAsString(command.getBuildArgs()));
} catch (IOException e) {
// pass
}
}

webTarget.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.CHUNKED);
webTarget.property(ClientProperties.CHUNKED_ENCODING_SIZE, 1024 * 1024);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.dockerjava.netty.exec;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -13,9 +14,12 @@
import com.github.dockerjava.netty.MediaType;
import com.github.dockerjava.netty.WebTarget;

import java.io.IOException;

public class BuildImageCmdExec extends AbstrAsyncDockerCmdExec<BuildImageCmd, BuildResponseItem> implements
BuildImageCmd.Exec {
private static final Logger LOGGER = LoggerFactory.getLogger(BuildImageCmdExec.class);
private static final ObjectMapper MAPPER = new ObjectMapper();

public BuildImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
super(baseResource, dockerClientConfig);
Expand Down Expand Up @@ -80,6 +84,14 @@ protected Void execute0(BuildImageCmd command, ResultCallback<BuildResponseItem>
webTarget = webTarget.queryParam("cpusetcpus", command.getCpusetcpus());
}

if (command.getBuildArgs() != null) {
try {
webTarget = webTarget.queryParam("buildargs", MAPPER.writeValueAsString(command.getBuildArgs()));
} catch (IOException e) {
// pass
}
}

LOGGER.trace("POST: {}", webTarget);

InvocationBuilder builder = resourceWithOptionalAuthConfig(command, webTarget.request()).accept(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,18 @@ public void testBuildFromPrivateRegistry() throws Exception {
LOG.info("Image Inspect: {}", inspectImageResponse.toString());

}

@Test
public void testBuildArgs() throws Exception {
File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("testBuildArgs").getFile());

String imageId = dockerClient.buildImageCmd(baseDir).withNoCache(true).withBuildArg("testArg", "abc").exec(new BuildImageResultCallback())
.awaitImageId();

InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
assertThat(inspectImageResponse, not(nullValue()));
LOG.info("Image Inspect: {}", inspectImageResponse.toString());

assertThat(inspectImageResponse.getConfig().getLabels().get("test"), equalTo("abc"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,18 @@ public void testBuildFromPrivateRegistry() throws Exception {
LOG.info("Image Inspect: {}", inspectImageResponse.toString());

}

@Test
public void testBuildArgs() throws Exception {
File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("testBuildArgs").getFile());

String imageId = dockerClient.buildImageCmd(baseDir).withNoCache(true).withBuildArg("testArg", "abc").exec(new BuildImageResultCallback())
.awaitImageId();

InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
assertThat(inspectImageResponse, not(nullValue()));
LOG.info("Image Inspect: {}", inspectImageResponse.toString());

assertThat(inspectImageResponse.getConfig().getLabels().get("test"), equalTo("abc"));
}
}
5 changes: 5 additions & 0 deletions src/test/resources/testBuildArgs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM ubuntu:latest

ARG testArg

LABEL "test"=$testArg
X Tutup