use of com.spotify.docker.client.messages.ProgressMessage in project docker-client by spotify.
the class DefaultDockerClientTest method testBuildNoTimeout.
@Test
public void testBuildNoTimeout() throws Exception {
// The Dockerfile specifies a sleep of 10s during the build
// Returned image id is last piece of output, so this confirms stream did not timeout
final Path dockerDirectory = getResource("dockerDirectorySleeping");
final String returnedImageId = sut.build(dockerDirectory, "test", new ProgressHandler() {
@Override
public void progress(ProgressMessage message) throws DockerException {
log.info(message.stream());
}
}, BuildParam.noCache());
assertTrue(returnedImageId != null);
}
use of com.spotify.docker.client.messages.ProgressMessage in project zeppelin by apache.
the class DockerInterpreterProcess method start.
@Override
public void start(String userName) throws IOException {
docker = DefaultDockerClient.builder().uri(URI.create(DOCKER_HOST)).build();
removeExistContainer(containerName);
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
// Bind container ports to host ports
int intpServicePort = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
this.dockerIntpServicePort = String.valueOf(intpServicePort);
final String[] ports = { dockerIntpServicePort };
for (String port : ports) {
List<PortBinding> hostPorts = new ArrayList<>();
hostPorts.add(PortBinding.of("0.0.0.0", port));
portBindings.put(port, hostPorts);
}
final HostConfig hostConfig = HostConfig.builder().networkMode("host").portBindings(portBindings).build();
DockerSpecTemplate specTemplate = new DockerSpecTemplate();
specTemplate.loadProperties(getTemplateBindings());
URL urlTemplate = this.getClass().getResource(DOCKER_INTP_JINJA);
String template = Resources.toString(urlTemplate, StandardCharsets.UTF_8);
String dockerCommand = specTemplate.render(template);
int firstLineIsNewline = dockerCommand.indexOf("\n");
if (firstLineIsNewline == 0) {
dockerCommand = dockerCommand.replaceFirst("\n", "");
}
LOGGER.info("dockerCommand = {}", dockerCommand);
List<String> listEnv = getListEnvs();
LOGGER.info("docker listEnv = {}", listEnv);
// check if the interpreter process exit script
// if interpreter process exit, then container need exit
StringBuilder sbStartCmd = new StringBuilder();
sbStartCmd.append("sleep 10; ");
sbStartCmd.append("process=RemoteInterpreterServer; ");
sbStartCmd.append("RUNNING_PIDS=$(ps x | grep $process | grep -v grep | awk '{print $1}'); ");
sbStartCmd.append("while [ ! -z \"$RUNNING_PIDS\" ]; ");
sbStartCmd.append("do sleep 1; ");
sbStartCmd.append("RUNNING_PIDS=$(ps x | grep $process | grep -v grep | awk '{print $1}'); ");
sbStartCmd.append("done");
// Create container with exposed ports
final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).hostname(this.intpEventServerHost).image(containerImage).workingDir("/").env(listEnv).cmd("sh", "-c", sbStartCmd.toString()).build();
try {
LOGGER.info("wait docker pull image {} ...", containerImage);
docker.pull(containerImage, new ProgressHandler() {
@Override
public void progress(ProgressMessage message) throws DockerException {
if (null != message.error()) {
LOGGER.error(message.toString());
}
}
});
final ContainerCreation containerCreation = docker.createContainer(containerConfig, containerName);
this.containerId = containerCreation.id();
// Start container
docker.startContainer(containerId);
copyRunFileToContainer(containerId);
execInContainer(containerId, dockerCommand, false);
} catch (DockerException e) {
LOGGER.error(e.getMessage(), e);
throw new IOException(e.getMessage());
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
throw new IOException(e.getMessage());
}
long startTime = System.currentTimeMillis();
// wait until interpreter send dockerStarted message through thrift rpc
synchronized (dockerStarted) {
if (!dockerStarted.get()) {
try {
dockerStarted.wait(getConnectTimeout());
} catch (InterruptedException e) {
LOGGER.error("Remote interpreter is not accessible");
throw new IOException(e.getMessage());
}
}
}
if (!dockerStarted.get()) {
LOGGER.info("Interpreter docker creation is time out in {} seconds", getConnectTimeout() / 1000);
}
// waits for interpreter thrift rpc server ready
while (System.currentTimeMillis() - startTime < getConnectTimeout()) {
if (RemoteInterpreterUtils.checkIfRemoteEndpointAccessible(getHost(), getPort())) {
break;
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
}
Aggregations