forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Home
zdenekp edited this page Apr 3, 2019
·
29 revisions
Most example code snippets are taken from the test cases
DockerClient dockerClient = DefaultDockerClientConfig.getInstance("tcp://localhost:2375").build();
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("tcp://docker.somewhere.tld:2376")
.withDockerTlsVerify(true)
.withDockerCertPath("/home/user/.docker")
.withRegistryUsername(registryUser)
.withRegistryPassword(registryPass)
.withRegistryEmail(registryMail)
.withRegistryUrl(registryUrl)
.build();
// using jaxrs/jersey implementation here (netty impl is also available)
DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
.withReadTimeout(1000)
.withConnectTimeout(1000)
.withMaxTotalConnections(100)
.withMaxPerRouteConnections(10);
DockerClient dockerClient = DockerClientBuilder.getInstance(config)
.withDockerCmdExecFactory(dockerCmdExecFactory)
.build();
Info info = dockerClient.infoCmd().exec();
System.out.print(info);
List<SearchItem> dockerSearch = dockerClient.searchImagesCmd("busybox").exec();
System.out.println("Search returned" + dockerSearch.toString());
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
.withCmd("touch", "/test")
.exec();
dockerClient.startContainerCmd(container.getId()).exec();
dockerClient.stopContainerCmd(container.getId()).exec();
dockerClient.waitContainerCmd(container.getId()).exec();
Volume volume1 = new Volume("/opt/webapp1");
Volume volume2 = new Volume("/opt/webapp2");
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
.withVolumes(volume1, volume2)
.withHostConfig(HostConfig.newHostConfig()
.withBinds(new Bind("/src/webapp1", volume1, true), new Bind("/src/webapp2", volume2)))
.withCmd("true")
.exec();
dockerClient.startContainerCmd(container.getId())
.exec();
ExposedPort tcp22 = ExposedPort.tcp(22);
ExposedPort tcp23 = ExposedPort.tcp(23);
Ports portBindings = new Ports();
portBindings.bind(tcp22, Ports.Binding.bindPort(11022));
portBindings.bind(tcp23, Ports.Binding.bindPort(11023));
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
.withCmd("true")
.withExposedPorts(tcp22, tcp23)
.withHostConfig(HostConfig.newHostConfig()
.withPortBindings(portBindings))
.exec();
dockerClient.startContainerCmd(container.getId()).exec();
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.empty());
portBindings.bind(tcp23, Ports.Binding.empty());
CreateContainerResponse container = dockerClient
.createContainerCmd("busybox")
.withCmd("sleep", "9999")
.withExposedPorts(tcp22, tcp23)
.withHostConfig(HostConfig.newHostConfig()
.withPortBindings(portBindings)
.withPublishAllPorts(true))
.exec();
dockerClient.startContainerCmd(container.getId()).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")
.withHostConfig(HostConfig.newHostConfig()
.withLinks(new Link("container1", "container1Link")))
.exec();
dockerClient.startContainerCmd(container2.getId())
.exec();
CreateContainerResponse container1 = dockerClient
.createContainerCmd("busybox")
.withCmd("sleep", "9999")
.withName("container1")
.withHostConfig(HostConfig.newHostConfig()
.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")
.withHostConfig(HostConfig.newHostConfig()
.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")
.withHostConfig(HostConfig.newHostConfig()
.withVolumesFrom(new VolumesFrom("container1"), new VolumesFrom("container2")))
.exec();
dockerClient.startContainerCmd(container3.getId()).exec();
EventsResultCallback callback = new EventsResultCallback() {
@Override
public void onNext(Event event) {
System.out.println("Event: " + event);
super.onNext(event);
}
};
dockerClient.eventsCmd().exec(callback).awaitCompletion().close();
File baseDir = new File("~/kpelykh/docker/netcat");
BuildImageResultCallback callback = new BuildImageResultCallback() {
@Override
public void onNext(BuildResponseItem item) {
System.out.println("" + item);
super.onNext(item);
}
};
dockerClient.buildImageCmd(baseDir).exec(callback).awaitImageId();