Docker Java API
•
Java
docker-java
Docker Java is the Java version API of docker
The current implementation is based on Jersey 2 X, so classpath is not compatible with the old version of Jersey 1 x !
Developer Forum docker Java
Changelog Wiki
Version support
Supports a subset of the Docker Remote API v1. 37,Docker Server version since 1.12. six
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<!-- use latest version https://github.com/docker-java/docker-java/releases -->
<version>3.X.Y</version>
</dependency>
The latest version is 3.1 0, you can click here to view the latest version.
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>3.1.0</version>
</dependency>
Wiki documents
For code examples,please look at the Wiki or Test cases
Configure docker environment
The configurable items and default values of the system are as follows:
There are three ways to configure,in descending order of precedence:
Programming mode configuration:
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("tcp://my-docker-host.tld:2376")
.withDockerTlsVerify(true)
.withDockerCertPath("/home/user/.docker/certs")
.withDockerConfig("/home/user/.docker")
.withApiVersion("1.30") // optional
.withRegistryUrl("https://index.docker.io/v1/")
.withRegistryUsername("dockeruser")
.withRegistryPassword("ilovedocker")
.withRegistryEmail("dockeruser@github.com")
.build();
DockerClient docker = DockerClientBuilder.getInstance(config).build();
Through properties (docker Java. Properties)
DOCKER_HOST=tcp://localhost:2376
DOCKER_TLS_VERIFY=1
DOCKER_CERT_PATH=/home/user/.docker/certs
DOCKER_CONfig=/home/user/.docker
api.version=1.23
registry.url=https://index.docker.io/v1/
registry.username=dockeruser
registry.password=ilovedocker
registry.email=dockeruser@github.com
Through system properties:
java -DDOCKER_HOST=tcp://localhost:2375 -Dregistry.username=dockeruser pkg.Main
Through environment
export DOCKER_HOST=tcp://localhost:2376
export DOCKER_TLS_VERIFY=1
export DOCKER_CERT_PATH=/home/user/.docker/certs
export DOCKER_CONfig=/home/user/.docker
test
Let's simply test:
DockerClient dockerClient = createClient();
// docker info
Info info = dockerClient.infoCmd().exec();
System.out.print(info);
// 搜索镜像
List<SearchItem> dockerSearch = dockerClient.searchImagesCmd("busy@R_860_2419@").exec();
System.out.println("Search returned" + dockerSearch.toString());
// pull
dockerClient.pullImageCmd("busy@R_860_2419@:latest").exec(new ResultCallback<PullResponseItem>() {
public void onStart(Closeable closeable) {
}
public void onNext(PullResponseItem object) {
System.out.println(object.getStatus());
}
public void onError(Throwable throwable) {
throwable.printStackTrace();
}
public void onComplete() {
System.out.println("pull finished");
}
public void close() throws IOException {
}
});
// 创建container 并测试
// create
CreateContainerResponse container = dockerClient.createContainerCmd("busy@R_860_2419@")
.withCmd("/bin/bash")
.exec();
// start
dockerClient.startContainerCmd(container.getId()).exec();
dockerClient.stopContainerCmd(container.getId()).exec();
dockerClient.removeContainerCmd(container.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();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码