一、首先要修改docker服務器的 /usr/lib/systemd/system/docker.service,加入紫色框的配置java
二、下載docker-java 的github源碼nginx
git clone https://github.com/docker-java/docker-java.git
三、本人這裏是在windows上用gitbash構建的git
cd docker-java/ mvn install -Dmaven.test.skip=true
構建成功後在maven生成jar包github
四、新建一個工程來使用該jar包docker
pom.xml以下:apache
<?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>
以上紅色加粗的是必要的json
1)操做容器的工具類:DockerClientServicewindows
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(); } }
測試:建立容器api
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()); } }
其餘測試省略bash
更多使用方法能夠查看下載出來的源碼的Test部分