Search in sources :

Example 1 with UnixSystem

use of com.sun.security.auth.module.UnixSystem in project engineblock by engineblock.

the class DockerMetricsHelper method startDocker.

private String startDocker(String IMG, String tag, String name, List<Integer> ports, List<String> volumeDescList, List<String> envList, List<String> cmdList, String reload) {
    ListContainersCmd listContainersCmd = dockerClient.listContainersCmd().withStatusFilter(List.of("exited"));
    listContainersCmd.getFilters().put("name", Arrays.asList(name));
    List<Container> stoppedContainers = null;
    try {
        stoppedContainers = listContainersCmd.exec();
        for (Container stoppedContainer : stoppedContainers) {
            String id = stoppedContainer.getId();
            logger.info("Removing exited container: " + id);
            dockerClient.removeContainerCmd(id).exec();
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Unable to contact docker, make sure docker is up and try again.");
        logger.error("If docker is installed make sure this user has access to the docker group.");
        logger.error("$ sudo gpasswd -a ${USER} docker && newgrp docker");
        System.exit(1);
    }
    Container containerId = searchContainer(name, reload);
    if (containerId != null) {
        return containerId.getId();
    }
    Info info = dockerClient.infoCmd().exec();
    dockerClient.buildImageCmd();
    String term = IMG.split("/")[1];
    // List<SearchItem> dockerSearch = dockerClient.searchImagesCmd(term).exec();
    List<Image> dockerList = dockerClient.listImagesCmd().withImageNameFilter(IMG).exec();
    if (dockerList.size() == 0) {
        dockerClient.pullImageCmd(IMG).withTag(tag).exec(new PullImageResultCallback()).awaitSuccess();
        dockerList = dockerClient.listImagesCmd().withImageNameFilter(IMG).exec();
        if (dockerList.size() == 0) {
            logger.error(String.format("Image %s not found, unable to automatically pull image." + " Check `docker images`", IMG));
            System.exit(1);
        }
    }
    logger.info("Search returned" + dockerList.toString());
    List<ExposedPort> tcpPorts = new ArrayList<>();
    List<PortBinding> portBindings = new ArrayList<>();
    for (Integer port : ports) {
        ExposedPort tcpPort = ExposedPort.tcp(port);
        Ports.Binding binding = new Ports.Binding("0.0.0.0", String.valueOf(port));
        PortBinding pb = new PortBinding(binding, tcpPort);
        tcpPorts.add(tcpPort);
        portBindings.add(pb);
    }
    List<Volume> volumeList = new ArrayList<>();
    List<Bind> volumeBindList = new ArrayList<>();
    for (String volumeDesc : volumeDescList) {
        String volFrom = volumeDesc.split(":")[0];
        String volTo = volumeDesc.split(":")[1];
        Volume vol = new Volume(volTo);
        volumeList.add(vol);
        volumeBindList.add(new Bind(volFrom, vol));
    }
    CreateContainerResponse containerResponse;
    if (envList == null) {
        containerResponse = dockerClient.createContainerCmd(IMG + ":" + tag).withCmd(cmdList).withExposedPorts(tcpPorts).withHostConfig(new HostConfig().withPortBindings(portBindings).withPublishAllPorts(true).withBinds(volumeBindList)).withName(name).exec();
    } else {
        long user = new UnixSystem().getUid();
        containerResponse = dockerClient.createContainerCmd(IMG + ":" + tag).withEnv(envList).withExposedPorts(tcpPorts).withHostConfig(new HostConfig().withPortBindings(portBindings).withPublishAllPorts(true).withBinds(volumeBindList)).withName(name).withUser("" + user).exec();
    }
    dockerClient.startContainerCmd(containerResponse.getId()).exec();
    return containerResponse.getId();
}
Also used : ListContainersCmd(com.github.dockerjava.api.command.ListContainersCmd) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) UnixSystem(com.sun.security.auth.module.UnixSystem) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback)

Aggregations

CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 ListContainersCmd (com.github.dockerjava.api.command.ListContainersCmd)1 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)1 UnixSystem (com.sun.security.auth.module.UnixSystem)1