use of com.github.dockerjava.api.model.PortBinding in project elastest-torm by elastest.
the class DockerComposeService method setup.
public void setup() throws IOException, InterruptedException {
// 1. Start docker-compose-ui container
dockerComposeUiContainerName = dockerService.generateContainerName(dockerComposeUiPrefix);
int dockerComposeBindPort = dockerService.findRandomOpenPort();
Binding bindNoVncPort = bindPort(dockerComposeBindPort);
ExposedPort exposedNoVncPort = tcp(dockerComposeUiPort);
String dockerComposeServiceUrl = "http://" + dockerService.getDockerServerIp() + ":" + dockerComposeBindPort;
log.debug("Starting docker-compose-ui container: {}", dockerComposeUiContainerName);
List<PortBinding> portBindings = asList(new PortBinding(bindNoVncPort, exposedNoVncPort));
Volume volume = new Volume(dockerDefaultSocket);
List<Volume> volumes = asList(volume);
List<Bind> binds = asList(new Bind(dockerDefaultSocket, volume));
DockerBuilder dockerBuilder = dockerBuilder(dockerComposeUiImageId, dockerComposeUiContainerName).portBindings(portBindings).volumes(volumes).binds(binds);
dockerService.startAndWaitContainer(dockerBuilder.build());
// 2. Create Retrofit object to call docker-compose-ui REST API
OkHttpClient okHttpClient = new OkHttpClient.Builder().readTimeout(dockerComposeTimeout, SECONDS).connectTimeout(dockerComposeTimeout, SECONDS).build();
Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).baseUrl(dockerComposeServiceUrl).build();
dockerComposeApi = retrofit.create(DockerComposeApi.class);
log.debug("docker-compose-ui up and running on URL: {}", dockerComposeServiceUrl);
// 3.Delete default example projects
dockerService.waitForHostIsReachable(dockerComposeServiceUrl);
String[] defaultProjects = { "env-demo", "hello-node", "node-redis", "volumes-demo", "volumes-relative-paths" };
removeProjects(defaultProjects);
}
use of com.github.dockerjava.api.model.PortBinding in project elastest-torm by elastest.
the class DockerUnitTest method testEmptyContainer.
@Test
@DisplayName("Try to start a container with invalid input")
void testEmptyContainer() {
assertThrows(Exception.class, () -> {
Binding bindPort = bindPort(0);
ExposedPort exposedPort = tcp(0);
List<PortBinding> portBindings = asList(new PortBinding(bindPort, exposedPort));
dockerService.startAndWaitContainer(dockerBuilder("", "").portBindings(portBindings).build());
});
}
use of com.github.dockerjava.api.model.PortBinding in project testcontainers-java by testcontainers.
the class GenericContainer method applyConfiguration.
private void applyConfiguration(CreateContainerCmd createCommand) {
// Set up exposed ports (where there are no host port bindings defined)
ExposedPort[] portArray = exposedPorts.stream().map(ExposedPort::new).toArray(ExposedPort[]::new);
createCommand.withExposedPorts(portArray);
// Set up exposed ports that need host port bindings
PortBinding[] portBindingArray = portBindings.stream().map(PortBinding::parse).toArray(PortBinding[]::new);
createCommand.withPortBindings(portBindingArray);
if (commandParts != null) {
createCommand.withCmd(commandParts);
}
String[] envArray = env.entrySet().stream().map(it -> it.getKey() + "=" + it.getValue()).toArray(String[]::new);
createCommand.withEnv(envArray);
Bind[] bindsArray = binds.stream().toArray(Bind[]::new);
createCommand.withBinds(bindsArray);
VolumesFrom[] volumesFromsArray = volumesFroms.stream().toArray(VolumesFrom[]::new);
createCommand.withVolumesFrom(volumesFromsArray);
Set<Link> allLinks = new HashSet<>();
Set<String> allLinkedContainerNetworks = new HashSet<>();
for (Map.Entry<String, LinkableContainer> linkEntries : linkedContainers.entrySet()) {
String alias = linkEntries.getKey();
LinkableContainer linkableContainer = linkEntries.getValue();
Set<Link> links = findLinksFromThisContainer(alias, linkableContainer);
allLinks.addAll(links);
if (allLinks.size() == 0) {
throw new ContainerLaunchException("Aborting attempt to link to container " + linkableContainer.getContainerName() + " as it is not running");
}
Set<String> linkedContainerNetworks = findAllNetworksForLinkedContainers(linkableContainer);
allLinkedContainerNetworks.addAll(linkedContainerNetworks);
}
createCommand.withLinks(allLinks.toArray(new Link[allLinks.size()]));
allLinkedContainerNetworks.remove("bridge");
if (allLinkedContainerNetworks.size() > 1) {
logger().warn("Container needs to be on more than one custom network to link to other " + "containers - this is not currently supported. Required networks are: {}", allLinkedContainerNetworks);
}
Optional<String> networkForLinks = allLinkedContainerNetworks.stream().findFirst();
if (networkForLinks.isPresent()) {
logger().debug("Associating container with network: {}", networkForLinks.get());
createCommand.withNetworkMode(networkForLinks.get());
}
createCommand.withPublishAllPorts(true);
String[] extraHostsArray = extraHosts.stream().toArray(String[]::new);
createCommand.withExtraHosts(extraHostsArray);
if (network != null) {
createCommand.withNetworkMode(network.getId());
createCommand.withAliases(this.networkAliases);
} else if (networkMode != null) {
createCommand.withNetworkMode(networkMode);
}
if (workingDirectory != null) {
createCommand.withWorkingDir(workingDirectory);
}
if (privilegedMode) {
createCommand.withPrivileged(privilegedMode);
}
createContainerCmdModifiers.forEach(hook -> hook.accept(createCommand));
Map<String, String> labels = createCommand.getLabels();
labels = new HashMap<>(labels != null ? labels : Collections.emptyMap());
labels.putAll(DockerClientFactory.DEFAULT_LABELS);
createCommand.withLabels(labels);
}
use of com.github.dockerjava.api.model.PortBinding in project elastest-torm by elastest.
the class DockerIntegrationTest method testDocker.
@Test
@DisplayName("Ask for Chrome to Docker")
void testDocker() throws Exception {
// Test data (input)
String imageId = "selenium/standalone-chrome-debug:3.5.3";
log.debug("Starting Hub with image {}", imageId);
String containerName = dockerService.generateContainerName("hub-for-test-");
Binding bindPort = bindPort(dockerService.findRandomOpenPort());
ExposedPort exposedPort = tcp(4444);
Binding bindHubVncPort = bindPort(dockerService.findRandomOpenPort());
ExposedPort exposedHubVncPort = tcp(5900);
List<PortBinding> portBindings = asList(new PortBinding(bindPort, exposedPort), new PortBinding(bindHubVncPort, exposedHubVncPort));
DockerBuilder dockerBuilder = dockerBuilder(imageId, containerName).portBindings(portBindings);
dockerService.startAndWaitContainer(dockerBuilder.build());
// Assertions
assertTrue(dockerService.existsContainer(containerName));
// Tear down
log.debug("Stoping Hub");
dockerService.stopAndRemoveContainer(containerName);
}
Aggregations