Skip to content
marcuslinke edited this page Jul 7, 2015 · 29 revisions

Most examples code snippet are taken from the test cases

Intialize docker client
DockerClient dockerClient = DockerClientBuilder.getInstance("http://localhost:2375").build();
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, start and stop it:
ContainerCreateResponse container = dockerClient.createContainerCmd("busybox")
   .withCmd("touch", "/test")
   .exec();

dockerClient.startContainerCmd(container.getId()).exec();
dockerClient.stopContainerCmd(container.getId()).exec();
dockerClient.waitContainerCmd(container.getId()).exec();
Create new Docker container and start it with mount binded volumes
Volume volume1 = new Volume("/opt/webapp1");
Volume volume2 = new Volume("/opt/webapp2");

CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
   .withVolumes(volume1, volume2)
   .withBinds(new Bind("/src/webapp1", volume1, true), new Bind("/src/webapp2", volume2))
   .withCmd("true")
   .exec();

dockerClient.startContainerCmd(container.getId())
   .exec();
Create new Docker container and start it with exposed ports
ExposedPort tcp22 = ExposedPort.tcp(22);
ExposedPort tcp23 = ExposedPort.tcp(23);

Ports portBindings = new Ports();
portBindings.bind(tcp22, Ports.Binding(11022));
portBindings.bind(tcp23, Ports.Binding(11023));

CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
   .withCmd("true")
   .withExposedPorts(tcp22, tcp23)
   .withPortBindings(portBindings)
   .exec();

dockerClient.startContainerCmd(container.getId()).exec();
Create new Docker container and start it with auto exposed ports
ExposedPort tcp22 = ExposedPort.tcp(22);
ExposedPort tcp23 = ExposedPort.tcp(23);

CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
   .withCmd("sleep", "9999")
   .withExposedPorts(tcp22, tcp23)
   .exec();

Ports portBindings = new Ports();
portBindings.bind(tcp22, Ports.Binding(null));
portBindings.bind(tcp23, Ports.Binding(null));

CreateContainerResponse container = dockerClient
   .createContainerCmd("busybox")
   .withCmd("sleep", "9999")
   .withExposedPorts(tcp22, tcp23)
   .withPortBindings(portBindings)
   .withPublishAllPorts(true).exec();

dockerClient.startContainerCmd(container.getId()).exec();
Create two Docker containers and start it with linking
CreateContainerResponse container1 = dockerClient.createContainerCmd("busybox")
   .withCmd("sleep", "9999")
   .withName("container1")
   .exec();

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

CreateContainerResponse container2 = dockerClient.createContainerCmd("busybox")
   .withCmd("true")
   .withName("container2")
   .withLinks(new Link("container1", "container1Link"))
   .exec();

dockerClient.startContainerCmd(container2.getId())
   .exec();
Create two containers with mount binded volumes and inherit its volumes in a third container
CreateContainerResponse container1 = dockerClient
   .createContainerCmd("busybox")
   .withCmd("sleep", "9999")
   .withName("container1")
   .withBinds(new Bind("/src/webapp1", new Volume("/opt/webapp1")))
   .exec();
		
dockerClient.startContainerCmd(container1.getId()).exec();

CreateContainerResponse container2 = dockerClient
   .createContainerCmd("busybox")
   .withCmd("sleep", "9999")
   .withName("container2")
   .withBinds(new Bind("/src/webapp2", new Volume("/opt/webapp2")))
   .exec();
		
dockerClient.startContainerCmd(container2.getId()).exec();

// create a third container with all volumes from container1 and container2
CreateContainerResponse container3 = dockerClient
   .createContainerCmd("busybox")
   .withCmd("sleep", "9999")
   .withName("container3")
   .withVolumesFrom(new VolumesFrom("container1"), new VolumesFrom("container2"))
   .exec();

dockerClient.startContainerCmd(container3.getId()).exec();
Handle events
ResultCallback<Event> callback = new ResultCallbackTemplate<Event>() {
    @Override
    public void onNext(Event event) {
       System.out.println("Event: " + event);
    }
};

dockerClient.eventsCmd(callback).exec();
        
Thread.sleep(10000);

callback.close();
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