private static HashMap<String, String> labelsFrom(CreateAgentRequest request) {
    HashMap<String, String> labels = new HashMap<>();

    labels.put(CREATED_BY_LABEL_KEY, Constants.PLUGIN_ID);
    if (StringUtils.isNotBlank(request.environment())) {
      labels.put(ENVIRONMENT_LABEL_KEY, request.environment());
    }
    labels.put(CONFIGURATION_LABEL_KEY, GSON.toJson(request.properties()));
    return labels;
  }
  private static List<String> environmentFrom(
      CreateAgentRequest request, PluginSettings settings, String containerName) {
    Set<String> env = new HashSet<>();

    env.addAll(settings.getEnvironmentVariables());
    env.addAll(splitIntoLinesAndTrimSpaces(request.properties().get("Environment")));

    env.addAll(
        Arrays.asList("GO_EA_MODE=" + mode(), "GO_EA_SERVER_URL=" + settings.getGoServerUrl()));

    env.addAll(request.autoregisterPropertiesAsEnvironmentVars(containerName));

    return new ArrayList<>(env);
  }
  public static DockerContainer create(
      CreateAgentRequest request, PluginSettings settings, DockerClient docker)
      throws InterruptedException, DockerException, IOException {
    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);
    } catch (ImageNotFoundException ex) {
      LOG.info("Image " + imageName + " not found, attempting to download.");
      docker.pull(imageName);
    }

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

    ContainerConfig containerConfig =
        containerConfigBuilder.image(imageName).labels(labels).env(env).build();
    ContainerCreation container = docker.createContainer(containerConfig, containerName);
    String id = container.id();

    ContainerInfo containerInfo = docker.inspectContainer(id);

    LOG.debug("Created container " + containerName);
    docker.startContainer(containerName);

    return new DockerContainer(
        containerName, containerInfo.created(), request.properties(), request.environment());
  }