Skip to content
docker-java edited this page Jul 3, 2014 · 29 revisions

Example code snippets:

Intialize docker client
DockerClient dockerClient = new DockerClient("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");

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);
        LOG.info(line);
    }
} finally {
    IOUtils.closeQuietly(response.getEntityInputStream());
}
Clone this wiki locally