private void checkDiskSpace(DockerClient dockerClient, String id) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { dockerClient .execStartCmd(dockerClient.execCreateCmd(id).withAttachStdout(true).withCmd("df", "-P").exec().getId()) .exec(new ExecStartResultCallback(outputStream, null)) .awaitCompletion(); } catch (Exception e) { log.debug("Can't exec disk checking command", e); } DiskSpaceUsage df = parseAvailableDiskSpace(outputStream.toString()); VisibleAssertions.assertTrue( "Docker environment should have more than 2GB free disk space", df.availableMB.map(it -> it >= 2048).orElse(true) ); }
private void exposePortThroughProxy(String networkAlias, int originalPort, int mappedPort) { ExecCreateCmdResponse createCmdResponse = dockerClient .execCreateCmd(proxy.getContainerId()) .withCmd("/usr/bin/socat", "TCP-LISTEN:" + originalPort + ",fork,reuseaddr", "TCP:" + networkAlias + ":" + mappedPort) .exec(); dockerClient.execStartCmd(createCmdResponse.getId()) .exec(new ExecStartResultCallback()); }
public void exec(String id, String... cmd){ ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(id) .withAttachStdout(true) .withCmd(cmd).exec(); }
public String call() throws Exception { DockerClient client = DockerCommand.getClient(descriptor, cfgData.dockerUrlRes, cfgData.dockerVersionRes, cfgData.dockerCertPathRes, null); final ExecCreateCmdResponse response; if (withAttachStdoutAndStderr) { response = client.execCreateCmd(id).withCmd(cmd).withAttachStderr(true).withAttachStdout(true).exec(); } else { response = client.execCreateCmd(id).withCmd(cmd).exec(); } return response.getId(); }
public static ExecCmdResult execCmd(DockerClient dockerClient, String containerId, String[] command) { ExecCreateCmdResponse cmd = dockerClient.execCreateCmd(containerId) .withAttachStdout(true) .withAttachStderr(true) .withCmd(command) .exec(); String cmdStdout; String cmdStderr; try (ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); ExecStartResultCallback cmdCallback = new ExecStartResultCallback(stdout, stderr)) { dockerClient.execStartCmd(cmd.getId()).exec(cmdCallback).awaitCompletion(); cmdStdout = stdout.toString(StandardCharsets.UTF_8.name()); cmdStderr = stderr.toString(StandardCharsets.UTF_8.name()); } catch (Exception e) { String format = String.format("Exception was thrown when executing: %s, for container: %s ", Arrays.toString(command), containerId); throw new IllegalStateException(format, e); } int exitCode = dockerClient.inspectExecCmd(cmd.getId()).exec().getExitCode(); String output = cmdStdout.isEmpty() ? cmdStderr : cmdStdout; return new ExecCmdResult(exitCode, output); }
public String execCommand(String containerId, boolean awaitCompletion, String... command) { ExecCreateCmdResponse exec = client.execCreateCmd(containerId).withCmd(command).withTty(false) .withAttachStdin(true).withAttachStdout(true).withAttachStderr(true).exec(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); String output = null; try { ExecStartResultCallback resultCallback = client.execStartCmd(exec.getId()).withDetach(false) .withTty(true).exec(new ExecStartResultCallback(outputStream, System.err)); if (awaitCompletion) { resultCallback.awaitCompletion(); } output = new String(outputStream.toByteArray()); } catch (InterruptedException e) { log.warn("Exception executing command {} on container {}", Arrays.toString(command), containerId, e); } return output; }
public String execCommand(String containerId, boolean awaitCompletion, String... command) { ExecCreateCmdResponse exec = client.execCreateCmd(containerId).withCmd(command).withTty(false) .withAttachStdin(true).withAttachStdout(true).withAttachStderr(true).exec(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); String output = null; try { ExecStartResultCallback resultCallback = client.execStartCmd(exec.getId()).withDetach(false) .withTty(true).exec(new ExecStartResultCallback(outputStream, System.err)); if (awaitCompletion) { resultCallback.awaitCompletion(); } output = new String(outputStream.toByteArray()); } catch (InterruptedException e) { log.warn("Exception executing command {} on container {}", Arrays.toString(command), containerId, e); } return output; }
private String execCreate(String containerId, String... commands) { ExecCreateCmdResponse execCreateCmdResponse = this.dockerClient.execCreateCmd(containerId) .withAttachStdout(true).withAttachStdin(true).withAttachStderr(true).withTty(false).withCmd(commands) .exec(); return execCreateCmdResponse.getId(); }
private ExecCreateCmdResponse execCreateCmd(ContainerName containerName, String user, String... command) { try { return dockerClient.execCreateCmd(containerName.asString()) .withCmd(command) .withAttachStdout(true) .withAttachStderr(true) .withUser(user) .exec(); } catch (NotFoundException e) { throw new ContainerNotFoundException(containerName); } }
private String execCreate(String containerId, String... commands) { ExecCreateCmdResponse execCreateCmdResponse = this.dockerClient.execCreateCmd(containerId) .withAttachStdout(true).withAttachStdin(true).withAttachStderr(true).withTty(false).withCmd(commands) .exec(); return execCreateCmdResponse.getId(); }
private String execCreate(String containerId, String... command) { return this.client.execCreateCmd(containerId) .withAttachStdout(true).withAttachStderr(true) .withAttachStdin(true).withTty(false) .withCmd(command).exec().getId(); } }
.withAttachStderr(true) .withTty(true) .withCmd("sh", "-c", "" + "echo 1 > /proc/sys/net/ipv4/ip_forward && " + iptablesRoute +
final String execID = client .execCreateCmd(cube.getId()) .withCmd(command) .withAttachStdout(true) .withAttachStderr(true)
@Test public void network_should_reach_pingpong2_by_alias_pong() throws UnsupportedEncodingException, InterruptedException { ExecCreateCmdResponse exec = dockerClient.execCreateCmd("pingpong").withCmd("curl", "http://pong:8080/").withAttachStdout(true).exec(); ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); dockerClient.execStartCmd(exec.getId()).exec(new ExecStartResultCallback(stdOut, null)).awaitCompletion(2, TimeUnit.SECONDS); assertThat(stdOut.toString("UTF-8")).isEqualToIgnoringWhitespace(EXPECTED_RESPONSE); }
@Test public void network_should_reach_pingpong_by_alias_ping() throws UnsupportedEncodingException, InterruptedException { ExecCreateCmdResponse exec = dockerClient.execCreateCmd("pingpong2").withCmd("curl", "http://ping:8080/").withAttachStdout(true).exec(); ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); dockerClient.execStartCmd(exec.getId()).exec(new ExecStartResultCallback(stdOut, null)).awaitCompletion(2, TimeUnit.SECONDS); assertThat(stdOut.toString("UTF-8")).isEqualToIgnoringWhitespace(EXPECTED_RESPONSE); }
@Test public void network_should_reach_pingpong_by_alias_foo() throws UnsupportedEncodingException, InterruptedException { ExecCreateCmdResponse exec = dockerClient.execCreateCmd("pingpong2").withCmd("curl", "http://foo:8080/").withAttachStdout(true).exec(); ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); dockerClient.execStartCmd(exec.getId()).exec(new ExecStartResultCallback(stdOut, null)).awaitCompletion(2, TimeUnit.SECONDS); assertThat(stdOut.toString("UTF-8")).isEqualToIgnoringWhitespace(EXPECTED_RESPONSE); } }
private String asyncRunCommand(String command, InputStream input, DockerStreamDecoder outputDecoder) { ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId) .withAttachStdin(true) .withAttachStdout(true) .withAttachStderr(true) .withCmd(command.split("\\s")) .exec(); ExecStartCmd execStartCmd = dockerClient.execStartCmd(containerId) .withTty(false) .withExecId(execCreateCmdResponse.getId()) .withDetach(false); if (input != null) { execStartCmd.withStdIn(input); } execStartCmd.exec(outputDecoder); return execCreateCmdResponse.getId(); }
@Test public void container_should_have_static_ip_for_app_net_network() throws InterruptedException, IOException { final InspectContainerResponse pingpong = dockerClient.inspectContainerCmd("pingpong").exec(); ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(pingpong.getId()) .withAttachStdout(true).withAttachStdin(true).withAttachStderr(true).withTty(false).withCmd("ifconfig") .exec(); try (OutputStream outputStream = new ByteArrayOutputStream(); OutputStream errorStream = new ByteArrayOutputStream()) { dockerClient.execStartCmd(execCreateCmdResponse.getId()).withDetach(false) .exec(new ExecStartResultCallback(outputStream, errorStream)).awaitCompletion(); assertThat(outputStream.toString()).contains("inet addr:172.16.238.10", "inet6 addr: fe80::42:acff:fe10:ee0a/64"); } }
execCreateCmd.withCmd(cmd);