docker 安装请参考: docker - 环境搭建
- 修改配置
默认不支持远程访问, 需要修改配置以开启远程访问, 开启后, 可以远程执行 docker 命令
# 修改 /usr/lib/systemd/system/docker.service 文件
less /usr/lib/systemd/system/docker.service
================
# 替换前
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
# 替换后, 端口号 2375 可自定义
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --containerd=/run/containerd/containerd.sock
================
# 保存修改, 重启docker
systemctl daemon-reload
systemctl restart docker
- 接口文档
https://docs.docker.com/engine/api/v1.40
执行 docker versioin, 查看 API version, 获取版本号, 替换 v1.40
- java-client
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>3.1.2</version>
</dependency>
- 代码示例
import java.io.IOException;
import org.junit.Test;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
public class ImageTest {
@Test
public void test() throws InterruptedException, IOException {
try {
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder() //
.withDockerHost("tcp://192.168.x.x:2375") //
.build();
DockerClient docker = DockerClientBuilder.getInstance(config).build();
docker.pushImageCmd("192.168.x.x:5000/test").exec(new PushImageResultCallback()).awaitCompletion();
List<Container> exec = docker.listContainersCmd().withNameFilter(Collections.singletonList("test")).exec();
InspectImageResponse exec = docker.inspectImageCmd("192.168.x.x:5000/test").exec();
docker.removeImageCmd("192.168.x.x:5000/test").exec();
List<Image> exec = docker.listImagesCmd().withImageNameFilter("192.168.x.x:5000/test").exec();
docker.removeImageCmd("231d40e811cd").withForce(true).exec();
docker.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
网友评论