Java连接Docker创建、启动、删除、停止容器

Java连接Docker创建、启动、删除、停止容器

Scroll Down

1、首先要修改docker服务器的 /usr/lib/systemd/system/docker.service,加入如下的配置

-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

目的就是开放docker的2375端口,这样才可以使用dockerapi

在这里插入图片描述

2、下载docker-java 的github源码

https://github.com/docker-java/docker-java.git

3、下载解压后如下,然后打包成Jar包

在这里插入图片描述

4、开始打包

mvn install -Dmaven.test.skip=true

5、创建Java项目,导入Jar包

在这里插入图片描述

pom.xml中添加如下maven的Jar包:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
	<version>1.2.38</version>
</dependency>
<dependency>
    <groupId>com.github.docker-java</groupId>
    <artifactId>docker-java</artifactId>
    <version>3.2.0-SNAPSHOT</version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-nop</artifactId>
   <version>1.7.2</version>
</dependency>

6、操作容器的工具类:DockerClientService

import com.alibaba.fastjson.JSONObject;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.core.DockerClientBuilder;

import static com.github.dockerjava.api.model.HostConfig.newHostConfig;

public class DockerClientService {
    /**
     * 连接docker服务器
     * @return
     */
    public DockerClient connectDocker(){
        DockerClient dockerClient = DockerClientBuilder.getInstance("tcp://192.168.182.129:2375").build();
        Info info = dockerClient.infoCmd().exec();
        String infoStr = JSONObject.toJSONString(info);
        System.out.println("docker的环境信息如下:=================");
        System.out.println(info);
        return dockerClient;
    }

    /**
     * 创建容器
     * @param client
     * @return
     */
    public CreateContainerResponse createContainers(DockerClient client,String containerName,String imageName){
        //映射端口8080—>80
        ExposedPort tcp80 = ExposedPort.tcp(80);
        Ports portBindings = new Ports();
        portBindings.bind(tcp80, Ports.Binding.bindPort(8080));

        CreateContainerResponse container = client.createContainerCmd(imageName)
                .withName(containerName)
                .withHostConfig(newHostConfig().withPortBindings(portBindings))
                .withExposedPorts(tcp80).exec();
        return container;
    }


    /**
     * 启动容器
     * @param client
     * @param containerId
     */
    public void startContainer(DockerClient client,String containerId){
        client.startContainerCmd(containerId).exec();
    }

    /**
     * 停止容器
     * @param client
     * @param containerId
     */
    public void stopContainer(DockerClient client,String containerId){
        client.stopContainerCmd(containerId).exec();
    }

    /**
     * 删除容器
     * @param client
     * @param containerId
     */
    public void removeContainer(DockerClient client,String containerId){
        client.removeContainerCmd(containerId).exec();
    }
    public static void main(String[] args){
        DockerClientService dockerClientService =new DockerClientService();
        //连接docker服务器
        DockerClient client = dockerClientService.connectDocker();
        //删除容器
        //dockerClientService.removeContainer(client,"08f6568decd0");
        //停止容器
        //dockerClientService.stopContainer(client,"9b72aad8e1db");
        //启动容器
        //dockerClientService.startContainer(client,"9b72aad8e1db");
        //创建容器并启动
        CreateContainerResponse response=dockerClientService.createContainers(client,"mytomcat","tomcat:8.5");
        dockerClientService.startContainer(client,response.getId());
    }
}