Java Code Examples for com.spotify.docker.client.DockerClient#removeContainer()

The following examples show how to use com.spotify.docker.client.DockerClient#removeContainer() . 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: RunnablePipelineContainer.java    From repairnator with MIT License 6 votes vote down vote up
/**
 * In case of timeout, we kill the container.
 * @param remove if true, it will remove both the container and the volumes
 */
public void killDockerContainer(DockerClient docker, boolean remove) {
    if (this.containerId == null) {
        LOGGER.error("Error while trying to kill docker container: the container id is not available. Maybe the container is not started yet.");
    } else {
        LOGGER.info("Killing the docker container with id "+containerId+". Forced killing date: "+this.limitDateBeforeKilling);
        try {
            docker.killContainer(containerId);
            if (remove) {
                docker.removeContainer(containerId);
                this.removeVolumes(docker);
            }
            this.poolManager.removeSubmittedRunnablePipelineContainer(this);
            serialize("INTERRUPTED");
        } catch (DockerException|InterruptedException e) {
            LOGGER.error("Error while killing docker container "+containerId, e);
        }
    }

}
 
Example 2
Source File: CheckBranchRunner.java    From repairnator with MIT License 5 votes vote down vote up
private void killDockerContainer(DockerClient docker, String containerId) {
    LOGGER.info("Killing docker container: "+containerId);
    try {
        docker.killContainer(containerId);
        docker.removeContainer(containerId);
    } catch (DockerException|InterruptedException e) {
        LOGGER.error("Error while killing docker container "+containerId, e);
    }

}
 
Example 3
Source File: DockerContainer.java    From docker-elastic-agents-plugin with Apache License 2.0 5 votes vote down vote up
public void terminate(DockerClient docker) throws DockerException, InterruptedException {
    try {
        LOG.debug("Terminating instance " + this.name());
        docker.stopContainer(name, 2);
        docker.removeContainer(name);
    } catch (ContainerNotFoundException ignore) {
        LOG.warn("Cannot terminate a container that does not exist " + name);
    }
}
 
Example 4
Source File: Deployer.java    From MineCloud with ISC License 4 votes vote down vote up
public static boolean deployServer(Server server) {
    Credentials mongoCreds = MineCloud.instance().mongo().credentials();
    Credentials redisCreds = MineCloud.instance().redis().credentials();
    String name = server.type().name() + server.number();
    World defaultWorld = server.type().defaultWorld();
    ContainerConfig config = ContainerConfig.builder()
            .hostname(name)
            .image("minecloud/server")
            .openStdin(true)
            .env(new EnvironmentBuilder()
                    .append("mongo_hosts", mongoCreds.formattedHosts())
                    .append("mongo_username", mongoCreds.username())
                    .append("mongo_password", new String(mongoCreds.password()))
                    .append("mongo_database", mongoCreds.database())

                    .append("redis_host", redisCreds.hosts()[0])
                    .append("redis_password", new String(redisCreds.password()))
                    .append("SERVER_MOD", server.type().mod())
                    .append("DEDICATED_RAM", String.valueOf(server.type().dedicatedRam()))
                    .append("MAX_PLAYERS", String.valueOf(server.type().maxPlayers()))

                    .append("server_id", server.entityId())
                    .append("DEFAULT_WORLD", defaultWorld.name())
                    .append("DEFAULT_WORLD_VERSION", defaultWorld.version())
                    .build())
            .build();

    ContainerCreation creation;

    try {
        DockerClient client = MineCloudDaemon.instance().dockerClient();

        try {
            ContainerInfo info = client.inspectContainer(name);

            if (info.state().running()) {
                client.killContainer(name);
            }

            client.removeContainer(info.id());
        } catch (ContainerNotFoundException ignored) {}

        creation = client.createContainer(config, name);

        client.startContainer(creation.id(), HostConfig.builder()
                .binds("/mnt/minecloud:/mnt/minecloud")
                .publishAllPorts(true)
                .build());
    } catch (InterruptedException | DockerException ex) {
        MineCloud.logger().log(Level.SEVERE, "Was unable to create server with type " + server.type().name(),
                ex);
        return false;
    }

    MineCloud.logger().info("Started server " + server.name()
            + " with container id " + server.containerId());
    return true;
}
 
Example 5
Source File: Deployer.java    From MineCloud with ISC License 4 votes vote down vote up
public static Bungee deployBungeeCord(Network network, BungeeType type) {
    DockerClient client = MineCloudDaemon.instance().dockerClient();
    BungeeRepository repository = MineCloud.instance().mongo().repositoryBy(Bungee.class);
    Node node = MineCloudDaemon.instance().node();
    Bungee bungee = new Bungee();

    if (repository.count("_id", node.publicIp()) > 0) {
        MineCloud.logger().log(Level.WARNING, "Did not create bungee on this node; public ip is already in use");
        return null;
    }

    bungee.setId(node.publicIp());

    Credentials mongoCreds = MineCloud.instance().mongo().credentials();
    Credentials redisCreds = MineCloud.instance().redis().credentials();
    ContainerConfig config = ContainerConfig.builder()
            .image("minecloud/bungee")
            .hostname("bungee" + bungee.publicIp())
            .exposedPorts("25565")
            .openStdin(true)
            .env(new EnvironmentBuilder()
                    .append("mongo_hosts", mongoCreds.formattedHosts())
                    .append("mongo_username", mongoCreds.username())
                    .append("mongo_password", new String(mongoCreds.password()))
                    .append("mongo_database", mongoCreds.database())

                    .append("redis_host", redisCreds.hosts()[0])
                    .append("redis_password", new String(redisCreds.password()))
                    .append("DEDICATED_RAM", String.valueOf(type.dedicatedRam()))

                    .append("bungee_id", node.publicIp())
                    .build())
            .build();
    HostConfig hostConfig = HostConfig.builder()
            .binds("/mnt/minecloud:/mnt/minecloud")
            .portBindings(new HashMap<String, List<PortBinding>>() {{
                put("25565", Arrays.asList(PortBinding.of(node.publicIp(), 25565))); // I'm sorry
            }})
            .publishAllPorts(true)
            .build();

    try {
        ContainerInfo info = client.inspectContainer("bungee");

        if (info.state().running()) {
            client.killContainer("bungee");
        }

        client.removeContainer(info.id());
    } catch (DockerException | InterruptedException ignored) {}

    ContainerCreation creation;

    try {
        creation = client.createContainer(config, type.name());

        client.startContainer(creation.id(), hostConfig);
    } catch (InterruptedException | DockerException ex) {
        MineCloud.logger().log(Level.SEVERE, "Was unable to create bungee with type " + type.name(),
                ex);
        return bungee;
    }

    bungee.setNetwork(network);
    bungee.setNode(node);
    bungee.setPublicIp(node.publicIp());
    bungee.setType(type);

    repository.save(bungee);
    MineCloud.logger().info("Started bungee " + bungee.name() + " with container id " + bungee.containerId());
    return bungee;
}