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
33 changes: 26 additions & 7 deletions src/main/java/com/github/dockerjava/api/command/BuildImageCmd.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.github.dockerjava.api.command;

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

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
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;
import java.util.Set;

/**
* Build an image from Dockerfile.
Expand All @@ -34,7 +34,11 @@ public interface BuildImageCmd extends AsyncDockerCmd<BuildImageCmd, BuildRespon

/**
* "t" in API
*
* @deprecated since docker API version 1.21 there can be multiple tags
* specified so use {@link #getTags()}
*/
@Deprecated
@CheckForNull
String getTag();

Expand Down Expand Up @@ -107,10 +111,25 @@ public interface BuildImageCmd extends AsyncDockerCmd<BuildImageCmd, BuildRespon
@CheckForNull
Map<String, String> getLabels();

/**
* @since {@link RemoteApiVersion#VERSION_1_21}
*/
@CheckForNull
Set<String> getTags();

// setters

/**
* @deprecated since docker API version 1.21 there can be multiple tags
* specified so use {@link #withTags(Set<String>)}
* @param tag
* @return
*/
@Deprecated
BuildImageCmd withTag(String tag);

BuildImageCmd withTags(Set<String> tags);

BuildImageCmd withRemote(URI remote);

BuildImageCmd withBaseDirectory(File baseDirectory);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package com.github.dockerjava.core.command;

import static com.google.common.base.Preconditions.checkNotNull;
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.BuildResponseItem;
import com.github.dockerjava.core.dockerfile.Dockerfile;
import com.github.dockerjava.core.util.FilePathUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.BuildResponseItem;
import com.github.dockerjava.core.dockerfile.Dockerfile;
import com.github.dockerjava.core.util.FilePathUtil;
import static com.google.common.base.Preconditions.checkNotNull;

/**
*
* Build an image from Dockerfile.
*
*/
public class BuildImageCmdImpl extends AbstrAsyncDockerCmd<BuildImageCmd, BuildResponseItem> implements BuildImageCmd {

private InputStream tarInputStream;

private String tag;

private Set<String> tags;

private Boolean noCache;

private Boolean remove = true;
Expand Down Expand Up @@ -156,6 +157,11 @@ public Map<String, String> getLabels() {
return labels;
}

@Override
public Set<String> getTags() {
return tags;
}

// getter lib specific

@Override
Expand Down Expand Up @@ -185,6 +191,12 @@ public BuildImageCmdImpl withTag(String tag) {
return this;
}

@Override
public BuildImageCmd withTags(Set<String> tags) {
this.tags = tags;
return this;
}

@Override
public BuildImageCmd withRemote(URI remote) {
this.remote = remote;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ protected AbstractCallbackNotifier<BuildResponseItem> callbackNotifier(BuildImag
if (dockerFilePath != null && command.getRemote() == null && !"Dockerfile".equals(dockerFilePath)) {
webTarget = webTarget.queryParam("dockerfile", dockerFilePath);
}
if (command.getTag() != null) {
if (command.getTags() != null && !command.getTags().isEmpty()) {
for (String t : command.getTags()) {
webTarget = webTarget.queryParam("t", t);
}
} else if (command.getTag() != null) {
webTarget = webTarget.queryParam("t", command.getTag());
}

if (command.getRemote() != null) {
webTarget = webTarget.queryParam("remote", command.getRemote().toString());
}
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/github/dockerjava/netty/WebTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public class WebTarget {

private final ImmutableList<String> path;

private final ImmutableMap<String, String> queryParams;
private final ImmutableMap<String, Object> queryParams;

private static final String PATH_SEPARATOR = "/";

public WebTarget(ChannelProvider channelProvider) {
this(channelProvider, ImmutableList.<String>of(), ImmutableMap.<String, String>of());
this(channelProvider, ImmutableList.<String>of(), ImmutableMap.<String, Object>of());
}

private WebTarget(ChannelProvider channelProvider,
ImmutableList<String> path,
ImmutableMap<String, String> queryParams) {
ImmutableMap<String, Object> queryParams) {
this.channelProvider = channelProvider;
this.path = path;
this.queryParams = queryParams;
Expand All @@ -52,8 +52,14 @@ public InvocationBuilder request() {
String resource = PATH_SEPARATOR + StringUtils.join(path, PATH_SEPARATOR);

List<String> params = new ArrayList<>();
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
params.add(entry.getKey() + "=" + entry.getValue());
for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
if (entry.getValue() instanceof Iterable) {
for (Object i : (Iterable) entry.getValue()) {
params.add(entry.getKey() + "=" + i.toString());
}
} else {
params.add(entry.getKey() + "=" + entry.getValue().toString());
}
}

if (!params.isEmpty()) {
Expand All @@ -73,9 +79,9 @@ public WebTarget resolveTemplate(String name, Object value) {
}

public WebTarget queryParam(String name, Object value) {
ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String>builder().putAll(queryParams);
ImmutableMap.Builder<String, Object> builder = ImmutableMap.<String, Object>builder().putAll(queryParams);
if (value != null) {
builder.put(name, value.toString());
builder.put(name, value);
}
return new WebTarget(channelProvider, path, builder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ protected Void execute0(BuildImageCmd command, ResultCallback<BuildResponseItem>
if (dockerFilePath != null && command.getRemote() == null && !"Dockerfile".equals(dockerFilePath)) {
webTarget = webTarget.queryParam("dockerfile", dockerFilePath);
}
if (command.getTag() != null) {
if (command.getTags() != null && !command.getTags().isEmpty()) {
webTarget = webTarget.queryParam("t", command.getTags());
} else if (command.getTag() != null) {
webTarget = webTarget.queryParam("t", command.getTag());
}
if (command.getRemote() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.containsInAnyOrder;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -274,6 +277,28 @@ public void labels() throws Exception {
assertThat(inspectImageResponse.getConfig().getLabels().get("test"), equalTo("abc"));
}

@Test
public void multipleTags() throws Exception {
if (!getVersion(dockerClient).isGreaterOrEqual(RemoteApiVersion.VERSION_1_21)) {
throw new SkipException("API version should be >= 1.21");
}

File baseDir = fileFromBuildTestResource("labels");

String imageId = dockerClient.buildImageCmd(baseDir).withNoCache(true)
.withTag("fallback-when-withTags-not-called")
.withTags(new HashSet<>(Arrays.asList("docker-java-test:tag1", "docker-java-test:tag2")))
.exec(new BuildImageResultCallback())
.awaitImageId();

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

assertThat(inspectImageResponse.getRepoTags().size(), equalTo(2));
assertThat(inspectImageResponse.getRepoTags(), containsInAnyOrder("docker-java-test:tag1", "docker-java-test:tag2"));
}

public void dockerfileNotInBaseDirectory() throws Exception {
File baseDirectory = fileFromBuildTestResource("dockerfileNotInBaseDirectory");
File dockerfile = fileFromBuildTestResource("dockerfileNotInBaseDirectory/dockerfileFolder/Dockerfile");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.containsInAnyOrder;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -279,6 +282,28 @@ public void labels() throws Exception {
assertThat(inspectImageResponse.getConfig().getLabels().get("test"), equalTo("abc"));
}

@Test
public void multipleTags() throws Exception {
if (!getVersion(dockerClient).isGreaterOrEqual(RemoteApiVersion.VERSION_1_21)) {
throw new SkipException("API version should be >= 1.21");
}

File baseDir = fileFromBuildTestResource("labels");

String imageId = dockerClient.buildImageCmd(baseDir).withNoCache(true)
.withTag("fallback-when-withTags-not-called")
.withTags(new HashSet<>(Arrays.asList("docker-java-test:tag1", "docker-java-test:tag2")))
.exec(new BuildImageResultCallback())
.awaitImageId();

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

assertThat(inspectImageResponse.getRepoTags().size(), equalTo(2));
assertThat(inspectImageResponse.getRepoTags(), containsInAnyOrder("docker-java-test:tag1", "docker-java-test:tag2"));
}

public void dockerfileNotInBaseDirectory() throws Exception {
File baseDirectory = fileFromBuildTestResource("dockerfileNotInBaseDirectory");
File dockerfile = fileFromBuildTestResource("dockerfileNotInBaseDirectory/dockerfileFolder/Dockerfile");
Expand Down