docker开启远程访问,参考 docker学习(十七)docker开启远程访问
<!-- https://mvnrepository.com/artifact/com.github.docker-java/docker-java -->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>3.2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.docker-java/docker-java-transport-httpclient5 -->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-httpclient5</artifactId>
<version>3.2.11</version>
</dependency>
public static DockerClient connect() throws URISyntaxException {
String host = "tcp://172.16.10.151:2375";
String apiVersion = "1.38";
//创建DefaultDockerClientConfig
DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withApiVersion(apiVersion)
.withDockerHost(host)
.build();
//创建DockerHttpClient
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
.dockerHost(config.getDockerHost())
.maxConnections(100)
.connectionTimeout(Duration.ofSeconds(30))
.responseTimeout(Duration.ofSeconds(45))
.build();
//创建DockerClient
DockerClient client = DockerClientImpl.getInstance(config, httpClient);
return client;
}
1、docker ping
private static void dockerPing() throws URISyntaxException {
DockerClient client = connect();
client.pingCmd().exec();
System.out.println("ping...");
}
2、docker info
private static void dockerInfo() throws URISyntaxException {
DockerClient client = connect();
Info info = client.infoCmd().exec();
System.out.println("docker info : " + info.toString());
}
docker info :
{
"architecture":"x86_64",
"containers":17,
"containersStopped":8,
"containersPaused":0,
"containersRunning":9,
"cpuCfsPeriod":true,
"cpuCfsQuota":true,
"cpuShares":true,
"cpuSet":true,
"debug":false,
"dockerRootDir":"/var/lib/docker",
"driver":"overlay2",
...
}
3、创建容器
public static CreateContainerResponse createContainer(DockerClient client) throws URISyntaxException {
CreateContainerCmd containerCmd = client.createContainerCmd("nginx:latest")
//名字
.withName("nginx-01")
//端口映射 内部80端口与外部81端口映射
.withHostConfig(new HostConfig().withPortBindings(new Ports(new ExposedPort(80), Ports.Binding.bindPort(81))))
//环境变量
.withEnv("key=value")
//挂载
.withVolumes(new Volume("/var/log"));
//创建
CreateContainerResponse response = containerCmd.exec();
System.out.println(response.getId());
return response;
}
4、创建并启动容器
public static void startContainer() throws URISyntaxException {
DockerClient client = connect();
//创建
CreateContainerResponse response = createContainer(client);
String containerId = response.getId();
//启动
client.startContainerCmd(containerId).exec();
}
5、停止容器
public static void stopContainer() throws URISyntaxException {
String containerId = "f97cca9cc21f";
DockerClient client = connect();
client.stopContainerCmd(containerId).exec();
}
6、删除容器
public static void removeContainer() throws URISyntaxException {
String containerId = "f97cca9cc21f";
DockerClient client = connect();
client.removeContainerCmd(containerId).exec();
}
7、构建镜像
准备Dockerfile和后台jar包
public static String buildImage() throws URISyntaxException {
String imageName = "app";
String imageTag = "v1";
DockerClient client = connect();
ImmutableSet<String> tag = ImmutableSet.of(imageName + ":" + imageTag);
String imageId = client.buildImageCmd(new File("/opt/tmp/Dockerfile"))
.withTags(tag)
.start()
.awaitImageId();
return imageId;
}
8、打镜像tag
public static void tagImage() throws URISyntaxException {
DockerClient client = connect();
client.tagImageCmd("nginx:latest", "172.16.10.151:80/library/nginx", "v2").exec();
}
9、推送镜像
推送到Harbor仓库
public static void pushImage() throws URISyntaxException, InterruptedException {
DockerClient client = connect();
AuthConfig authConfig = new AuthConfig()
.withUsername("admin")
.withPassword("Harbor12345")
.withRegistryAddress("172.16.10.151:80");
client.pushImageCmd("172.16.10.151:80/library/nginx:v2")
.withAuthConfig(authConfig)
.start()
.awaitCompletion(30, TimeUnit.SECONDS);
}
10、删除镜像
public static void removeImage() throws URISyntaxException {
DockerClient client = connect();
client.removeImageCmd("172.16.10.151:80/library/nginx:v2").exec();
}
11、拉取镜像
public static void pullImage() throws URISyntaxException, InterruptedException {
DockerClient client = connect();
client.pullImageCmd("172.16.10.151:80/library/nginx:v2")
.start()
.awaitCompletion(30, TimeUnit.SECONDS);
}
12、查看镜像详细信息
public static void inspectImage() throws URISyntaxException, InterruptedException {
DockerClient client = connect();
InspectImageResponse response = client.inspectImageCmd("172.16.10.151:80/library/nginx:v2").exec();
System.out.println(new Gson().toJson(response));
}