码迷,mamicode.com
首页 > 编程语言 > 详细

java操作docker示例(docker-java)

时间:2019-04-16 09:15:37      阅读:1524      评论:0      收藏:0      [点我收藏+]

标签:工程   test   rjava   das   pom   print   构建   command   sch   

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

技术图片

 

2、下载docker-java 的github源码

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

3、本人这里是在windows上用gitbash构建的

cd docker-java/
mvn install -Dmaven.test.skip=true

构建成功后在maven生成jar包

技术图片

 

4、新建一个工程来使用该jar包

技术图片

pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.boshen.simple</groupId>
    <artifactId>docker-java-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.github.docker-java</groupId>
            <artifactId>docker-java</artifactId>
            <version>3.1.3-SNAPSHOT</version>
        </dependency>
        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-nop</artifactId>
             <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>
    </dependencies>



</project>

以上红色加粗的是必要的

1)操作容器的工具类:DockerClientService

package com.simple.docker.util;

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.137.12: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){
        //映射端口8088—>80
        ExposedPort tcp80 = ExposedPort.tcp(80);
        Ports portBindings = new Ports();
        portBindings.bind(tcp80, Ports.Binding.bindPort(8088));

        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();
    }

}

测试:创建容器

package com.simple.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.simple.docker.util.DockerClientService;

public class CreateAndStartContainer {


    public static void main(String[] args){
        DockerClientService dockerClientService =new DockerClientService();
        //连接docker服务器
        DockerClient client = dockerClientService.connectDocker();
        //创建容器
        CreateContainerResponse container = dockerClientService.createContainers(client,"nginx1","nginx:latest");
        //启动容器
        dockerClientService.startContainer(client,container.getId());
    }

}

技术图片

其他测试省略

更多使用方法可以查看下载出来的源码的Test部分

技术图片

 

java操作docker示例(docker-java)

标签:工程   test   rjava   das   pom   print   构建   command   sch   

原文地址:https://www.cnblogs.com/boshen-hzb/p/10714414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!