-
Notifications
You must be signed in to change notification settings - Fork 298
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
Bug: port mapping not working with with_network
(with_exposed_ports)
#645
Comments
with_network
(with_exposed_ports)
Moreover, I have some difficulties to create communication between these two containers on gitlab ci. |
I also experience some difficulties with advanced network setup and I came to the conclusion it's due to the way it's implemented in TC:
the solution could be to pass
|
It seems to work when I add . |
Confirming this does not work, for good reasons: When calling Instead, this is what start should look like: ....
self._container = docker_client.run(
self.image,
command=self._command,
detach=True,
environment=self.env,
ports=self.ports,
name=self._name,
volumes=self.volumes,
# New params
network=network.name,
networking_config={network.name: EndpointConfig(version, aliases=self._network_aliases, **self._network_endpoint_configs) )
**self._kwargs,
) In the meantime a workaround is: # replace:
# ctr.with_network(network)
# ctr.with_network_aliases("network_alias")
# with:
ctr.with_kwargs(
network=network.name, networking_config={network.name: EndpointConfig("1.33", aliases=["network_alias"])}
) Also adding a quick reproducer for testing: def kafka(network: Network):
ctr = KafkaContainer(image=f"confluentinc/cp-kafka:7.6.1")
ctr.with_network(network)
ctr.with_network_aliases("kafka")
# uncomment to fix
# ctr.with_kwargs(network=network.name, networking_config={network.name: EndpointConfig("1.33", aliases=["kafka"])})
with ctr:
assert re.match("[^/:]+:[0-9]{4,5}", ctr.get_bootstrap_server()).group() |
open to a PR for this |
I struggled for hours with the same problem, and got this to work (in Pytest's fixtures) : MQTT_BROKER_NETWORK_ALIAS = "mqtt_broker"
@pytest.fixture()
def network_for_test() -> Generator[Network, None, None]:
test_network = Network()
with test_network:
yield test_network
@pytest.fixture()
def mqtt_broker(network_for_test: Network) -> Generator[MosquittoContainer, None, None]:
mqtt_broker = MosquittoContainer() \
.with_network(network_for_test) \
.with_network_aliases(MQTT_BROKER_NETWORK_ALIAS)
with mqtt_broker:
yield mqtt_broker
@pytest.fixture()
def web_server(mqtt_broker: MosquittoContainer,
network_for_test: Network) -> Generator[ServerContainer, None, None]:
# [...]
web_server = ServerContainer(
image="something", port=80
).with_network(network_for_test)
# [...]
with web_server:
yield web_server This way, I can make the web server connect to the MQTT broker (using the |
I've combined @champialex and mine findings into #678 |
fixes #645 - network should be attached as the container is started, not as a post-start action. This will make sure port binding and exposing works correctly. --------- Signed-off-by: mgorsk1 <gorskimariusz13@gmail.com>
fixes #645 - network should be attached as the container is started, not as a post-start action. This will make sure port binding and exposing works correctly. --------- Signed-off-by: mgorsk1 <gorskimariusz13@gmail.com>
Hi all,
I would like to have two containers connected each others, with a code similar to
I regularly have no port mapping for my containers. Do you have any ideas?
The text was updated successfully, but these errors were encountered: