Skip to content

Commit 6a5e6a7

Browse files
committed
get rid of docker link
1 parent 5323acd commit 6a5e6a7

File tree

9 files changed

+14
-164
lines changed

9 files changed

+14
-164
lines changed

core/src/main/java/org/testcontainers/containers/ComposeDelegate.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ public void withExposedService(String serviceName, int servicePort, @NonNull Wai
282282
/*
283283
* For every service/port pair that needs to be exposed, we register a target on an 'ambassador container'.
284284
*
285-
* The ambassador container's role is to link (within the Docker network) to one of the
286-
* compose services, and proxy TCP network I/O out to a port that the ambassador container
285+
* The ambassador container's role is to be on the same Docker network as the compose
286+
* services, and proxy TCP network I/O out to a port that the ambassador container
287287
* exposes.
288288
*
289289
* This avoids the need for the docker compose file to explicitly expose ports on all the
@@ -299,10 +299,7 @@ public void withExposedService(String serviceName, int servicePort, @NonNull Wai
299299
.computeIfAbsent(serviceInstanceName, __ -> new ConcurrentHashMap<>())
300300
.put(servicePort, ambassadorPort);
301301
ambassadorContainer.withTarget(ambassadorPort, serviceInstanceName, servicePort);
302-
ambassadorContainer.addLink(
303-
new FutureContainer(this.project + this.composeSeparator + serviceInstanceName),
304-
serviceInstanceName
305-
);
302+
ambassadorContainer.withNetworkMode(this.project + "_default");
306303
addWaitStrategy(serviceInstanceName, waitStrategy);
307304
}
308305

core/src/main/java/org/testcontainers/containers/Container.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import lombok.Value;
88
import org.testcontainers.containers.output.OutputFrame;
99
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
10-
import org.testcontainers.containers.traits.LinkableContainer;
1110
import org.testcontainers.containers.wait.strategy.WaitStrategy;
1211
import org.testcontainers.images.ImagePullPolicy;
1312
import org.testcontainers.images.builder.Transferable;
@@ -22,7 +21,7 @@
2221
import java.util.function.Consumer;
2322
import java.util.function.Function;
2423

25-
public interface Container<SELF extends Container<SELF>> extends LinkableContainer, ContainerState {
24+
public interface Container<SELF extends Container<SELF>> extends ContainerState {
2625
/**
2726
* @return a reference to this container instance, cast to the expected generic type.
2827
*/
@@ -97,16 +96,6 @@ default void addFileSystemBind(final String hostPath, final String containerPath
9796
@Deprecated
9897
void addFileSystemBind(String hostPath, String containerPath, BindMode mode, SelinuxContext selinuxContext);
9998

100-
/**
101-
* Add a link to another container.
102-
*
103-
* @param otherContainer the other container object to link to
104-
* @param alias the alias (for the other container) that this container should be able to use
105-
* @deprecated Links are deprecated (see <a href="https://github.com/testcontainers/testcontainers-java/issues/465">#465</a>). Please use {@link Network} features instead.
106-
*/
107-
@Deprecated
108-
void addLink(LinkableContainer otherContainer, String alias);
109-
11099
/**
111100
* Add an exposed port. Consider using {@link #withExposedPorts(Integer...)}
112101
* for building a container in a fluent style.
@@ -453,12 +442,6 @@ default void followOutput(Consumer<OutputFrame> consumer, OutputFrame.OutputType
453442

454443
List<Bind> getBinds();
455444

456-
/**
457-
* @deprecated Links are deprecated (see <a href="https://github.com/testcontainers/testcontainers-java/issues/465">#465</a>). Please use {@link Network} features instead.
458-
*/
459-
@Deprecated
460-
Map<String, LinkableContainer> getLinkedContainers();
461-
462445
void setExposedPorts(List<Integer> exposedPorts);
463446

464447
void setPortBindings(List<String> portBindings);
@@ -473,11 +456,5 @@ default void followOutput(Consumer<OutputFrame> consumer, OutputFrame.OutputType
473456

474457
void setBinds(List<Bind> binds);
475458

476-
/**
477-
* @deprecated Links are deprecated (see <a href="https://github.com/testcontainers/testcontainers-java/issues/465">#465</a>). Please use {@link Network} features instead.
478-
*/
479-
@Deprecated
480-
void setLinkedContainers(Map<String, LinkableContainer> linkedContainers);
481-
482459
void setWaitStrategy(WaitStrategy waitStrategy);
483460
}

core/src/main/java/org/testcontainers/containers/ContainerState.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ default String getContainerId() {
233233
return getContainerInfo().getId();
234234
}
235235

236+
/**
237+
* @return the name of the container
238+
*/
239+
default String getContainerName() {
240+
return getContainerInfo().getName();
241+
}
242+
236243
/**
237244
* Returns the container inspect response. The response might be cached/outdated.
238245
*

core/src/main/java/org/testcontainers/containers/FutureContainer.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.github.dockerjava.api.model.ContainerNetwork;
1212
import com.github.dockerjava.api.model.ExposedPort;
1313
import com.github.dockerjava.api.model.HostConfig;
14-
import com.github.dockerjava.api.model.Link;
1514
import com.github.dockerjava.api.model.PortBinding;
1615
import com.github.dockerjava.api.model.Ports;
1716
import com.github.dockerjava.api.model.Volume;
@@ -40,7 +39,6 @@
4039
import org.testcontainers.containers.startupcheck.IsRunningStartupCheckStrategy;
4140
import org.testcontainers.containers.startupcheck.MinimumDurationRunningStartupCheckStrategy;
4241
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
43-
import org.testcontainers.containers.traits.LinkableContainer;
4442
import org.testcontainers.containers.wait.strategy.Wait;
4543
import org.testcontainers.containers.wait.strategy.WaitStrategy;
4644
import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;
@@ -72,7 +70,6 @@
7270
import java.util.ArrayList;
7371
import java.util.Arrays;
7472
import java.util.Collections;
75-
import java.util.HashMap;
7673
import java.util.HashSet;
7774
import java.util.Iterator;
7875
import java.util.LinkedHashMap;
@@ -124,13 +121,6 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
124121
@NonNull
125122
private List<VolumesFrom> volumesFroms = new ArrayList<>();
126123

127-
/**
128-
* @deprecated Links are deprecated (see <a href="https://github.com/testcontainers/testcontainers-java/issues/465">#465</a>). Please use {@link Network} features instead.
129-
*/
130-
@NonNull
131-
@Deprecated
132-
private Map<String, LinkableContainer> linkedContainers = new HashMap<>();
133-
134124
private StartupCheckStrategy startupCheckStrategy = new IsRunningStartupCheckStrategy();
135125

136126
private int startupAttempts = 1;
@@ -781,45 +771,6 @@ private void applyConfiguration(CreateContainerCmd createCommand) {
781771
VolumesFrom[] volumesFromsArray = volumesFroms.stream().toArray(VolumesFrom[]::new);
782772
createCommand.withVolumesFrom(volumesFromsArray);
783773

784-
Set<Link> allLinks = new HashSet<>();
785-
Set<String> allLinkedContainerNetworks = new HashSet<>();
786-
for (Entry<String, LinkableContainer> linkEntries : linkedContainers.entrySet()) {
787-
String alias = linkEntries.getKey();
788-
LinkableContainer linkableContainer = linkEntries.getValue();
789-
790-
Set<Link> links = findLinksFromThisContainer(alias, linkableContainer);
791-
allLinks.addAll(links);
792-
793-
if (allLinks.size() == 0) {
794-
throw new ContainerLaunchException(
795-
"Aborting attempt to link to container " +
796-
linkableContainer.getContainerName() +
797-
" as it is not running"
798-
);
799-
}
800-
801-
Set<String> linkedContainerNetworks = findAllNetworksForLinkedContainers(linkableContainer);
802-
allLinkedContainerNetworks.addAll(linkedContainerNetworks);
803-
}
804-
805-
createCommand.withLinks(allLinks.toArray(new Link[allLinks.size()]));
806-
807-
allLinkedContainerNetworks.remove("bridge");
808-
if (allLinkedContainerNetworks.size() > 1) {
809-
logger()
810-
.warn(
811-
"Container needs to be on more than one custom network to link to other " +
812-
"containers - this is not currently supported. Required networks are: {}",
813-
allLinkedContainerNetworks
814-
);
815-
}
816-
817-
Optional<String> networkForLinks = allLinkedContainerNetworks.stream().findFirst();
818-
if (networkForLinks.isPresent()) {
819-
logger().debug("Associating container with network: {}", networkForLinks.get());
820-
createCommand.withNetworkMode(networkForLinks.get());
821-
}
822-
823774
if (hostAccessible) {
824775
PortForwardingContainer.INSTANCE.start();
825776
}
@@ -841,32 +792,6 @@ private void applyConfiguration(CreateContainerCmd createCommand) {
841792
}
842793
}
843794

844-
private Set<Link> findLinksFromThisContainer(String alias, LinkableContainer linkableContainer) {
845-
return dockerClient
846-
.listContainersCmd()
847-
.withStatusFilter(Arrays.asList("running"))
848-
.exec()
849-
.stream()
850-
.flatMap(container -> Stream.of(container.getNames()))
851-
.filter(name -> name.endsWith(linkableContainer.getContainerName()))
852-
.map(name -> new Link(name, alias))
853-
.collect(Collectors.toSet());
854-
}
855-
856-
private Set<String> findAllNetworksForLinkedContainers(LinkableContainer linkableContainer) {
857-
return dockerClient
858-
.listContainersCmd()
859-
.exec()
860-
.stream()
861-
.filter(container -> container.getNames()[0].endsWith(linkableContainer.getContainerName()))
862-
.filter(container -> {
863-
return container.getNetworkSettings() != null && container.getNetworkSettings().getNetworks() != null;
864-
})
865-
.flatMap(container -> container.getNetworkSettings().getNetworks().keySet().stream())
866-
.distinct()
867-
.collect(Collectors.toSet());
868-
}
869-
870795
/**
871796
* {@inheritDoc}
872797
*/
@@ -1029,15 +954,6 @@ private void addVolumesFrom(Container container, BindMode mode) {
1029954
volumesFroms.add(new VolumesFrom(container.getContainerName(), mode.accessMode));
1030955
}
1031956

1032-
/**
1033-
* @deprecated Links are deprecated (see <a href="https://github.com/testcontainers/testcontainers-java/issues/465">#465</a>). Please use {@link Network} features instead.
1034-
*/
1035-
@Deprecated
1036-
@Override
1037-
public void addLink(LinkableContainer otherContainer, String alias) {
1038-
this.linkedContainers.put(alias, otherContainer);
1039-
}
1040-
1041957
@Override
1042958
public void addExposedPort(Integer port) {
1043959
this.containerDef.addExposedTcpPort(port);

core/src/main/java/org/testcontainers/containers/traits/LinkableContainer.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.apache.commons.lang3.StringUtils;
77
import org.jetbrains.annotations.NotNull;
88
import org.jetbrains.annotations.Nullable;
9-
import org.testcontainers.containers.traits.LinkableContainer;
109
import org.testcontainers.delegate.DatabaseDelegate;
1110
import org.testcontainers.ext.ScriptUtils;
1211
import org.testcontainers.jdbc.JdbcDatabaseDelegate;
@@ -31,9 +30,7 @@
3130
/**
3231
* Base class for containers that expose a JDBC connection
3332
*/
34-
public abstract class JdbcDatabaseContainer<SELF extends JdbcDatabaseContainer<SELF>>
35-
extends GenericContainer<SELF>
36-
implements LinkableContainer {
33+
public abstract class JdbcDatabaseContainer<SELF extends JdbcDatabaseContainer<SELF>> extends GenericContainer<SELF> {
3734

3835
private static final Object DRIVER_LOAD_MUTEX = new Object();
3936

modules/nginx/src/main/java/org/testcontainers/containers/NginxContainer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.testcontainers.containers;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.testcontainers.containers.traits.LinkableContainer;
54
import org.testcontainers.utility.DockerImageName;
65

76
import java.net.MalformedURLException;
@@ -12,9 +11,7 @@
1211
* @deprecated use {@link org.testcontainers.nginx.NginxContainer} instead.
1312
*/
1413
@Deprecated
15-
public class NginxContainer<SELF extends NginxContainer<SELF>>
16-
extends GenericContainer<SELF>
17-
implements LinkableContainer {
14+
public class NginxContainer<SELF extends NginxContainer<SELF>> extends GenericContainer<SELF> {
1815

1916
private static final int NGINX_DEFAULT_PORT = 80;
2017

modules/selenium/src/main/java/org/testcontainers/containers/BrowserWebDriverContainer.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
2020
import org.testcontainers.containers.VncRecordingContainer.VncRecordingFormat;
21-
import org.testcontainers.containers.traits.LinkableContainer;
2221
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
2322
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
2423
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
@@ -52,7 +51,7 @@
5251
@Deprecated
5352
public class BrowserWebDriverContainer<SELF extends BrowserWebDriverContainer<SELF>>
5453
extends GenericContainer<SELF>
55-
implements LinkableContainer, TestLifecycleAware {
54+
implements TestLifecycleAware {
5655

5756
private static final DockerImageName CHROME_IMAGE = DockerImageName.parse("selenium/standalone-chrome");
5857

@@ -400,22 +399,6 @@ private void retainRecordingIfNeeded(String prefix, boolean succeeded) {
400399
}
401400
}
402401

403-
/**
404-
* Remember any other containers this needs to link to. We have to pass these down to the container so that
405-
* the other containers will be initialized before linking occurs.
406-
*
407-
* @param otherContainer the container rule to link to
408-
* @param alias the alias (hostname) that this other container should be referred to by
409-
* @return this
410-
*
411-
* @deprecated Links are deprecated (see <a href="https://github.com/testcontainers/testcontainers-java/issues/465">#465</a>). Please use {@link Network} features instead.
412-
*/
413-
@Deprecated
414-
public SELF withLinkToContainer(LinkableContainer otherContainer, String alias) {
415-
addLink(otherContainer, alias);
416-
return self();
417-
}
418-
419402
public SELF withRecordingMode(VncRecordingMode recordingMode, File vncRecordingDirectory) {
420403
return withRecordingMode(recordingMode, vncRecordingDirectory, null);
421404
}

0 commit comments

Comments
 (0)