@Before public void setup() throws Exception { try (final DockerClient docker = getNewDockerClient()) { // Build an image with an ENTRYPOINT and CMD prespecified final String dockerDirectory = Resources.getResource("syslog-test-image").getPath(); docker.build(Paths.get(dockerDirectory), testImage); // Figure out the host IP from the container's point of view (needed for syslog) final ContainerConfig config = ContainerConfig.builder() .image(BUSYBOX) .cmd(asList("ip", "route", "show")) .build(); final ContainerCreation creation = docker.createContainer(config); final String containerId = creation.id(); docker.startContainer(containerId); // Wait for the container to exit. // If we don't wait, docker.logs() might return an epmty string because the container // cmd hasn't run yet. docker.waitContainer(containerId); final String log; try (LogStream logs = docker.logs(containerId, stdout(), stderr())) { log = logs.readFully(); } final Matcher m = DEFAULT_GATEWAY_PATTERN.matcher(log); if (m.find()) { syslogHost = m.group("gateway"); } else { fail("couldn't determine the host address from '" + log + "'"); } } }
dockerClient.startContainer(container.id()); return new ContainerCreationStatus(true, containerName, container.id(), nodePort); } catch (DockerRequestException e) {
private void startContainer(final String name) throws DockerException, InterruptedException { docker.pull(BUSYBOX); final HostConfig hostConfig = HostConfig.builder().build(); final ContainerConfig config = ContainerConfig.builder() .image(BUSYBOX) .cmd(IDLE_COMMAND) .hostConfig(hostConfig) .build(); final ContainerCreation creation = docker.createContainer(config, name); final String containerId = creation.id(); docker.startContainer(containerId); } }
private String startContainer(final String image, final Optional<String> dockerVersion) throws InterruptedException, DockerException { // Get container image info final ImageInfo imageInfo = docker.inspectImage(image); if (imageInfo == null) { throw new HeliosRuntimeException("docker inspect image returned null on image " + image); } // Create container final HostConfig hostConfig = config.hostConfig(dockerVersion); final ContainerConfig containerConfig = config.containerConfig(imageInfo, dockerVersion) .toBuilder() .hostConfig(hostConfig) .build(); listener.creating(); final ContainerCreation container = docker.createContainer(containerConfig, containerName); log.info("created container: {}: {}, {}", config, container, containerConfig); listener.created(container.id()); // Start container log.info("starting container: {}: {} {}", config, container.id(), hostConfig); listener.starting(); docker.startContainer(container.id()); log.info("started container: {}: {}", config, container.id()); listener.started(); return container.id(); }
final ContainerCreation creation = docker.createContainer(config, testTag + "-probe"); final String containerId = creation.id(); docker.startContainer(containerId);
final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog"); final String syslogContainerId = creation.id(); docker.startContainer(syslogContainerId);
private void startContainer(DockerClient docker, String containerId) { LOG.info("Starting container '{}", containerName); try { docker.startContainer(containerId); LOG.debug("Container '{}' started", containerName); } catch (DockerException | InterruptedException e) { LOG.error("Unable to start container '{}'. Is there something running on the same ports?", containerName); throw new EnvironmentBuilderException("Unable to start container", e); } }
private void startContainer(DockerClient docker, String containerId) { LOG.info("Starting Couchbase container '{}", CONTAINER_NAME); try { docker.startContainer(containerId); LOG.debug("Couchbase container '{}' started", CONTAINER_NAME); } catch (DockerException | InterruptedException e) { LOG.error("Unable to start Couchbase container '{}'. Is there something running on the same ports?", CONTAINER_NAME); throw new EnvironmentBuilderException("Unable to start Couchbase container", e); } }
private void startContainer(String containerId) throws DockerException, InterruptedException { logger().info("Starting container #{}", containerId); client.startContainer(containerId); }
@Override public void process(InputStream inputStream) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer); String cc = writer.toString(); try { docker.startContainer(cc); } catch (DockerException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } getLogger().info("Successfully started container: " + cc); } });
public String runContainer(ContainerConfig.Builder containerConfigBuilder, String containerName) throws DockerException { try { final ContainerConfig containerConfig = containerConfigBuilder.build(); final ContainerCreation creation; creation = dockerClient.createContainer(containerConfig, containerName); final String id = creation.id(); dockerClient.startContainer(id); return id; } catch (InterruptedException e) { logger.error("", e); } return null; }
/** * Instructs Docker to start the container * * @throws DockerException * if docker cannot start the container */ private void startContainer() throws DockerException { final long startTime = System.currentTimeMillis(); try { LOGGER.info("Starting container {} with id {}", containerConfig.image(), containerId); client.startContainer(containerId); } catch (DockerException | InterruptedException e) { throw new DockerException( "Unable to start container " + containerConfig.image() + " with id " + containerId, e); } finally { final long elapsedMillis = System.currentTimeMillis() - startTime; LOGGER.info("Container {} started in {}ms", containerConfig.image(), elapsedMillis); } }
/** * Start a docker container * * @throws NodeException this exception will be thrown if the container can not be started */ public void start(String containerName) { log.info("Starting docker container: {}", containerName); try { ContainerInfo container = docker.inspectContainer(containerName); if (container.state().running() || container.state().paused()) { log.warn("The container `{}` already running, should stop before start", container.name()); return; } docker.startContainer(containerName); } catch (DockerException | InterruptedException ex) { throw new NodeException("Can't start container " + containerName, ex); } }
public static void main(String[] args) throws DockerException, InterruptedException { System.out.println("Testing"); // Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars try { final DockerClient docker = DefaultDockerClient.builder() .uri("http://192.168.99.100:2375") .build(); //Create a container ContainerConfig cc = ContainerConfig.builder() .image("nginx") .build(); final ContainerCreation container = docker.createContainer(cc); docker.startContainer(container.id()); } catch (Exception ex) { ex.printStackTrace(); } } }
public void runContainer() { try { buildImageConfig(); try { createImage(); } catch (ImageNotFoundException e) { dockerClient.pull(dockerHost.getImage(), new ProcessHandlerLogger()); createImage(); } dockerClient.startContainer(containerId); final ContainerInfo info = dockerClient.inspectContainer(containerId); portMappings = info.networkSettings().ports(); } catch (InterruptedException | DockerException e) { logger.error("Error while setting up docker container", e); throw new RuntimeException("Error while setting up docker container", e); } }
public void start(DockerClient dockerClient) throws DockerException, InterruptedException, IOException { dockerClient.startContainer(container.id()); Thread thread = new Thread(() -> { try { dockerClient.attachContainer(container.id(), DockerClient.AttachParameter.LOGS, DockerClient.AttachParameter.STDOUT, DockerClient.AttachParameter.STDERR, DockerClient.AttachParameter.STREAM) .attach(System.out, System.err); } catch (Exception e) { e.printStackTrace(); } }); thread.setDaemon(true); thread.start(); }
@Override public void start(RuntimeId runtimeId) throws RuntimeOperationException { DockerRuntime runtime = (DockerRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId()); try { LOG.info("Starting container: " + runtimeId.getId()); docker.getDockerClient(runtime.getProviderId()).startContainer(runtime.getId()); refresh(runtimeId); } catch (DockerException | InterruptedException ex) { LOG.error("Error Starting container: " + runtimeId.getId(), ex); throw new RuntimeOperationException("Error Starting container: " + runtimeId.getId(), ex); } }
@Override public void start(RuntimeId runtimeId) throws RuntimeOperationException { DockerRuntime runtime = (DockerRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId()); try { LOG.info("Starting container: " + runtimeId.getId()); docker.getDockerClient(runtime.getProviderId()).startContainer(runtime.getId()); refresh(runtimeId); } catch (DockerException | InterruptedException ex) { LOG.error("Error Starting container: " + runtimeId.getId(), ex); throw new RuntimeOperationException("Error Starting container: " + runtimeId.getId(), ex); } }
@Override protected void before() throws Throwable { super.before(); dockerClient.startContainer(container.id()); ContainerInfo info = dockerClient.inspectContainer(container.id()); ports = info.networkSettings().ports(); if (params.portToWaitOn != null) { waitForPort(getHostPort(params.portToWaitOn), params.waitTimeout); } if (params.logToWait != null) { waitForLog(params.logToWait); } }
/** * Deploy and start docker container, expose ports, connect to a network * * @return docker container id */ private String deployContainer() { ContainerConfig containerConfig = buildContainerConfig(); String id; try { ContainerCreation container = docker.createContainer(containerConfig, params.getName()); id = container.id(); dockerManager.addShutdownHook(params.getName()); docker.disconnectFromNetwork(id, "bridge"); docker.connectToNetwork(id, docker.inspectNetwork(universeParams.getNetworkName()).id()); docker.startContainer(id); String ipAddr = docker.inspectContainer(id) .networkSettings().networks() .values().asList().get(0) .ipAddress(); if (StringUtils.isEmpty(ipAddr)) { throw new NodeException("Empty Ip address for container: " + params.getName()); } ipAddress.set(ipAddr); } catch (InterruptedException | DockerException e) { throw new NodeException("Can't start a container", e); } return id; }