DockerUtil.java 3.46 KB
Newer Older
Lukas Wiest's avatar
Lukas Wiest committed
1
2
3
4
package de.hftstuttgart.utils;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
5
import com.github.dockerjava.api.exception.DockerException;
Lukas Wiest's avatar
Lukas Wiest committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class DockerUtil {
    private static final Logger LOG = LogManager.getLogger(DockerUtil.class);

    private DockerClient dockerClient;

    public DockerUtil(Environment env) {
        LOG.info("initializing Docker tools");
        LOG.debug("create docker client config");
        DockerClientConfig dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder().build();

        LOG.debug("create docker http client");
        DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
            .dockerHost(dockerClientConfig.getDockerHost())
            .sslConfig(dockerClientConfig.getSSLConfig())
            .build();

        LOG.debug("create docker client");
        dockerClient = DockerClientImpl.getInstance(dockerClientConfig, httpClient);
    }

    public int runContainer(String image, Bind... binds) throws InterruptedException, IOException
    {
        LOG.debug(String.format("pull image: %s", image));
44
45
46
47
48
49
50
51
52
53
54
        try {
            dockerClient.pullImageCmd(image)
                .start()
                .awaitCompletion()
                .close();
        } catch (DockerException e) {
            LOG.error(String.format(
                "pulling docker image %s failed with %s, trying with local image",
                image,
                e.getMessage()));
        }
Lukas Wiest's avatar
Lukas Wiest committed
55
56

        LOG.debug("creating container");
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        CreateContainerResponse containerResponse;
        try {
            containerResponse = dockerClient.createContainerCmd("testcontainer")
                .withImage(image)
                .withHostConfig(
                    HostConfig.newHostConfig()
                        .withBinds(binds))
                .exec();
        } catch (DockerException e) {
            LOG.error(String.format(
                "Creating Docker Testrunner container failed with %s", e.getMessage()));
            throw e;
        }

Lukas Wiest's avatar
Lukas Wiest committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        LOG.debug(String.format("container created: %s", containerResponse.getId()));

        LOG.debug(String.format("starting container %s", containerResponse.getId()));
        dockerClient.startContainerCmd(containerResponse.getId()).exec();

        LOG.debug(String.format("waiting for completion of container %s", containerResponse.getId()));
        int ret = dockerClient
            .waitContainerCmd(containerResponse.getId())
            .start()
            .awaitCompletion()
            .awaitStatusCode();
        LOG.debug(String.format("container completed with status %d", ret));

        LOG.debug(String.format("deleting container %s", containerResponse.getId()));
        dockerClient.removeContainerCmd(containerResponse.getId())
            .withRemoveVolumes(true)
            .exec();

        return ret;
    }
}