Skip to content
marcuslinke edited this page Aug 19, 2014 · 29 revisions

Example code snippets:

Intialize docker client
DockerClient dockerClient = new DockerClientImpl("http://localhost:2375");
Get Docker info:
Info info = dockerClient.infoCmd().exec();
System.out.print(info);
Search Docker repository:
List<SearchItem> dockerSearch = dockerClient.searchImagesCmd("busybox").exec();
System.out.println("Search returned" + dockerSearch.toString());
Create new Docker container, wait for its start and stop it:
ContainerCreateResponse container =
     dockerClient.createContainerCmd("busybox")
          .withCmd("touch", "/test")
                .exec();
dockerClient.startContainerCmd(container.id).exec();
dockerClient.waitContainerCmd(container.id).exec();
dockerClient.stopContainerCmd(container.id).exec();
Build image from Dockerfile
File baseDir = new File("~/kpelykh/docker/netcat");

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

StringWriter logwriter = new StringWriter();

try {
    LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
    while (itr.hasNext()) {
        String line = itr.next();
        logwriter.write(line);
        LOG.info(line);
    }
} finally {
    IOUtils.closeQuietly(response);
}
Clone this wiki locally