Search in sources :

Example 21 with Container

use of com.spotify.docker.client.messages.Container in project zeppelin by apache.

the class DockerInterpreterProcess method removeExistContainer.

// Because docker can't create a container with the same name, it will cause the creation to fail.
// If the zeppelin service is abnormal and the container that was created is not closed properly,
// the container will not be created again.
private void removeExistContainer(String containerName) {
    boolean isExist = false;
    try {
        final List<Container> containers = docker.listContainers(DockerClient.ListContainersParam.allContainers());
        for (Container container : containers) {
            for (String name : container.names()) {
                // because container name like '/md-shared', so need add '/'
                if (StringUtils.equals(name, "/" + containerName)) {
                    isExist = true;
                    break;
                }
            }
        }
        if (isExist == true) {
            LOGGER.info("kill exist container {}", containerName);
            docker.killContainer(containerName);
        }
    } catch (DockerException | InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        try {
            if (isExist == true) {
                docker.removeContainer(containerName);
            }
        } catch (DockerException | InterruptedException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) Container(com.spotify.docker.client.messages.Container)

Aggregations

Container (com.spotify.docker.client.messages.Container)21 DockerClient (com.spotify.docker.client.DockerClient)13 Test (org.junit.Test)8 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)7 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)7 DockerException (com.spotify.docker.client.exceptions.DockerException)6 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 HostConfig (com.spotify.docker.client.messages.HostConfig)4 Image (com.spotify.docker.client.messages.Image)4 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)3 LogStream (com.spotify.docker.client.LogStream)3 ExecCreation (com.spotify.docker.client.messages.ExecCreation)3 ImageInfo (com.spotify.docker.client.messages.ImageInfo)3 HeliosClient (com.spotify.helios.client.HeliosClient)3 JobId (com.spotify.helios.common.descriptors.JobId)3 IOException (java.io.IOException)3 Integer.toHexString (java.lang.Integer.toHexString)3 ArrayList (java.util.ArrayList)3 IDockerContainer (org.eclipse.linuxtools.docker.core.IDockerContainer)3