com.spotify.docker.client.messages.ContainerCreation Java Examples

The following examples show how to use com.spotify.docker.client.messages.ContainerCreation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DockerContainerManager.java    From simpleci with MIT License 6 votes vote down vote up
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;
}
 
Example #2
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public ContainerCreation createContainer(final ContainerConfig config, final String name)
    throws DockerException, InterruptedException {
  WebTarget resource = resource()
      .path("containers").path("create");

  if (name != null) {
    checkArgument(CONTAINER_NAME_PATTERN.matcher(name).matches(),
                  "Invalid container name: \"%s\"", name);
    resource = resource.queryParam("name", name);
  }

  log.debug("Creating container with ContainerConfig: {}", config);

  try {
    return request(POST, ContainerCreation.class, resource, resource
        .request(APPLICATION_JSON_TYPE), Entity.json(config));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ImageNotFoundException(config.image(), e);
      case 406:
        throw new DockerException("Impossible to attach. Container not running.", e);
      default:
        throw e;
    }
  }
}
 
Example #3
Source File: HeliosSoloDeploymentTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  this.dockerClient = mock(DockerClient.class);
  this.heliosClient = mock(HeliosClient.class);

  // the anonymous classes to override a method are to workaround the docker-client "messages"
  // having no mutators, fun
  final Info info = mock(Info.class);
  when(info.operatingSystem()).thenReturn("foo");
  when(this.dockerClient.info()).thenReturn(info);

  // mock the call to dockerClient.createContainer so we can test the arguments passed to it
  this.containerConfig = ArgumentCaptor.forClass(ContainerConfig.class);

  final ContainerCreation creation = mock(ContainerCreation.class);
  when(creation.id()).thenReturn(CONTAINER_ID);

  when(this.dockerClient.createContainer(
      this.containerConfig.capture(), anyString())).thenReturn(creation);

  // we have to mock out several other calls to get the HeliosSoloDeployment ctor
  // to return non-exceptionally. the anonymous classes to override a method are to workaround
  // the docker-client "messages" having no mutators, fun
  when(this.dockerClient.info()).thenReturn(info);

  final PortBinding binding = PortBinding.of("192.168.1.1", 5801);
  final ImmutableMap<String, List<PortBinding>> ports =
      ImmutableMap.<String, List<PortBinding>>of("5801/tcp", ImmutableList.of(binding));
  final ContainerInfo containerInfo = mock(ContainerInfo.class);
  final NetworkSettings networkSettings = mock(NetworkSettings.class);
  when(networkSettings.gateway()).thenReturn("a-gate-way");
  when(networkSettings.ports()).thenReturn(ports);
  when(containerInfo.networkSettings()).thenReturn(networkSettings);
  when(this.dockerClient.inspectContainer(CONTAINER_ID)).thenReturn(containerInfo);

  when(this.dockerClient.waitContainer(CONTAINER_ID)).thenReturn(ContainerExit.create(0L));
}
 
Example #4
Source File: SupervisorTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySupervisorRestartsExitedContainer() throws Exception {
  final String containerId1 = "deadbeef1";
  final String containerId2 = "deadbeef2";

  final ContainerCreation createResponse1 = ContainerCreation.builder().id(containerId1).build();
  final ContainerCreation createResponse2 = ContainerCreation.builder().id(containerId2).build();

  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(createResponse1);

  final ImageInfo imageInfo = mock(ImageInfo.class);
  when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);

  when(docker.inspectContainer(eq(containerId1))).thenReturn(runningResponse);

  final SettableFuture<ContainerExit> waitFuture1 = SettableFuture.create();
  final SettableFuture<ContainerExit> waitFuture2 = SettableFuture.create();
  when(docker.waitContainer(containerId1)).thenAnswer(futureAnswer(waitFuture1));
  when(docker.waitContainer(containerId2)).thenAnswer(futureAnswer(waitFuture2));

  // Start the job
  sut.setGoal(START);
  verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class));
  verify(docker, timeout(30000)).startContainer(eq(containerId1));
  verify(docker, timeout(30000)).waitContainer(containerId1);

  // Indicate that the container exited
  when(docker.inspectContainer(eq(containerId1))).thenReturn(stoppedResponse);
  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(createResponse2);
  when(docker.inspectContainer(eq(containerId2))).thenReturn(runningResponse);
  waitFuture1.set(ContainerExit.create(1L));

  // Verify that the container was restarted
  verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class));
  verify(docker, timeout(30000)).startContainer(eq(containerId2));
  verify(docker, timeout(30000)).waitContainer(containerId2);
}
 
Example #5
Source File: TaskRunner.java    From helios with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #6
Source File: ReapingTest.java    From helios with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #7
Source File: SyslogRedirectionTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@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 + "'");
    }
  }
}
 
Example #8
Source File: PushPullIT.java    From docker-client with Apache License 2.0 5 votes vote down vote up
private static String startAndAwaitContainer(final DockerClient client,
                                             final ContainerConfig containerConfig,
                                             final String containerName)
    throws Exception {
  final ContainerCreation creation = client.createContainer(containerConfig, containerName);
  final String containerId = creation.id();
  client.startContainer(containerId);
  awaitRunning(client, containerId);
  return containerId;
}
 
Example #9
Source File: Docker.java    From james-project with Apache License 2.0 5 votes vote down vote up
public int getIMAPPort(ContainerCreation container) {
    try {
        return Integer.parseInt(
                Iterables.getOnlyElement(
                        dockerClient.inspectContainer(
                                container.id())
                                .networkSettings()
                                .ports()
                                .get(EXPOSED_IMAP_PORT))
                        .hostPort());
    } catch (NumberFormatException | DockerException | InterruptedException e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: Docker.java    From james-project with Apache License 2.0 5 votes vote down vote up
public void stop(ContainerCreation container) {
    try {
        dockerClient.killContainer(container.id());
        dockerClient.removeContainer(container.id(), RemoveContainerParam.removeVolumes());
    } catch (DockerException | InterruptedException e) {
        throw new RuntimeException(e);
    }
}
 
Example #11
Source File: Docker.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void waitingForSocketToBeReady(ContainerCreation container) {
    Awaitility
        .await()
        .atMost(30, TimeUnit.SECONDS)
        .with()
        .pollInterval(10, TimeUnit.MILLISECONDS)
        .and()
        .ignoreExceptions()
        .until(() -> socketIsReady(container));
}
 
Example #12
Source File: CyrusMailboxTestModule.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    bind(Docker.class).toInstance(new Docker("linagora/cyrus-imap"));
    bind(ContainerCreation.class).toProvider(CyrusHostSystem.class);
    bind(ImapHostSystem.class).to(CyrusHostSystem.class);
    bind(HostSystem.class).to(CyrusHostSystem.class);
    bind(ExternalHostSystem.class).to(CyrusHostSystem.class);
    bind(UserAdder.class).to(CyrusUserAdder.class);
    bind(GrantRightsOnHost.class).to(GrantRightsOnCyrusHost.class);
    bind(MailboxMessageAppender.class).to(MailboxMessageAppenderOnCyrusHost.class);
}
 
Example #13
Source File: Docker.java    From james-project with Apache License 2.0 4 votes vote down vote up
public ContainerCreation start() throws Exception {
    ContainerCreation container = dockerClient.createContainer(containerConfig);
    dockerClient.startContainer(container.id());
    waitingForSocketToBeReady(container);
    return container;
}
 
Example #14
Source File: DockerContainer.java    From docker-elastic-agents-plugin with Apache License 2.0 4 votes vote down vote up
public static DockerContainer create(CreateAgentRequest request, PluginSettings settings, DockerClient docker,
                                     ConsoleLogAppender consoleLogAppender) throws InterruptedException, DockerException {
    String containerName = UUID.randomUUID().toString();

    HashMap<String, String> labels = labelsFrom(request);
    String imageName = image(request.properties());
    List<String> env = environmentFrom(request, settings, containerName);

    try {
        docker.inspectImage(imageName);
        if (settings.pullOnContainerCreate()) {
            consoleLogAppender.accept("Pulling a fresh version of " + imageName + ".");
            LOG.info("Pulling a fresh version of " + imageName + ".");
            docker.pull(imageName);
        }
    } catch (ImageNotFoundException ex) {
        consoleLogAppender.accept("Image " + imageName + " not found, attempting to download.");
        LOG.info("Image " + imageName + " not found, attempting to download.");
        docker.pull(imageName);
    }

    ContainerConfig.Builder containerConfigBuilder = ContainerConfig.builder();
    if (StringUtils.isNotBlank(request.properties().get("Command"))) {
        containerConfigBuilder.cmd(splitIntoLinesAndTrimSpaces(request.properties().get("Command")).toArray(new String[]{}));
    }

    final String hostConfig = request.properties().get("Hosts");
    final String reservedMemory = request.properties().get("ReservedMemory");
    final String maxMemory = request.properties().get("MaxMemory");
    final String cpus = request.properties().get("Cpus");
    final String volumeMounts = request.properties().get("Mounts");

    HostConfig.Builder hostBuilder = HostConfig.builder()
            .privileged(privileged(request.properties()))
            .extraHosts(new Hosts(hostConfig))
            .memoryReservation(new MemorySpecification(reservedMemory).getMemory())
            .memory(new MemorySpecification(maxMemory).getMemory());

    CpusSpecification cpusValue = new CpusSpecification(cpus);
    if (cpusValue.getCpus() != null) {
        hostBuilder
                .cpuPeriod(cpusValue.getCpuPeriod())
                .cpuQuota(cpusValue.getCpuQuota());
    }
    if (volumeMounts != null) {
        hostBuilder.appendBinds(Util.splitIntoLinesAndTrimSpaces(volumeMounts));
    }

    ContainerConfig containerConfig = containerConfigBuilder
            .image(imageName)
            .labels(labels)
            .env(env)
            .hostConfig(hostBuilder.build())
            .build();

    consoleLogAppender.accept(String.format("Creating container: %s", containerName));
    ContainerCreation container = docker.createContainer(containerConfig, containerName);
    String id = container.id();

    ContainerInfo containerInfo = docker.inspectContainer(id);

    LOG.debug("Created container " + containerName);
    consoleLogAppender.accept(String.format("Starting container: %s", containerName));
    docker.startContainer(containerName);
    consoleLogAppender.accept(String.format("Started container: %s", containerName));
    LOG.debug("container " + containerName + " started");
    return new DockerContainer(id, containerName, request.jobIdentifier(), containerInfo.created(), request.properties(), request.environment());
}
 
Example #15
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private Optional<ContainerInfo> runAndPullIfNotExist(String commands) {
  // assure that we don't have multiple threads set the started container
  synchronized (reuseStartedContainerLock) {
    // reuse existing containers if they are running
    if (nonNull(startedContainer)) {
      log.info("Using existing container '{}' for image '{}'", left(startedContainer.id(), SHORT_ID_LENGTH), image);
      return of(startedContainer);
    }

    if (pullIfNotExist().isPresent()) {
      try {
        ContainerCreation container = dockerClient
            .createContainer(ContainerConfig
                .builder()
                .hostConfig(hostConfig)
                .exposedPorts(hostConfig.portBindings().keySet())
                .image(image)
                .env(env)
                .cmd(cmd(commands))
                .build());

        String containerId = container.id();
        String shortId = left(containerId, SHORT_ID_LENGTH);

        startedContainer = dockerClient.inspectContainer(containerId);

        log.info("Attempting to run container '{}' with commands '{}' for image '{}'", shortId, commands, image);

        dockerClient.startContainer(containerId);

        log.info("Successfully run container '{}' with commands '{}' for image '{}'", shortId, commands, image);

        return of(startedContainer);
      }
      catch (Exception e) {
        log.error("Failed to run container for image '{}'", image, e);
      }
    }

    return empty();
  }
}
 
Example #16
Source File: SupervisorTest.java    From helios with Apache License 2.0 4 votes vote down vote up
@Test
public void verifySupervisorStopsDockerContainerWithConfiguredKillTime() throws Exception {
  final String containerId = "deadbeef";

  final Job longKillTimeJob = Job.newBuilder()
      .setName(NAME)
      .setCommand(COMMAND)
      .setImage(IMAGE)
      .setVersion(VERSION)
      .setSecondsToWaitBeforeKill(30)
      .build();

  mockTaskStatus(longKillTimeJob.getId());

  final Supervisor longKillTimeSupervisor = createSupervisor(longKillTimeJob);

  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(ContainerCreation.builder().id(containerId).build());

  final ImageInfo imageInfo = mock(ImageInfo.class);
  when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);

  // Have waitContainer wait forever.
  final SettableFuture<ContainerExit> waitFuture = SettableFuture.create();
  when(docker.waitContainer(containerId)).thenAnswer(futureAnswer(waitFuture));

  // Start the job (so that a runner exists)
  longKillTimeSupervisor.setGoal(START);
  when(docker.inspectContainer(eq(containerId))).thenReturn(runningResponse);

  // This is already verified above, but it works as a hack to wait for the model/docker state
  // to converge in such a way that a setGoal(STOP) will work. :|
  verify(docker, timeout(30000)).waitContainer(containerId);

  // Stop the job
  longKillTimeSupervisor.setGoal(STOP);
  verify(docker, timeout(30000)).stopContainer(
      eq(containerId), eq(longKillTimeJob.getSecondsToWaitBeforeKill()));

  // Change docker container state to stopped now that it was killed
  when(docker.inspectContainer(eq(containerId))).thenReturn(stoppedResponse);
}
 
Example #17
Source File: SupervisorTest.java    From helios with Apache License 2.0 4 votes vote down vote up
@Test
public void verifySupervisorStartsAndStopsDockerContainer() throws Exception {
  final String containerId = "deadbeef";

  when(docker.createContainer(any(ContainerConfig.class), any(String.class)))
      .thenReturn(ContainerCreation.builder().id(containerId).build());

  final ImageInfo imageInfo = mock(ImageInfo.class);
  when(docker.inspectImage(IMAGE)).thenReturn(imageInfo);

  // Have waitContainer wait forever.
  final SettableFuture<ContainerExit> waitFuture = SettableFuture.create();
  when(docker.waitContainer(containerId)).thenAnswer(futureAnswer(waitFuture));

  // Start the job
  sut.setGoal(START);

  // Verify that the container is created
  verify(docker, timeout(30000)).createContainer(containerConfigCaptor.capture(),
      containerNameCaptor.capture());
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(START)
          .setState(CREATING)
          .setContainerId(null)
          .setEnv(ENV)
          .build())
  );
  final ContainerConfig containerConfig = containerConfigCaptor.getValue();
  assertEquals(IMAGE, containerConfig.image());
  assertEquals(EXPECTED_CONTAINER_ENV, ImmutableSet.copyOf(containerConfig.env()));
  final String containerName = containerNameCaptor.getValue();

  assertEquals(JOB.getId().toShortString(), shortJobIdFromContainerName(containerName));

  // Verify that the container is started
  verify(docker, timeout(30000)).startContainer(eq(containerId));
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(START)
          .setState(STARTING)
          .setContainerId(containerId)
          .setEnv(ENV)
          .build())
  );
  when(docker.inspectContainer(eq(containerId))).thenReturn(runningResponse);

  verify(docker, timeout(30000)).waitContainer(containerId);
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(START)
          .setState(RUNNING)
          .setContainerId(containerId)
          .setEnv(ENV)
          .build())
  );

  // Stop the job
  sut.setGoal(STOP);
  verify(docker, timeout(30000)).stopContainer(
      eq(containerId), eq(Supervisor.DEFAULT_SECONDS_TO_WAIT_BEFORE_KILL));

  // Change docker container state to stopped now that it was killed
  when(docker.inspectContainer(eq(containerId))).thenReturn(stoppedResponse);

  // Verify that the pulling state is signalled
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(START)
          .setState(PULLING_IMAGE)
          .setContainerId(null)
          .setEnv(ENV)
          .build())
  );

  // Verify that the STOPPING and STOPPED states are signalled
  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(STOP)
          .setState(STOPPING)
          .setContainerId(containerId)
          .setEnv(ENV)
          .build())
  );

  verify(model, timeout(30000)).setTaskStatus(eq(JOB.getId()),
      eq(TaskStatus.newBuilder()
          .setJob(JOB)
          .setGoal(STOP)
          .setState(STOPPED)
          .setContainerId(containerId)
          .setEnv(ENV)
          .build())
  );
}
 
Example #18
Source File: TaskRunnerTest.java    From helios with Apache License 2.0 4 votes vote down vote up
@Test
public void testContainerNotRunningVariation() throws Throwable {
  final TaskRunner.NopListener mockListener = mock(TaskRunner.NopListener.class);
  final ImageInfo mockImageInfo = mock(ImageInfo.class);
  final ContainerCreation mockCreation = mock(ContainerCreation.class);
  final HealthChecker mockHealthChecker = mock(HealthChecker.class);

  final ContainerState stoppedState = mock(ContainerState.class);
  when(stoppedState.running()).thenReturn(false);
  when(stoppedState.error()).thenReturn("container is a potato");
  final ContainerInfo stopped = mock(ContainerInfo.class);
  when(stopped.state()).thenReturn(stoppedState);

  when(mockCreation.id()).thenReturn("potato");
  when(mockDocker.inspectContainer(anyString())).thenReturn(stopped);
  when(mockDocker.inspectImage(IMAGE)).thenReturn(mockImageInfo);
  when(mockDocker.createContainer(any(ContainerConfig.class), anyString()))
      .thenReturn(mockCreation);
  when(mockHealthChecker.check(anyString())).thenReturn(false);

  final TaskRunner tr = TaskRunner.builder()
      .delayMillis(0)
      .config(TaskConfig.builder()
          .namespace("test")
          .host(HOST)
          .job(JOB)
          .containerDecorators(ImmutableList.of(containerDecorator))
          .build())
      .docker(mockDocker)
      .listener(mockListener)
      .healthChecker(mockHealthChecker)
      .build();

  tr.run();

  try {
    tr.resultFuture().get();
    fail("this should throw");
  } catch (Exception t) {
    assertTrue(t instanceof ExecutionException);
    assertEquals(RuntimeException.class, t.getCause().getClass());
    verify(mockListener).failed(t.getCause(), "container is a potato");
  }
}
 
Example #19
Source File: CyrusUserAdder.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Inject
private CyrusUserAdder(Docker docker, Provider<ContainerCreation> container) {
    this.docker = docker;
    this.container = container;
}
 
Example #20
Source File: CyrusHostSystem.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public ContainerCreation get() {
    return container;
}
 
Example #21
Source File: GCloudEmulatorManager.java    From flink with Apache License 2.0 4 votes vote down vote up
public static void launchDocker() throws DockerException, InterruptedException, DockerCertificateException {
	// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
	docker = DefaultDockerClient.fromEnv().build();

	terminateAndDiscardAnyExistingContainers(true);

	LOG.info("");
	LOG.info("/===========================================");
	LOG.info("| GCloud Emulator");

	ContainerInfo containerInfo;
	String id;

	try {
		docker.inspectImage(DOCKER_IMAGE_NAME);
	} catch (ImageNotFoundException e) {
		// No such image so we must download it first.
		LOG.info("| - Getting docker image \"{}\"", DOCKER_IMAGE_NAME);
		docker.pull(DOCKER_IMAGE_NAME, message -> {
			if (message.id() != null && message.progress() != null) {
				LOG.info("| - Downloading > {} : {}", message.id(), message.progress());
			}
		});
	}

	// No such container. Good, we create one!
	LOG.info("| - Creating new container");

	// Bind container ports to host ports
	final Map<String, List<PortBinding>> portBindings = new HashMap<>();
	portBindings.put(INTERNAL_PUBSUB_PORT, Collections.singletonList(PortBinding.randomPort("0.0.0.0")));

	final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();

	// Create new container with exposed ports
	final ContainerConfig containerConfig = ContainerConfig.builder()
		.hostConfig(hostConfig)
		.exposedPorts(INTERNAL_PUBSUB_PORT)
		.image(DOCKER_IMAGE_NAME)
		.cmd("sh", "-c", "mkdir -p /opt/data/pubsub ; gcloud beta emulators pubsub start --data-dir=/opt/data/pubsub --host-port=0.0.0.0:" + INTERNAL_PUBSUB_PORT)
		.build();

	final ContainerCreation creation = docker.createContainer(containerConfig, CONTAINER_NAME_JUNIT);
	id = creation.id();

	containerInfo = docker.inspectContainer(id);

	if (!containerInfo.state().running()) {
		LOG.warn("| - Starting it up ....");
		docker.startContainer(id);
		Thread.sleep(1000);
	}

	containerInfo = docker.inspectContainer(id);

	dockerIpAddress = "127.0.0.1";

	Map<String, List<PortBinding>> ports = containerInfo.networkSettings().ports();

	assertNotNull("Unable to retrieve the ports where to connect to the emulators", ports);
	assertEquals("We expect 1 port to be mapped", 1, ports.size());

	pubsubPort = getPort(ports, INTERNAL_PUBSUB_PORT, "PubSub");

	LOG.info("| Waiting for the emulators to be running");

	// PubSub exposes an "Ok" at the root url when running.
	if (!waitForOkStatus("PubSub", pubsubPort)) {
		// Oops, we did not get an "Ok" within 10 seconds
		startHasFailedKillEverything();
	}
	LOG.info("\\===========================================");
	LOG.info("");
}
 
Example #22
Source File: Docker.java    From james-project with Apache License 2.0 4 votes vote down vote up
private boolean socketIsReady(ContainerCreation container) throws UnknownHostException, IOException {
    try (Socket socket = new Socket(getHost(container), getIMAPPort(container))) {
        return socket.getInputStream().read() >= 0;
    }
}
 
Example #23
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@Override
public ContainerCreation commitContainer(final String containerId,
                                         final String repo,
                                         final String tag,
                                         final ContainerConfig config,
                                         final String comment,
                                         final String author)
    throws DockerException, InterruptedException {

  checkNotNull(containerId, "containerId");
  checkNotNull(repo, "repo");
  checkNotNull(config, "containerConfig");

  WebTarget resource = resource()
      .path("commit")
      .queryParam("container", containerId)
      .queryParam("repo", repo);

  if (!isNullOrEmpty(author)) {
    resource = resource.queryParam("author", author);
  }
  if (!isNullOrEmpty(comment)) {
    resource = resource.queryParam("comment", comment);
  }
  if (!isNullOrEmpty(tag)) {
    resource = resource.queryParam("tag", tag);
  }

  log.debug("Committing container id: {} to repository: {} with ContainerConfig: {}", containerId,
           repo, config);

  try {
    return request(POST, ContainerCreation.class, resource, resource
        .request(APPLICATION_JSON_TYPE), Entity.json(config));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #24
Source File: GCloudEmulatorManager.java    From flink with Apache License 2.0 4 votes vote down vote up
public static void launchDocker() throws DockerException, InterruptedException, DockerCertificateException {
	// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
	docker = DefaultDockerClient.fromEnv().build();

	terminateAndDiscardAnyExistingContainers(true);

	LOG.info("");
	LOG.info("/===========================================");
	LOG.info("| GCloud Emulator");

	ContainerInfo containerInfo;
	String id;

	try {
		docker.inspectImage(DOCKER_IMAGE_NAME);
	} catch (ImageNotFoundException e) {
		// No such image so we must download it first.
		LOG.info("| - Getting docker image \"{}\"", DOCKER_IMAGE_NAME);
		docker.pull(DOCKER_IMAGE_NAME, message -> {
			if (message.id() != null && message.progress() != null) {
				LOG.info("| - Downloading > {} : {}", message.id(), message.progress());
			}
		});
	}

	// No such container. Good, we create one!
	LOG.info("| - Creating new container");

	// Bind container ports to host ports
	final Map<String, List<PortBinding>> portBindings = new HashMap<>();
	portBindings.put(INTERNAL_PUBSUB_PORT, Collections.singletonList(PortBinding.randomPort("0.0.0.0")));

	final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();

	// Create new container with exposed ports
	final ContainerConfig containerConfig = ContainerConfig.builder()
		.hostConfig(hostConfig)
		.exposedPorts(INTERNAL_PUBSUB_PORT)
		.image(DOCKER_IMAGE_NAME)
		.cmd("sh", "-c", "mkdir -p /opt/data/pubsub ; gcloud beta emulators pubsub start --data-dir=/opt/data/pubsub  --host-port=0.0.0.0:" + INTERNAL_PUBSUB_PORT)
		.build();

	final ContainerCreation creation = docker.createContainer(containerConfig, CONTAINER_NAME_JUNIT);
	id = creation.id();

	containerInfo = docker.inspectContainer(id);

	if (!containerInfo.state().running()) {
		LOG.warn("| - Starting it up ....");
		docker.startContainer(id);
		Thread.sleep(1000);
	}

	containerInfo = docker.inspectContainer(id);

	dockerIpAddress = "127.0.0.1";

	Map<String, List<PortBinding>> ports = containerInfo.networkSettings().ports();

	assertNotNull("Unable to retrieve the ports where to connect to the emulators", ports);
	assertEquals("We expect 1 port to be mapped", 1, ports.size());

	pubsubPort = getPort(ports, INTERNAL_PUBSUB_PORT, "PubSub");

	LOG.info("| Waiting for the emulators to be running");

	// PubSub exposes an "Ok" at the root url when running.
	if (!waitForOkStatus("PubSub", pubsubPort)) {
		// Oops, we did not get an "Ok" within 10 seconds
		startHasFailedKillEverything();
	}
	LOG.info("\\===========================================");
	LOG.info("");
}
 
Example #25
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@Override
public ContainerCreation createContainer(final ContainerConfig config)
    throws DockerException, InterruptedException {
  return createContainer(config, null);
}
 
Example #26
Source File: Docker.java    From james-project with Apache License 2.0 4 votes vote down vote up
public void createUser(ContainerCreation container, String user, String password) throws DockerException, InterruptedException {
    String createUserCommand = String.format("echo %s | saslpasswd2 -u test -c %s -p", password, user);
    ExecCreation execCreation = dockerClient.execCreate(container.id(), new String[]{"/bin/bash", "-c", createUserCommand});
    dockerClient.execStart(execCreation.id());
}
 
Example #27
Source File: Docker.java    From james-project with Apache License 2.0 4 votes vote down vote up
public String getHost(ContainerCreation container) {
    return dockerClient.getHost();
}
 
Example #28
Source File: DockerClient.java    From docker-client with Apache License 2.0 3 votes vote down vote up
/**
 * Create a new image from a container's changes.
 *
 * @param containerId The id of the container to commit.
 * @param comment     commit message.
 * @param author      image author.
 * @param tag         image tag.
 * @param repo        repository to commit to.
 * @param config      ContainerConfig to commit.
 * @return ContainerCreation reply.
 * @throws ContainerNotFoundException
 *                            if container was not found (404)
 * @throws DockerException      if a server error occurred (500)
 * @throws InterruptedException If the thread is interrupted
 */
ContainerCreation commitContainer(final String containerId,
                                  final String repo,
                                  final String tag,
                                  final ContainerConfig config,
                                  final String comment,
                                  final String author)
    throws DockerException, InterruptedException;
 
Example #29
Source File: DockerClient.java    From docker-client with Apache License 2.0 2 votes vote down vote up
/**
 * Create a docker container.
 *
 * @param config The container configuration.
 * @param name   The container name.
 * @return Container creation result with container id and eventual warnings from docker.
 * @throws ImageNotFoundException
 *                            if the requested parent image was not found (404)
 * @throws DockerException   if logs cannot be attached, because container is not running (406),
 *                              or if a server error occurred (500)
 * @throws InterruptedException If the thread is interrupted
 */
ContainerCreation createContainer(ContainerConfig config, String name)
    throws DockerException, InterruptedException;
 
Example #30
Source File: DockerClient.java    From docker-client with Apache License 2.0 2 votes vote down vote up
/**
 * Create a docker container.
 *
 * @param config The container configuration.
 * @return Container creation result with container id and eventual warnings from docker.
 * @throws ImageNotFoundException
 *                            if the requested parent image was not found (404)
 * @throws DockerException  if logs cannot be attached, because container is not running (406),
 *                              or if a server error occurred (500)
 * @throws InterruptedException If the thread is interrupted
 */
ContainerCreation createContainer(ContainerConfig config)
    throws DockerException, InterruptedException;