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

The following examples show how to use com.spotify.docker.client.messages.ContainerInfo. 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: DockerContainerTest.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStartContainerWithHostEntry() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "alpine:latest");
    properties.put("Hosts", "127.0.0.2\tbaz \n192.168.5.1\tfoo\tbar\n127.0.0.1  gocd.local ");
    properties.put("Command", "cat\n/etc/hosts");

    DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod", jobIdentifier, Collections.EMPTY_MAP), createClusterProfiles(), docker, consoleLogAppender);

    containers.add(container.name());
    ContainerInfo containerInfo = docker.inspectContainer(container.name());

    final ImmutableList<String> extraHosts = containerInfo.hostConfig().extraHosts();
    assertThat(extraHosts, containsInAnyOrder(
            "baz:127.0.0.2", "foo\tbar:192.168.5.1", "gocd.local:127.0.0.1"
    ));

    String logs = docker.logs(container.name(), DockerClient.LogsParam.stdout()).readFully();
    assertThat(logs, containsString("127.0.0.2\tbaz"));
    assertThat(logs, containsString("192.168.5.1\tfoo"));
    assertThat(logs, containsString("127.0.0.1\tgocd.local"));

    AgentStatusReport agentStatusReport = container.getAgentStatusReport(docker);
    assertThat(agentStatusReport.getHosts(), containsInAnyOrder(
            "baz:127.0.0.2", "foo\tbar:192.168.5.1", "gocd.local:127.0.0.1"));
}
 
Example #2
Source File: DockerUtils.java    From Singularity with Apache License 2.0 6 votes vote down vote up
public ContainerInfo inspectContainer(final String containerName)
  throws DockerException {
  Callable<ContainerInfo> callable = new Callable<ContainerInfo>() {

    @Override
    public ContainerInfo call() throws Exception {
      return dockerClient.inspectContainer(containerName);
    }
  };

  try {
    return callWithRetriesAndTimeout(callable);
  } catch (Exception e) {
    throw new DockerException(e);
  }
}
 
Example #3
Source File: SingularityExecutorCleanup.java    From Singularity with Apache License 2.0 6 votes vote down vote up
private void stopContainer(Container container) {
  try {
    ContainerInfo containerInfo = dockerUtils.inspectContainer(container.id());
    if (containerInfo.state().running()) {
      dockerUtils.stopContainer(
        container.id(),
        executorConfiguration.getDockerStopTimeout()
      );
      LOG.debug("Forcefully stopped container {}", container.names());
    }
    dockerUtils.removeContainer(container.id(), true);
    LOG.debug("Removed container {}", container.names());
  } catch (Exception e) {
    LOG.error("Failed to stop or remove container {}", container.names(), e);
    exceptionNotifier.notify(
      String.format("Failed stopping container (%s)", e.getMessage()),
      e,
      Collections.<String, String>emptyMap()
    );
  }
}
 
Example #4
Source File: RedisContainer.java    From pay-publicapi with MIT License 6 votes vote down vote up
private static int hostPortNumber(ContainerInfo containerInfo) {
    String redisPortSpec = INTERNAL_PORT + "/tcp";
    PortBinding portBinding;
    try {
        portBinding = Objects.requireNonNull(containerInfo
                .networkSettings()
                .ports())
                .get(redisPortSpec)
                .get(0);
        logger.info("Redis host port: {}", portBinding.hostPort());
        return parseInt(portBinding.hostPort());
    } catch (NullPointerException e) {
        logger.error("Unable to find host port mapping for {} in container {}", redisPortSpec, containerInfo.id());
        throw e;
    }
}
 
Example #5
Source File: DockerContainerTest.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStartContainerWithMountedVolumes() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "busybox:latest");
    // using "/" as source folder because it seems the folder must exist on testing machine
    properties.put("Mounts", "/:/A\n/:/B:ro");

    PluginSettings clusterProfiles = createClusterProfiles();
    DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod", jobIdentifier, Collections.EMPTY_MAP), clusterProfiles, docker, consoleLogAppender);
    containers.add(container.name());

    ContainerInfo containerInfo = docker.inspectContainer(container.name());

    assertThat(containerInfo.hostConfig().binds(), containsInAnyOrder(
            "/:/A", "/:/B:ro"));

    DockerContainer dockerContainer = DockerContainer.fromContainerInfo(containerInfo);

    assertThat(dockerContainer.properties(), is(properties));
}
 
Example #6
Source File: DockerContainerTest.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStartContainerNoMemoryLimits() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "busybox:latest");
    properties.put("ReservedMemory", "");
    properties.put("MaxMemory", "");

    PluginSettings clusterProfiles = createClusterProfiles();
    DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod", jobIdentifier, Collections.EMPTY_MAP), clusterProfiles, docker, consoleLogAppender);
    containers.add(container.name());

    ContainerInfo containerInfo = docker.inspectContainer(container.name());

    assertThat(containerInfo.hostConfig().memoryReservation(), is(0L));
    assertThat(containerInfo.hostConfig().memory(), is(0L));
}
 
Example #7
Source File: DockerContainerTest.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStartContainerWithMemoryLimits() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "busybox:latest");
    properties.put("ReservedMemory", "6M");
    properties.put("MaxMemory", "10M");

    PluginSettings clusterProfiles = createClusterProfiles();
    DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod", jobIdentifier, Collections.EMPTY_MAP), clusterProfiles, docker, consoleLogAppender);
    containers.add(container.name());

    ContainerInfo containerInfo = docker.inspectContainer(container.name());

    assertThat(containerInfo.hostConfig().memoryReservation(), is(6 * 1024 * 1024L));
    assertThat(containerInfo.hostConfig().memory(), is(10 * 1024 * 1024L));

    DockerContainer dockerContainer = DockerContainer.fromContainerInfo(containerInfo);

    assertThat(dockerContainer.properties(), is(properties));
}
 
Example #8
Source File: DockerContainerTest.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStartContainerWithPrivilegedMode() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "busybox:latest");
    properties.put("Privileged", "true");

    PluginSettings clusterProfiles = createClusterProfiles();
    clusterProfiles.setEnvironmentVariables("GLOBAL=something");
    DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod", jobIdentifier, Collections.EMPTY_MAP), clusterProfiles, docker, consoleLogAppender);
    containers.add(container.name());

    ContainerInfo containerInfo = docker.inspectContainer(container.name());

    assertThat(containerInfo.hostConfig().privileged(), is(true));
    DockerContainer dockerContainer = DockerContainer.fromContainerInfo(containerInfo);

    assertThat(dockerContainer.properties(), is(properties));
}
 
Example #9
Source File: DockerContainerTest.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStartContainerWithCorrectEnvironment() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "busybox:latest");
    properties.put("Environment", "A=B\nC=D\r\nE=F\n\n\nX=Y");

    PluginSettings clusterProfiles = createClusterProfiles();
    clusterProfiles.setEnvironmentVariables("GLOBAL=something");
    DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod", jobIdentifier, Collections.EMPTY_MAP), clusterProfiles, docker, consoleLogAppender);
    containers.add(container.name());

    ContainerInfo containerInfo = docker.inspectContainer(container.name());

    assertThat(containerInfo.config().env(), hasItems("A=B", "C=D", "E=F", "X=Y", "GLOBAL=something"));
    DockerContainer dockerContainer = DockerContainer.fromContainerInfo(containerInfo);

    assertThat(dockerContainer.properties(), is(properties));
}
 
Example #10
Source File: DockerContainers.java    From docker-elastic-agents-plugin with Apache License 2.0 6 votes vote down vote up
private DockerContainers unregisteredAfterTimeout(PluginSettings settings, Agents knownAgents) throws Exception {
    Period period = settings.getAutoRegisterPeriod();
    DockerContainers unregisteredContainers = new DockerContainers();

    for (String containerName : instances.keySet()) {
        if (knownAgents.containsAgentWithId(containerName)) {
            continue;
        }

        ContainerInfo containerInfo;
        try {
            containerInfo = docker(settings).inspectContainer(containerName);
        } catch (ContainerNotFoundException e) {
            LOG.warn("The container " + containerName + " could not be found.");
            continue;
        }
        DateTime dateTimeCreated = new DateTime(containerInfo.created());

        if (clock.now().isAfter(dateTimeCreated.plus(period))) {
            unregisteredContainers.register(DockerContainer.fromContainerInfo(containerInfo));
        }
    }
    return unregisteredContainers;
}
 
Example #11
Source File: DockerUtil.java    From SuitAgent with Apache License 2.0 6 votes vote down vote up
/**
 * 容器网络模式是否为host模式
 * @param containerId
 * @return
 */
public static boolean isHostNet(String containerId){
    String cacheKey = "isHostNet" + containerId;
    Boolean v = (Boolean) getCache(cacheKey);
    if (v != null){
        return v;
    }
    Boolean value = false;
    try {
        ContainerInfo containerInfo = getContainerInfo(containerId);
        if (containerInfo != null) {
            ImmutableMap<String, AttachedNetwork> networks =  containerInfo.networkSettings().networks();
            if (networks != null && !networks.isEmpty()){
                value = networks.get("host") != null && StringUtils.isNotEmpty(networks.get("host").ipAddress());
                setCache(cacheKey,value);
            }else {
                log.warn("容器{}无Networks配置",containerInfo.name());
            }
        }
    } catch (Exception e) {
        log.error("",e);
    }
    return value;
}
 
Example #12
Source File: PushPullIT.java    From docker-client with Apache License 2.0 5 votes vote down vote up
private static void awaitRunning(final DockerClient client, final String containerId)
    throws Exception {
  Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<Object>() {
    @Override
    public Object call() throws Exception {
      final ContainerInfo containerInfo = client.inspectContainer(containerId);
      return containerInfo.state().running() ? true : null;
    }
  });
}
 
Example #13
Source File: PollingDockerClient.java    From helios with Apache License 2.0 5 votes vote down vote up
@Override
public ContainerExit waitContainer(final String containerId)
    throws DockerException, InterruptedException {
  // XXX (dano): We're doing this poll loop instead of docker.waitContainer() because we saw the
  //             agent hang forever on waitContainer after the socket got into a weird half-open
  //             state where the kernel (netstat/lsof) would only show one end of the connection
  //             and restarting docker would not close the socket. ¯\_(ツ)_/¯
  while (true) {
    final ContainerInfo info = inspectContainer(containerId);
    if (!info.state().running()) {
      return ContainerExit.create(info.state().exitCode());
    }
    Thread.sleep(WAIT_INSPECT_INTERVAL_MILLIS);
  }
}
 
Example #14
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public ContainerInfo inspectContainer(final String containerId)
    throws DockerException, InterruptedException {
  try {
    final WebTarget resource = resource().path("containers").path(containerId).path("json");
    return request(GET, ContainerInfo.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example #15
Source File: TaskRunner.java    From helios with Apache License 2.0 5 votes vote down vote up
protected String getContainerError() {
  final ContainerInfo info;
  try {
    // If we don't know our containerId at this point there's not a lot we can do.
    info = getContainerInfo(containerId.orNull());
  } catch (DockerException | InterruptedException e) {
    log.warn("failed to propagate container error: {}", e);
    return "";
  }
  if (info == null) {
    return "";
  }
  return info.state().error();
}
 
Example #16
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Set<File> download(final ContainerInfo containerInfo, final String fromContainerPath, final File toLocal) {
  String containerId = containerInfo.id();
  String shortId = left(containerId, SHORT_ID_LENGTH);

  ImmutableSet.Builder<File> files = ImmutableSet.builder();

  log.info("Attempting to download from path '{}' in container '{}' for image '{}'",
      fromContainerPath, shortId, image);

  try (final TarArchiveInputStream tarStream = new TarArchiveInputStream(
      dockerClient.archiveContainer(containerId, fromContainerPath))) {

    TarArchiveEntry entry;
    while ((entry = tarStream.getNextTarEntry()) != null) {
      log.info("Downloading entry '{}' in container '{}' for image '{}'", entry.getName(), shortId, image);

      String entryName = entry.getName();
      entryName = entryName.substring(entryName.indexOf('/') + 1);
      if (entryName.isEmpty()) {
        continue;
      }

      File file = (toLocal.exists() && toLocal.isDirectory()) ? new File(toLocal, entryName) : toLocal;
      files.add(file);

      try (OutputStream outStream = new FileOutputStream(file)) {
        copy(tarStream, outStream);
      }
    }
  }
  catch (Exception e) {
    log.error("Failed to download from path '{}' in container '{}' for image '{}'",
        fromContainerPath, shortId, image, e);
  }

  return files.build();
}
 
Example #17
Source File: TaskRunner.java    From helios with Apache License 2.0 5 votes vote down vote up
private ContainerInfo getContainerInfo(final String existingContainerId)
    throws DockerException, InterruptedException {
  if (existingContainerId == null) {
    return null;
  }
  log.info("inspecting container: {}: {}", config, existingContainerId);
  try {
    return docker.inspectContainer(existingContainerId);
  } catch (ContainerNotFoundException e) {
    return null;
  }
}
 
Example #18
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private boolean exec(final ContainerInfo container,
                     final String commands,
                     final OutputStream outputStream)
{
  String containerId = container.id();
  String shortId = left(containerId, SHORT_ID_LENGTH);

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

  try {
    // attach stdin as well as stdout/stderr to workaround https://github.com/spotify/docker-client/issues/513
    final ExecCreation execCreation = dockerClient
        .execCreate(containerId, cmd(commands), attachStdin(), attachStdout(), attachStderr());

    try (final LogStream stream = dockerClient.execStart(execCreation.id())) {
      // pretend to be a command line, by printing command to run
      log.debug("$ " + commands);

      // Why read each, instead attaching to out and err stream? Mostly because
      // Logstream preserves order of written out and err if/when they get written.
      stream.forEachRemaining(logMessage -> write(outputStream, logMessage));
    }

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

    return true;
  }
  catch (DockerException | InterruptedException e) {
    log.error("Failed to exec commands '{}' in container '{}' for image '{}'", commands, shortId, image, e);
  }

  return false;
}
 
Example #19
Source File: TaskRunner.java    From helios with Apache License 2.0 5 votes vote down vote up
private ContainerState getContainerState(final String existingContainerId)
    throws DockerException, InterruptedException {
  final ContainerInfo info = getContainerInfo(existingContainerId);
  if (info == null) {
    return null;
  }
  return info.state();
}
 
Example #20
Source File: DockerUtil.java    From SuitAgent with Apache License 2.0 5 votes vote down vote up
/**
 * 获取主机上所有运行容器的proc信息
 * @return
 */
public static List<ContainerProcInfoToHost> getAllHostContainerProcInfos(){
    String cacheKey = "ALL_HOST_CONTAINER_PROC_INFOS";
    List<ContainerProcInfoToHost> procInfoToHosts = (List<ContainerProcInfoToHost>) getCache(cacheKey);
    if (procInfoToHosts != null){
        //返回缓存数据
        return procInfoToHosts;
    }else {
        procInfoToHosts = new ArrayList<>();
    }
    synchronized (LockForGetAllHostContainerProcInfos){
        try {
            List<Container> containers = getContainers(DockerClient.ListContainersParam.withStatusRunning());
            for (Container container : containers) {
                ContainerInfo info = getContainerInfo(container.id());
                if (info != null) {
                    String pid = String.valueOf(info.state().pid());
                    procInfoToHosts.add(new ContainerProcInfoToHost(container.id(),PROC_HOST_VOLUME + "/" + pid + "/root",pid));
                }
            }
        } catch (Exception e) {
            log.error("",e);
        }
    }
    if (!procInfoToHosts.isEmpty()) {
        //设置缓存
        setCache(cacheKey,procInfoToHosts);
    }
    return procInfoToHosts;
}
 
Example #21
Source File: DockerUtil.java    From SuitAgent with Apache License 2.0 5 votes vote down vote up
/**
 * 获取Java应用容器的应用名称
 * 注:
 * 必须通过docker run命令的-e参数执行应用名,例如 docker run -e "appName=suitAgent"
 * @param containerId
 * 容器id
 * @return
 * 若未指定应用名称或获取失败返回null
 */
public static String getJavaContainerAppName(String containerId) throws InterruptedException {
    String cacheKey = "appName" + containerId;
    String v = (String) getCache(cacheKey);
    if (StringUtils.isNotEmpty(v)) {
        return v;
    }
    try {
        ContainerInfo containerInfo = getContainerInfo(containerId);
        if (containerInfo != null){
            List<String> env = containerInfo.config().env();
            if (env != null){
                for (String s : env) {
                    String[] split = s.split(s.contains("=") ? "=" : ":");
                    if (split.length == 2){
                        String key = split[0];
                        String value = split[1];
                        if ("appName".equals(key)){
                            setCache(cacheKey,value);
                            return value;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("",e);
    }

    return null;
}
 
Example #22
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 #23
Source File: DockerContainerTest.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldStartContainerWithCorrectCommand() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "busybox:latest");
    properties.put("Command", "cat\n/etc/hosts\n/etc/group");

    DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod", jobIdentifier, Collections.EMPTY_MAP), createClusterProfiles(), docker, consoleLogAppender);
    containers.add(container.name());
    ContainerInfo containerInfo = docker.inspectContainer(container.name());
    assertThat(containerInfo.config().cmd(), is(Arrays.asList("cat", "/etc/hosts", "/etc/group")));
    String logs = docker.logs(container.name(), DockerClient.LogsParam.stdout()).readFully();
    assertThat(logs, containsString("127.0.0.1")); // from /etc/hosts
    assertThat(logs, containsString("floppy:x:19:")); // from /etc/group
}
 
Example #24
Source File: DockerContainerTest.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldStartContainerWithAutoregisterEnvironmentVariables() throws Exception {
    Map<String, String> properties = new HashMap<>();
    properties.put("Image", "busybox:latest");

    DockerContainer container = DockerContainer.create(new CreateAgentRequest("key", properties, "prod", jobIdentifier, Collections.EMPTY_MAP), createClusterProfiles(), docker, consoleLogAppender);
    containers.add(container.name());
    ContainerInfo containerInfo = docker.inspectContainer(container.name());
    assertThat(containerInfo.config().env(), hasItem("GO_EA_AUTO_REGISTER_KEY=key"));
    assertThat(containerInfo.config().env(), hasItem("GO_EA_AUTO_REGISTER_ENVIRONMENT=prod"));
    assertThat(containerInfo.config().env(), hasItem("GO_EA_AUTO_REGISTER_ELASTIC_AGENT_ID=" + container.name()));
    assertThat(containerInfo.config().env(), hasItem("GO_EA_AUTO_REGISTER_ELASTIC_PLUGIN_ID=" + Constants.PLUGIN_ID));
}
 
Example #25
Source File: DockerUtil.java    From SuitAgent with Apache License 2.0 5 votes vote down vote up
/**
 * 获取容器主机名
 * @param containerId
 * @return
 */
public static String getHostName(String containerId){
    try {
        ContainerInfo containerInfo = getContainerInfo(containerId);
        if (containerInfo != null) {
            return containerInfo.config().hostname();
        }
    } catch (Exception e) {
        log.error("",e);
    }
    return "";
}
 
Example #26
Source File: DockerUtil.java    From SuitAgent with Apache License 2.0 5 votes vote down vote up
/**
 * 获取容器IP地址
 * @param containerId
 * 容器ID
 * @return
 * 1、获取失败返回null
 * 2、host网络模式直接返回宿主机IP
 */
public static String getContainerIp(String containerId){
    String cacheKey = "containerIp" + containerId;
    String v = (String) getCache(cacheKey);
    if (StringUtils.isNotEmpty(v)) {
        return v;
    }
    try {
        if (isHostNet(containerId)){
            return HostUtil.getHostIp();
        }
        ContainerInfo containerInfo = getContainerInfo(containerId);
        if (containerInfo != null) {
            ImmutableMap<String, AttachedNetwork> networks =  containerInfo.networkSettings().networks();
            if (networks != null && !networks.isEmpty()){
                String ip = networks.get(networks.keySet().asList().get(0)).ipAddress();
                setCache(cacheKey,ip);
                return ip;
            }else {
                log.warn("容器{}无Networks配置",containerInfo.name());
            }
        }
    } catch (Exception e) {
        log.error("",e);
    }

    return null;
}
 
Example #27
Source File: DockerContainer.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
private static JobIdentifier jobIdentifier(ContainerInfo containerInfo) {
    ImmutableMap<String, String> labels = containerInfo.config().labels();
    if (labels == null) {
        return null;
    }
    return JobIdentifier.fromJson(labels.get(JOB_IDENTIFIER_LABEL_KEY));
}
 
Example #28
Source File: DockerContainer.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
private static List<String> extraHosts(ContainerInfo containerInfo) {
    HostConfig hostConfig = containerInfo.hostConfig();
    if (hostConfig != null) {
       return hostConfig.extraHosts();
    }
    return new ArrayList<>();
}
 
Example #29
Source File: DockerContainer.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
private static Map<String, String> parseEnvironmentVariables(ContainerInfo containerInfo) {
    ImmutableList<String> env = containerInfo.config().env();
    Map<String, String> environmentVariables = new HashMap<>();
    if (env != null) {
        env.forEach(e -> {
            String[] keyValue = e.split("=");
            if (keyValue.length == 2) {
                environmentVariables.put(keyValue[0], keyValue[1]);
            } else {
                environmentVariables.put(keyValue[0], null);
            }
        });
    }
    return environmentVariables;
}
 
Example #30
Source File: DockerContainer.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
public AgentStatusReport getAgentStatusReport(DockerClient dockerClient) throws Exception {
    ContainerInfo containerInfo = dockerClient.inspectContainer(id);
    String logs = readLogs(dockerClient);

    return new AgentStatusReport(jobIdentifier(containerInfo), name, containerInfo.created().getTime(),
            containerInfo.config().image(), containerInfo.path(), containerInfo.networkSettings().ipAddress(), logs,
            parseEnvironmentVariables(containerInfo), extraHosts(containerInfo));
}