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
@@ -1,5 +1,7 @@
package com.github.dockerjava.api.command;

import java.util.Map;

import com.github.dockerjava.api.ConflictException;
import com.github.dockerjava.api.NotFoundException;
import com.github.dockerjava.api.model.Bind;
Expand Down Expand Up @@ -173,6 +175,8 @@ public static interface Exec extends DockerCmdExec<CreateContainerCmd, CreateCon

public CreateContainerCmd withImage(String image);

public CreateContainerCmd withLabels(Map<String, String> labels);

/**
* Add link to another container.
*/
Expand Down Expand Up @@ -238,4 +242,9 @@ public static interface Exec extends DockerCmdExec<CreateContainerCmd, CreateCon

public CreateContainerCmd withMacAddress(String macAddress);

/**
* @return
*/
Map<String, String> getLabels();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Filters;

/**
* List containers
Expand Down Expand Up @@ -31,6 +32,8 @@ public interface ListContainersCmd extends DockerCmd<List<Container>> {

public String getBeforeId();

public Filters getFilters();

public ListContainersCmd withShowAll(boolean showAll);

public ListContainersCmd withShowSize(boolean showSize);
Expand All @@ -41,6 +44,8 @@ public interface ListContainersCmd extends DockerCmd<List<Container>> {

public ListContainersCmd withBefore(String before);

public ListContainersCmd withFilters(Filters filters);

public static interface Exec extends DockerCmdExec<ListContainersCmd, List<Container>> {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class ContainerConfig {
@JsonProperty("Image")
private String image;

@JsonProperty("Labels")
private Map<String, String> labels;

@JsonProperty("MacAddress")
private String macAddress;

Expand Down Expand Up @@ -184,6 +187,10 @@ public String[] getOnBuild() {
return onBuild;
}

public Map<String, String> getLabels() {
return labels;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/Filters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.github.dockerjava.api.model;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;

/**
* Representation of Docker filters.
*
* @author Carlos Sanchez <[email protected]>
*
*/
public class Filters {

private static ObjectMapper OBJECT_MAPPER = new JacksonJaxbJsonProvider().locateMapper(Map.class,
MediaType.APPLICATION_JSON_TYPE);

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

public Filters() {
}

/**
* Constructor.
*
* @param image
* image to filter
* @param container
* container to filter
*/
public Filters(String image, String container) {
withImage(image);
withContainer(container);
}

public Filters withFilter(String key, String... value) {
filters.put(key, Arrays.asList(value));
return this;
}

public List<String> getFilter(String key) {
return filters.get(key);
}

public Filters withImage(String... image) {
filters.put("image", Arrays.asList(image));
return this;
}

public List<String> getImage() {
return getFilter("image");
}

public Filters withContainer(String... container) {
filters.put("container", Arrays.asList(container));
return this;
}

public List<String> getContainer() {
return getFilter("container");
}

/**
* Filter by labels
*
* @param labels
* string array in the form ["key"] or ["key=value"] or a mix of both
* @return
*/
public Filters withLabel(String... labels) {
filters.put("label", Arrays.asList(labels));
return this;
}

@Override
public String toString() {
try {
return OBJECT_MAPPER.writeValueAsString(filters);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -107,6 +109,9 @@ public class CreateContainerCmdImpl extends AbstrDockerCmd<CreateContainerCmd, C
@JsonProperty("HostConfig")
private HostConfig hostConfig = new HostConfig();

@JsonProperty("Labels")
private Map<String, String> labels;

public CreateContainerCmdImpl(CreateContainerCmd.Exec exec, String image) {
super(exec);
checkNotNull(image, "image was not specified");
Expand Down Expand Up @@ -221,6 +226,12 @@ public Link[] getLinks() {
return hostConfig.getLinks();
}

@Override
@JsonIgnore
public Map<String, String> getLabels() {
return labels;
}

@Override
@JsonIgnore
public LxcConf[] getLxcConf() {
Expand Down Expand Up @@ -471,6 +482,13 @@ public CreateContainerCmdImpl withImage(String image) {
return this;
}

@Override
public CreateContainerCmdImpl withLabels(Map<String, String> labels) {
checkNotNull(labels, "labels was not specified");
this.labels = labels;
return this;
}

@Override
public CreateContainerCmdImpl withLinks(Link... links) {
checkNotNull(links, "links was not specified");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.github.dockerjava.core.command;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.*;

import java.util.List;

import com.github.dockerjava.api.command.ListContainersCmd;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Filters;

/**
* List containers
Expand All @@ -32,6 +32,8 @@ public class ListContainersCmdImpl extends AbstrDockerCmd<ListContainersCmd, Lis

private String sinceId, beforeId;

private Filters filters;

public ListContainersCmdImpl(ListContainersCmd.Exec exec) {
super(exec);
}
Expand Down Expand Up @@ -61,6 +63,11 @@ public String getBeforeId() {
return beforeId;
}

@Override
public Filters getFilters() {
return filters;
}

@Override
public ListContainersCmd withShowAll(boolean showAll) {
this.showAll = showAll;
Expand Down Expand Up @@ -94,11 +101,18 @@ public ListContainersCmd withBefore(String before) {
return this;
}

@Override
public ListContainersCmd withFilters(Filters filters) {
checkNotNull(filters, "filters was not specified");
this.filters = filters;
return this;
}

@Override
public String toString() {
return new StringBuilder("ps ").append(showAll ? "--all=true" : "").append(showSize ? "--size=true" : "")
.append(sinceId != null ? "--since " + sinceId : "")
.append(beforeId != null ? "--before " + beforeId : "").append(limit != -1 ? "-n " + limit : "")
.toString();
.append(sinceId != null ? " --since " + sinceId : "")
.append(beforeId != null ? " --before " + beforeId : "").append(limit != -1 ? "-n " + limit : "")
.append(filters != null ? " --filters " + filters : "").toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.dockerjava.jaxrs;

import static com.google.common.net.UrlEscapers.*;

import java.util.List;

import javax.ws.rs.client.WebTarget;
Expand Down Expand Up @@ -32,6 +34,11 @@ protected List<Container> execute(ListContainersCmd command) {
webResource = webResource.queryParam("limit", String.valueOf(command.getLimit()));
}

if (command.getFilters() != null) {
webResource = webResource.queryParam("filters",
urlPathSegmentEscaper().escape(command.getFilters().toString()));
}

LOGGER.trace("GET: {}", webResource);
List<Container> containers = webResource.request().accept(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Container>>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.lang.reflect.Method;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.github.dockerjava.api.model.Capability.MKNOD;
Expand Down Expand Up @@ -490,4 +492,24 @@ public void createContainerWithULimits() throws DockerException {

}

@Test
public void createContainerWithLabels() throws DockerException {

Map<String, String> labels = new HashMap<String, String>();
labels.put("com.github.dockerjava.null", null);
labels.put("com.github.dockerjava.boolean", "true");

CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("sleep", "9999")
.withLabels(labels).exec();

LOG.info("Created container {}", container.toString());

assertThat(container.getId(), not(isEmptyString()));

InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec();

// null becomes empty string
labels.put("com.github.dockerjava.null", "");
assertThat(inspectContainerResponse.getConfig().getLabels(), is(equalTo(labels)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Filters;
import com.github.dockerjava.client.AbstractDockerClientTest;
import com.google.common.collect.ImmutableMap;

@Test(groups = "integration")
public class ListContainersCmdImplTest extends AbstractDockerClientTest {
Expand Down Expand Up @@ -96,7 +98,24 @@ public void testListContainers() throws DockerException {

Container container2 = filteredContainers.get(0);
assertThat(container2.getCommand(), not(isEmptyString()));
assertThat(container2.getImage(), startsWith(testImage + ":"));
assertThat(container2.getImage(), startsWith(testImage));

// list with filter by label
dockerClient.createContainerCmd(testImage).withCmd("echo").withLabels(ImmutableMap.of("test", "docker-java"))
.exec();
filteredContainers = dockerClient.listContainersCmd().withShowAll(true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This specific test doesn't work in my local test env (docker api 1.18). The is no filtering when passing label filter without 'label' keyword.

.withFilters(new Filters().withLabel("test=docker-java")).exec();
assertThat(filteredContainers.size(), is(equalTo(1)));
Container container3 = filteredContainers.get(0);
assertThat(container3.getCommand(), not(isEmptyString()));
assertThat(container3.getImage(), startsWith(testImage));

filteredContainers = dockerClient.listContainersCmd().withShowAll(true)
.withFilters(new Filters().withLabel("test")).exec();
assertThat(filteredContainers.size(), is(equalTo(1)));
container3 = filteredContainers.get(0);
assertThat(container3.getCommand(), not(isEmptyString()));
assertThat(container3.getImage(), startsWith(testImage));
}

}