Search in sources :

Example 1 with LinkableContainer

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);
}
Also used : Arrays(java.util.Arrays) MinimumDurationRunningStartupCheckStrategy(org.testcontainers.containers.startupcheck.MinimumDurationRunningStartupCheckStrategy) VolumesFrom(com.github.dockerjava.api.model.VolumesFrom) Link(com.github.dockerjava.api.model.Link) DockerClient(com.github.dockerjava.api.DockerClient) IsRunningStartupCheckStrategy(org.testcontainers.containers.startupcheck.IsRunningStartupCheckStrategy) Future(java.util.concurrent.Future) Wait(org.testcontainers.containers.wait.Wait) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) STDOUT(org.testcontainers.containers.output.OutputFrame.OutputType.STDOUT) Duration(java.time.Duration) Map(java.util.Map) ExposedPort(com.github.dockerjava.api.model.ExposedPort) PortBinding(com.github.dockerjava.api.model.PortBinding) WaitStrategyTarget(org.testcontainers.containers.wait.strategy.WaitStrategyTarget) Path(java.nio.file.Path) NonNull(lombok.NonNull) StartupCheckStrategy(org.testcontainers.containers.startupcheck.StartupCheckStrategy) Set(java.util.Set) Description(org.junit.runner.Description) EqualsAndHashCode(lombok.EqualsAndHashCode) InspectContainerResponse(com.github.dockerjava.api.command.InspectContainerResponse) Collectors(java.util.stream.Collectors) WaitStrategy(org.testcontainers.containers.wait.WaitStrategy) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Optional(java.util.Optional) DockerClientFactory(org.testcontainers.DockerClientFactory) NotNull(org.jetbrains.annotations.NotNull) Info(com.github.dockerjava.api.model.Info) Setter(lombok.Setter) Bind(com.github.dockerjava.api.model.Bind) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) AccessLevel(lombok.AccessLevel) RemoteDockerImage(org.testcontainers.images.RemoteDockerImage) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) Charset(java.nio.charset.Charset) FrameConsumerResultCallback(org.testcontainers.containers.output.FrameConsumerResultCallback) CommandLine.runShellCommand(org.testcontainers.utility.CommandLine.runShellCommand) org.testcontainers.utility(org.testcontainers.utility) LinkedHashSet(java.util.LinkedHashSet) Profiler(org.slf4j.profiler.Profiler) Logger(org.slf4j.Logger) IOUtils(org.apache.commons.compress.utils.IOUtils) FileOutputStream(java.io.FileOutputStream) RateLimiterBuilder(org.rnorth.ducttape.ratelimits.RateLimiterBuilder) IOException(java.io.IOException) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Unreliables(org.rnorth.ducttape.unreliables.Unreliables) RateLimiter(org.rnorth.ducttape.ratelimits.RateLimiter) LinkableContainer(org.testcontainers.containers.traits.LinkableContainer) Data(lombok.Data) Volume(com.github.dockerjava.api.model.Volume) OutputFrame(org.testcontainers.containers.output.OutputFrame) Collections(java.util.Collections) STDERR(org.testcontainers.containers.output.OutputFrame.OutputType.STDERR) LinkableContainer(org.testcontainers.containers.traits.LinkableContainer) Bind(com.github.dockerjava.api.model.Bind) VolumesFrom(com.github.dockerjava.api.model.VolumesFrom) ExposedPort(com.github.dockerjava.api.model.ExposedPort) PortBinding(com.github.dockerjava.api.model.PortBinding) Map(java.util.Map) HashMap(java.util.HashMap) Link(com.github.dockerjava.api.model.Link) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

DockerClient (com.github.dockerjava.api.DockerClient)1 CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)1 InspectContainerResponse (com.github.dockerjava.api.command.InspectContainerResponse)1 Bind (com.github.dockerjava.api.model.Bind)1 ExposedPort (com.github.dockerjava.api.model.ExposedPort)1 Info (com.github.dockerjava.api.model.Info)1 Link (com.github.dockerjava.api.model.Link)1 PortBinding (com.github.dockerjava.api.model.PortBinding)1 Volume (com.github.dockerjava.api.model.Volume)1 VolumesFrom (com.github.dockerjava.api.model.VolumesFrom)1 Strings (com.google.common.base.Strings)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1