docker 删除所有未启动的容器_Java连接Docker创建、启动、删除、停止容器

86ef47b4323a45bbb0444d16e1408a75

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

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

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

aeadc7d8f39c4039b02cd5528e787636

2、下载docker-java 的github源码

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

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

d09f113fe4ab4fb3bccadf24736e4063

4、开始打包

mvn install -Dmaven.test.skip=true

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

a23e21bad7264b47bf458ee9b09d7658

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

com.alibaba    fastjson1.2.38com.github.docker-java    docker-java    3.2.0-SNAPSHOTorg.slf4j   slf4j-nop   1.7.2

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());    }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值