-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only publish exposed ports #4122
Changes from 7 commits
0e0e8c8
cc133cd
c40be26
1c0cded
1373b0e
006533e
c0dcddc
d931379
5583d00
b80f00f
a9272c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
package org.testcontainers.containers; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static org.testcontainers.utility.CommandLine.runShellCommand; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
|
@@ -16,46 +13,14 @@ | |
import com.github.dockerjava.api.model.HostConfig; | ||
import com.github.dockerjava.api.model.Link; | ||
import com.github.dockerjava.api.model.PortBinding; | ||
import com.github.dockerjava.api.model.Ports; | ||
import com.github.dockerjava.api.model.Volume; | ||
import com.github.dockerjava.api.model.VolumesFrom; | ||
import com.github.dockerjava.core.DefaultDockerClientConfig; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.hash.Hashing; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.UndeclaredThrowableException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.zip.Adler32; | ||
import java.util.zip.Checksum; | ||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.NonNull; | ||
|
@@ -97,6 +62,43 @@ | |
import org.testcontainers.utility.ResourceReaper; | ||
import org.testcontainers.utility.TestcontainersConfiguration; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.UndeclaredThrowableException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.zip.Adler32; | ||
import java.util.zip.Checksum; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static org.testcontainers.utility.CommandLine.runShellCommand; | ||
|
||
/** | ||
* Base class for that allows a container to be launched and controlled. | ||
*/ | ||
|
@@ -718,21 +720,32 @@ public Set<Integer> getLivenessCheckPortNumbers() { | |
|
||
private void applyConfiguration(CreateContainerCmd createCommand) { | ||
HostConfig hostConfig = buildHostConfig(); | ||
createCommand.withHostConfig(hostConfig); | ||
|
||
// Set up exposed ports (where there are no host port bindings defined) | ||
ExposedPort[] portArray = exposedPorts.stream() | ||
.map(ExposedPort::new) | ||
.toArray(ExposedPort[]::new); | ||
// PortBindings must contain: | ||
// * all exposed ports with a randomized host port (equivalent to -p CONTAINER_PORT) | ||
// * all exposed ports with a fixed host port (equivalent to -p HOST_PORT:CONTAINER_PORT) | ||
List<PortBinding> allPortBindings = new ArrayList<>(); | ||
// First collect all the randomized host ports from our 'exposedPorts' field | ||
exposedPorts.stream() | ||
.map(ExposedPort::new) | ||
.map(p -> new PortBinding(Ports.Binding.empty(), p)) | ||
.forEachOrdered(allPortBindings::add); | ||
// Next collect all the fixed host ports from our 'portBindings' field | ||
portBindings.stream() | ||
.map(PortBinding::parse) | ||
.forEachOrdered(allPortBindings::add); | ||
bsideup marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
hostConfig.withPortBindings(allPortBindings); | ||
|
||
// Next, ExposedPorts must be set up to publish all of the above ports, randomized and fixed. | ||
// Collect all of the exposed ports for publication | ||
final List<ExposedPort> exposedPorts = allPortBindings.stream() | ||
.map(PortBinding::getExposedPort) | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the thing that is substantially different from the previous implementation; changes above are mainly to support this change and to make it clearer what we're doing. Of course, |
||
createCommand.withExposedPorts(exposedPorts); | ||
|
||
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); | ||
createCommand.withHostConfig(hostConfig); | ||
|
||
if (commandParts != null) { | ||
createCommand.withCmd(commandParts); | ||
|
@@ -798,8 +811,6 @@ private void applyConfiguration(CreateContainerCmd createCommand) { | |
createCommand.withNetworkMode(networkForLinks.get()); | ||
} | ||
|
||
createCommand.withPublishAllPorts(true); | ||
|
||
PortForwardingContainer.INSTANCE.getNetwork().ifPresent(it -> { | ||
withExtraHost(INTERNAL_HOST_HOSTNAME, it.getIpAddress()); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package org.testcontainers.containers; | ||
|
||
import com.google.common.collect.Sets; | ||
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.time.Duration; | ||
import java.util.Set; | ||
|
||
public class ClickHouseContainer extends JdbcDatabaseContainer { | ||
public static final String NAME = "clickhouse"; | ||
|
@@ -54,8 +56,8 @@ public ClickHouseContainer(final DockerImageName dockerImageName) { | |
} | ||
|
||
@Override | ||
protected Integer getLivenessCheckPort() { | ||
return getMappedPort(HTTP_PORT); | ||
public Set<Integer> getLivenessCheckPortNumbers() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not strictly required for this PR, but during experimenting with approaches I found that a handful of our modules are using the |
||
return Sets.newHashSet(HTTP_PORT); | ||
bsideup marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,6 +166,19 @@ public String getConnectionString() { | |
protected void configure() { | ||
super.configure(); | ||
|
||
addExposedPorts( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interestingly the Couchbase module seems to have always relied on an |
||
MGMT_PORT, | ||
MGMT_SSL_PORT, | ||
VIEW_PORT, | ||
VIEW_SSL_PORT, | ||
QUERY_PORT, | ||
QUERY_SSL_PORT, | ||
SEARCH_PORT, | ||
SEARCH_SSL_PORT, | ||
KV_PORT, | ||
KV_SSL_PORT | ||
); | ||
|
||
WaitAllStrategy waitStrategy = new WaitAllStrategy(); | ||
|
||
// Makes sure that all nodes in the cluster are healthy. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding imports, there are some changes in ordering, but AFAICT these only match the IntelliJ defaults (with our
.editorconfig
tweaks layered on top for star imports).TBH I'd prefer to go with the changed (hopefully stable) import order in this PR, rather than continue to constantly have the burden of maintaining the order (I don't know why the current order is as it is).
I think if we find ourselves continuing to run up against this then we should introduce a code formatter to our build (e.g. spotless gradle plugin + google formatter with AOSP style (4 space indent)).