如果对于docker安装及基础命令有疑问的可以看这些
Docker概念、安装以及配置镜像加速器
Docker进程、镜像、容器相关命令
Docker应用部署(Nginx部署)

1,配置远程访问

在/usr/lib/systemd/system/docker.service。文件中添加配置远程访问

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

在这里插入图片描述

注意:原来的execStart 要注释掉

2,添加依赖

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hTCD6K9O-1682043931281)(D:\Users\lps\Desktop\三阶段笔记\docker-java\assert\1681979398158.jpg)]

在pom.xml中添加docker-java 依赖

     <dependency>
            <groupId>com.github.docker-java</groupId>
            <artifactId>docker-java</artifactId>
            <version>3.1.5</version>
        </dependency>

3,编写docker-java工具类

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.HostConfig;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.core.DockerClientBuilder;

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

public class DockerClientUtils {
    /**
     * 连接Docker服务器
     *
     * @return
     */
    public DockerClient connectDocker(String dockerInstance) {
        DockerClient dockerClient = DockerClientBuilder.getInstance(dockerInstance).build();
        dockerClient.infoCmd().exec();
        return dockerClient;
    }

    /**
     * 创建并启动容器
     *
     * @param client
     * @param containerName 容易名称
     * @param imageName     镜像名称
     * @param withoutPort   外部端口
     * @param interiorPort  映射的内部端口
     * @return
     */
    public CreateContainerResponse createContainers(DockerClient client, String containerName, String imageName, int withoutPort, int interiorPort) {
        //映射端口withoutPort—>interiorPort
        ExposedPort tcp80 = ExposedPort.tcp(interiorPort);
        Ports portBindings = new Ports();
        portBindings.bind(tcp80, Ports.Binding.bindPort(withoutPort));

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

//        CreateContainerResponse container = client.createContainerCmd(imageName)
//                .withName(containerName)
//                .withHostConfig(new HostConfig().
//                withPortBindings(new Ports(new ExposedPort(8080),
//                Ports.Binding.bindPort(8080))))
//环境变量
//                .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();
    }
}

4,main方法测试

     public static void main(String[] args) {
        DockerClientUtils dockerClientUtils = new DockerClientUtils();
        //连接Docker服务器
        DockerClient client = dockerClientUtils.connectDocker("tcp://192.168.10.41:2375");
        //创建容器
        //CreateContainerResponse container = dockerClientUtils.createContainers(client,"container1","vulhub/appweb:7.0.1",8080,8080);
        CreateContainerResponse container = dockerClientUtils.createContainers(client, "container1", "tomcat", 8080, 8080);
        //启动容器
        //System.out.println(container.getId());
        dockerClientUtils.startContainer(client, container.getId());
        System.out.println("=================");
        System.out.println(container.getId());

        //关闭及停止容器
      /*  dockerClientUtils.stopContainer(client,"27ebd4faeeaffcd4930eb4062fa2ade5dae9382161170dbe3825b83c7217954b");
        dockerClientUtils.removeContainer(client,"27ebd4faeeaffcd4930eb4062fa2ade5dae9382161170dbe3825b83c7217954b");*/
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3NjcWxfw-1682043435893)(C:\Users\lps\AppData\Roaming\Typora\typora-user-images\image-20230421101630309.png)]
也可以正常关闭
在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐