Commit c47948b0 authored by mamunozgil's avatar mamunozgil
Browse files

Added volumes for the backend host

No related merge requests found
Pipeline #10761 passed with stage
in 18 seconds
Showing with 214 additions and 233 deletions
+214 -233
......@@ -5,6 +5,9 @@ import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Mount;
import com.github.dockerjava.api.model.MountType;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
......@@ -15,6 +18,8 @@ import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;
@Component
public class DockerUtil {
......@@ -37,7 +42,7 @@ public class DockerUtil {
dockerClient = DockerClientImpl.getInstance(dockerClientConfig, httpClient);
}
public int runContainer(String image, Bind... binds) throws InterruptedException, IOException
public int runContainerWithBinds(String image, Bind... binds) throws InterruptedException, IOException
{
LOG.debug(String.format("pull image: %s", image));
try {
......@@ -87,4 +92,62 @@ public class DockerUtil {
return ret;
}
public int runContainerWithVolumes(String image, Volume... volumes) throws InterruptedException, IOException {
LOG.debug(String.format("Pulling image: %s", image));
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()));
}
LOG.debug("Creating container");
CreateContainerResponse containerResponse;
try {
// Prepare the host configuration with volumes
HostConfig hostConfig = HostConfig.newHostConfig()
.withMounts(
Arrays.stream(volumes)
.map(volume -> new Mount().withTarget(volume.getPath()).withType(MountType.VOLUME))
.collect(Collectors.toList())
);
// Create the container with the configured volumes
containerResponse = dockerClient.createContainerCmd("testcontainer")
.withImage(image)
.withHostConfig(hostConfig)
.exec();
} catch (DockerException e) {
LOG.error(String.format(
"Creating Docker Testrunner container failed with %s", e.getMessage()));
throw e;
}
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;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment