Skip to content
graf-markus edited this page May 1, 2020 · 9 revisions

Java Docker API Client

Usage:

Setup:

As Dependencies this OSGi Plug-In requires com.google.gson, org.apache.httpcomponents.httpcore and org.apache.httpcomponents.httpclient

IDockerClient docker = DockerClientBuilder.builder().setUrl("http://localhost:2375").build();

Container:

List Containers

listContainers returns a list of containers. Default only running ones.

List<Container> containers = docker.listContainers(); // only running
List<Container> containers = docker.listContainers(ListContainersParam.allContainer()); // all containers

Create Container

createContainer creates a new Container with the given config.

ContainerCreation creation = docker.createContainer(ContainerConfig.builder().addCmd("/bin/bash").build());
ContainerCreation creation = docker.createContainer(ContainerConfig.builder().addCmd("/bin/bash").build(), "name for new Container");

Inspect Container

inspectContainer returns more information about a specific Container

ContainerInfo info = docker.inspectContainer("containerId");

Top Container

topContainer list running processes inside a Container like the Linux top command.

TopResults top = docker.topContainer("containerId", "aux");

Inspect Changes

inspectContainerChanges inspects the changes of the Filesystem of the Container.

List<ContainerChanges> changes = docker.inspectContainerChanges("containerId");

Export Container

exportContainer Export the contents of a Container as a tarball.

docker.exportContainer("containerId");

Stat Container based on resource usage

statContainer and statContainerStream gets the current resource usage of a Container.

With statContainer you get the stats once.

With statContainerStream you get the stats continuously. This is running in a seperated Thread. Keep in mind that if the container exists this method doesn't throw an error even if the container is not running (e.g stopContainer without removing it or startContainer without the autoremove option) and also if you stop a running container, which you have started without the autoremove option, on which you listen on the listening doesn't stop but the returned values aren't useful.

ContainerStats stats = docker.statContainer("containerId");
docker.statContainerStream("containerId", IContainerStatListener);

Resize TTY

resizeTTYContainer resizes the TTY for a container.

docker.resizeTTYContainer("containerId", 64, 256);

Start Container

startContainer starts a Container if it is not running.

docker.startContainer("containerId");

Stop Container

stopContainer stops a Container if it is running.

If a time is given the Docker Engine waits the time in Seconds until it stops the Container.

docker.stopContainer("containerId");
docker.stopContainer("containerId", 1000) // time in millisecond
docker.stopContainer("containerid", 2, TimeUnit.SECONDS);

Restart Container

restartContainer restarts a Container.

If a time is given the Docker Engine waits the time in Seconds until it restarts the Container.

docker.restartContainer("containerId");
docker.restartContainer("containerId", 1000) // time in millisecond
docker.restartContainer("containerid", 2, TimeUnit.SECONDS);

Kill Container

killContainer killes a Container with the given id or name and with the given SIGNAL.

docker.topContainer("containerId", KillSignal.SIGTERM);

Update Container

updateContainer updates the HostConfig of a Container.

ContainerUpdate updateInfo = docker.updateContainer("containerId", HostConfig.builder().build());

Rename Container

renameContianer renames a existing Container.

docker.renameContainer("containerId", "newName");

Pause Container

pauseContainer pauses a Container with the given id or name.

docker.pauseContainer("containerId");

Unpause Container

unpauseContainer unpauses a Container with the given id or name.

docker.unpauseContainer("containerId");

Wait for Container

waitForContainer blocks until a container stops, then returns the exit code.

ContainerExit exit = docker.waitForContainer("containerId");

Remove Container

removeContainer removes a Container with the given id or name.

docker.removeContainer("containerId", RemoveContainersParam.force());

File Info

fileInfoContainer gets the Metadata of agiven file or folder inside a Container.

ContainerFileInfo info = docker.fileInfoContainer("containerId", "/path/to/file");

Get a Archive

archiveContainer gets an tar archive of a resource inside a Container.

docker.archiveContainer("containerId", "/path/to/file");

Extract To Container

extractToContainer extracts an archive (gzip, xz, bzip2) from the host to the container.

docker.extractToContainer("containerId", "/path/in/container", "/path/to/archive");

Delete Container

deleteContainers delete all stopped Containers.

ContainerDeletedInfo info = docker.deleteContainers();

Run Container

runContainer creates a new Container with the given config and then starts the newly created Container. Attention! If the HostConfig.autoremove(true) option isn't used, this Method throws an error on the second run.

docker.runContianer(Containerconfig.builder().addCmd("/bin/bash").build());
docker.runContianer(Containerconfig.builder().addCmd("/bin/bash").build(), "containername");

Stop Stat Stream

stopStatContainerStream stopps a previously started statContainerAsStream Thread.

docker.stopStatContainerStream("containerId");