use of com.spotify.docker.client.messages.PortBinding in project azure-tools-for-java by Microsoft.
the class DockerUtil method createContainer.
/**
* create container with specified ImageName:TagName.
*/
@NotNull
public static String createContainer(@NotNull DockerClient docker, @NotNull String imageNameWithTag, @NotNull String containerServerPort) throws DockerException, InterruptedException {
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
List<PortBinding> randomPort = new ArrayList<>();
PortBinding randomBinding = PortBinding.randomPort("0.0.0.0");
randomPort.add(randomBinding);
portBindings.put(containerServerPort, randomPort);
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
final ContainerConfig config = ContainerConfig.builder().hostConfig(hostConfig).image(imageNameWithTag).exposedPorts(containerServerPort).build();
final ContainerCreation container = docker.createContainer(config);
return container.id();
}
use of com.spotify.docker.client.messages.PortBinding in project flink by apache.
the class GCloudEmulatorManager method launchDocker.
public static void launchDocker() throws DockerException, InterruptedException, DockerCertificateException {
// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
docker = DefaultDockerClient.fromEnv().build();
terminateAndDiscardAnyExistingContainers(true);
LOG.info("");
LOG.info("/===========================================");
LOG.info("| GCloud Emulator");
ContainerInfo containerInfo;
String id;
try {
docker.inspectImage(DOCKER_IMAGE_NAME);
} catch (ImageNotFoundException e) {
// No such image so we must download it first.
LOG.info("| - Getting docker image \"{}\"", DOCKER_IMAGE_NAME);
docker.pull(DOCKER_IMAGE_NAME, message -> {
if (message.id() != null && message.progress() != null) {
LOG.info("| - Downloading > {} : {}", message.id(), message.progress());
}
});
}
// No such container. Good, we create one!
LOG.info("| - Creating new container");
// Bind container ports to host ports
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
portBindings.put(INTERNAL_PUBSUB_PORT, Collections.singletonList(PortBinding.randomPort("0.0.0.0")));
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
// Create new container with exposed ports
final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).exposedPorts(INTERNAL_PUBSUB_PORT).image(DOCKER_IMAGE_NAME).cmd("sh", "-c", "mkdir -p /opt/data/pubsub ; gcloud beta emulators pubsub start --data-dir=/opt/data/pubsub --host-port=0.0.0.0:" + INTERNAL_PUBSUB_PORT).build();
LOG.debug("Launching container with configuration {}", containerConfig);
final ContainerCreation creation = docker.createContainer(containerConfig, CONTAINER_NAME_JUNIT);
id = creation.id();
containerInfo = docker.inspectContainer(id);
if (!containerInfo.state().running()) {
LOG.warn("| - Starting it up ....");
docker.startContainer(id);
Thread.sleep(1000);
}
containerInfo = docker.inspectContainer(id);
dockerIpAddress = "127.0.0.1";
Map<String, List<PortBinding>> ports = containerInfo.networkSettings().ports();
assertNotNull("Unable to retrieve the ports where to connect to the emulators", ports);
assertEquals("We expect 1 port to be mapped", 1, ports.size());
pubsubPort = getPort(ports, INTERNAL_PUBSUB_PORT, "PubSub");
LOG.info("| Waiting for the emulators to be running");
// PubSub exposes an "Ok" at the root url when running.
if (!waitForOkStatus("PubSub", pubsubPort)) {
// Oops, we did not get an "Ok" within 10 seconds
startHasFailedKillEverything();
}
LOG.info("\\===========================================");
LOG.info("");
}
use of com.spotify.docker.client.messages.PortBinding in project shinyproxy by openanalytics.
the class DockerEngineBackend method prepareProxy.
@Override
protected void prepareProxy(DockerContainerProxy proxy, ContainerProxyRequest request) throws Exception {
Builder hostConfigBuilder = HostConfig.builder();
List<PortBinding> portBindings = Collections.emptyList();
if (proxy.getPort() > 0)
portBindings = Collections.singletonList(PortBinding.of("0.0.0.0", proxy.getPort()));
hostConfigBuilder.portBindings(Collections.singletonMap(String.valueOf(getAppPort(proxy)), portBindings));
Optional.ofNullable(Utils.memoryToBytes(request.app.getDockerMemory())).ifPresent(l -> hostConfigBuilder.memory(l));
Optional.ofNullable(request.app.getDockerNetwork()).ifPresent(n -> hostConfigBuilder.networkMode(request.app.getDockerNetwork()));
hostConfigBuilder.dns(request.app.getDockerDns());
hostConfigBuilder.binds(getBindVolumes(request.app));
hostConfigBuilder.privileged(Boolean.valueOf(getProperty(PROPERTY_PRIVILEGED, request.app, DEFAULT_PRIVILEGED)));
ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfigBuilder.build()).image(request.app.getDockerImage()).exposedPorts(String.valueOf(getAppPort(proxy))).cmd(request.app.getDockerCmd()).env(buildEnv(request.userId, request.app)).build();
ContainerCreation container = dockerClient.createContainer(containerConfig);
proxy.setContainerId(container.id());
}
use of com.spotify.docker.client.messages.PortBinding 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