@Test
  public void testListContainers() throws DockerException {

    String testImage = "hackmann/empty";

    LOG.info("Pulling image 'hackmann/empty'");
    // need to block until image is pulled completely
    logResponseStream(dockerClient.pullImageCmd(testImage).exec());
    tmpImgs.add(testImage);

    List<Container> containers = dockerClient.listContainersCmd().withShowAll(true).exec();
    assertThat(containers, notNullValue());
    LOG.info("Container List: {}", containers);

    int size = containers.size();

    ContainerCreateResponse container1 =
        dockerClient.createContainerCmd(testImage).withCmd(new String[] {"echo"}).exec();

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

    ContainerInspectResponse containerInspectResponse =
        dockerClient.inspectContainerCmd(container1.getId()).exec();

    assertThat(containerInspectResponse.getConfig().getImage(), is(equalTo(testImage)));

    dockerClient.startContainerCmd(container1.getId()).exec();
    tmpContainers.add(container1.getId());

    LOG.info("container id: " + container1.getId());

    List<Container> containers2 = dockerClient.listContainersCmd().withShowAll(true).exec();

    for (Container container : containers2) {
      LOG.info("listContainer: id=" + container.getId() + " image=" + container.getImage());
    }

    assertThat(size + 1, is(equalTo(containers2.size())));
    Matcher matcher = hasItem(hasField("id", startsWith(container1.getId())));
    assertThat(containers2, matcher);

    List<Container> filteredContainers =
        filter(hasField("id", startsWith(container1.getId())), containers2);
    assertThat(filteredContainers.size(), is(equalTo(1)));

    for (Container container : filteredContainers) {
      LOG.info("filteredContainer: " + container.getImage());
    }

    Container container2 = filteredContainers.get(0);
    assertThat(container2.getCommand(), not(isEmptyString()));
    assertThat(container2.getImage(), equalTo(testImage + ":latest"));
  }
  @Test
  public void testNetCatDockerfileBuilder()
      throws DockerException, IOException, InterruptedException {
    File baseDir =
        new File(Thread.currentThread().getContextClassLoader().getResource("netcat").getFile());

    ClientResponse response = dockerClient.buildImageCmd(baseDir).exec();

    StringWriter logwriter = new StringWriter();

    try {
      LineIterator itr = IOUtils.lineIterator(response.getEntityInputStream(), "UTF-8");
      while (itr.hasNext()) {
        String line = itr.next();
        logwriter.write(line + "\n");
        LOG.info(line);
      }
    } finally {
      IOUtils.closeQuietly(response.getEntityInputStream());
    }

    String fullLog = logwriter.toString();
    assertThat(fullLog, containsString("Successfully built"));

    String imageId = StringUtils.substringBetween(fullLog, "Successfully built ", "\\n\"}").trim();

    ImageInspectResponse imageInspectResponse = dockerClient.inspectImageCmd(imageId).exec();
    assertThat(imageInspectResponse, not(nullValue()));
    LOG.info("Image Inspect: {}", imageInspectResponse.toString());
    tmpImgs.add(imageInspectResponse.getId());

    ContainerCreateResponse container =
        dockerClient.createContainerCmd(imageInspectResponse.getId()).exec();
    assertThat(container.getId(), not(isEmptyString()));
    dockerClient.startContainerCmd(container.getId()).exec();
    tmpContainers.add(container.getId());

    ContainerInspectResponse containerInspectResponse =
        dockerClient.inspectContainerCmd(container.getId()).exec();

    assertThat(containerInspectResponse.getId(), notNullValue());
    assertThat(containerInspectResponse.getNetworkSettings().getPorts(), notNullValue());

    // No use as such if not running on the server
    for (Ports.Port p : containerInspectResponse.getNetworkSettings().getPorts().getAllPorts()) {
      int port = Integer.valueOf(p.getHostPort());
      LOG.info("Checking port {} is open", port);
      assertThat(available(port), is(false));
    }
    dockerClient.stopContainerCmd(container.getId()).withTimeout(0).exec();
  }