forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Home
marcuslinke edited this page Sep 12, 2014
·
29 revisions
Most examples code snippet are taken from the test cases
DockerClient dockerClient = new DockerClientImpl("http://localhost:2375");
Info info = dockerClient.infoCmd().exec();
System.out.print(info);
List<SearchItem> dockerSearch = dockerClient.searchImagesCmd("busybox").exec();
System.out.println("Search returned" + dockerSearch.toString());
ContainerCreateResponse container = dockerClient.createContainerCmd("busybox")
.withCmd("touch", "/test")
.exec();
dockerClient.startContainerCmd(container.id).exec();
dockerClient.waitContainerCmd(container.id).exec();
dockerClient.stopContainerCmd(container.id).exec();
Volume volume1 = new Volume("/opt/webapp1");
Volume volume2 = new Volume("/opt/webapp2");
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
.withVolumes(volume1, volume2)
.withCmd("true")
.exec();
dockerClient.startContainerCmd(container.getId())
.withBinds(new Bind("/src/webapp1", volume1, true), new Bind("/src/webapp2", volume2))
.exec();
ExposedPort tcp22 = ExposedPort.tcp(22);
ExposedPort tcp23 = ExposedPort.tcp(23);
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
.withCmd("true")
.withExposedPorts(tcp22, tcp23)
.exec();
Ports portBindings = new Ports();
portBindings.bind(tcp22, Ports.Binding(11022));
portBindings.bind(tcp23, Ports.Binding(11023));
dockerClient.startContainerCmd(container.getId())
.withPortBindings(portBindings)
.exec();
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")
.exec();
dockerClient.startContainerCmd(container2.getId())
.withLinks(new Link("container1", "container1Link"))
.exec();
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);
}