use of org.testcontainers.containers.traits.LinkableContainer 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);
}
Aggregations