Java 操作 Docker 使用 java 执行 Docker 内部命令

        String containerName = "generated_" + new SecureRandom().nextInt();
        CreateContainerResponse container = client.createContainerCmd("busybox").withCmd("sleep", "9999")
                .withName(containerName).exec();
        client.startContainerCmd(container.getId()).exec();
        ExecCreateCmdResponse execCreateCmdResponse = client.execCreateCmd(container.getId())
                .withAttachStdout(true).withCmd("ls").exec();
        client.execStartCmd(execCreateCmdResponse.getId()).withDetach(false).withTty(true)
                .exec(new ExecStartResultCallback(System.out, System.err)).awaitCompletion();

System.out,System.err 分别是结果的输出流,可替换为 OutputStream

命令为.witchCmd(“ls”) 函数

执行内部命令前,要先启动 container

  
    展开阅读全文