Testcontainers work as expected, fail to map Docker port when run with pytest in VS Code
Describe the bug I copied this code from the python-kafka tests in this repo. I am able to run it successfully as a script. However, when I run in pytest, the docker container doesn't even start because it can't map to a port.
This behavior only happens when I run pytest.
The error code is as follows:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <testcontainers.core.docker_client.DockerClient object at 0x0000016333B4DE90>
container_id = '0100dda1fb4811efbdf5743faf5208c2113f7952824e04799df9c3b497bd9864'
port = 9093
def port(self, container_id, port):
port_mappings = self.client.api.port(container_id, port)
if not port_mappings:
> raise RuntimeError(f'port mapping for container {container_id} and port {port} is not '
'available')
E RuntimeError: port mapping for container 0100dda1fb4811efbdf5743faf5208c2113f7952824e04799df9c3b497bd9864 and port 9093 is not available
I believe this is a windows issue as I had to add os.environ['TC_HOST'] = 'localhost' because Windows has issues connecting to the localnpipe named pipe.
To Reproduce
from kafka import KafkaConsumer, KafkaProducer, TopicPartition
from testcontainers.kafka import KafkaContainer
import os
def test_kafka_confluent_7_1_3():
kafka_container = KafkaContainer("confluentinc/cp-kafka:7.1.3")
# set the advertised listeners to be the container's IP address
with kafka_container as container:
container.get_bootstrap_server()
produce_and_consume_kafka_message(container)
def produce_and_consume_kafka_message(container):
topic = 'test-topic'
bootstrap_server = container.get_bootstrap_server()
producer = KafkaProducer(bootstrap_servers=[bootstrap_server])
producer.send(topic, b"verification message")
producer.flush()
producer.close()
consumer = KafkaConsumer(bootstrap_servers=[bootstrap_server])
tp = TopicPartition(topic, 0)
consumer.assign([tp])
consumer.seek_to_beginning()
assert consumer.end_offsets([tp])[tp] == 1, \
"Expected exactly one test message to be present on test topic !"
if __name__ == '__main__':
test_kafka_confluent_7_1_3()
Runtime environment
Windows 10 Testcontainers 3.6.0 kafka-python 2.0.2
I am running the snippet above in pipenv, so it is not Docker in Docker.
Will update if I find a solution.
UPDATE: This is not unique to pytest.
I am able to run and pass this test when using Run and Debug within VSCode, but not from the console.
So running python ./<my test file>.py also fails in the VSCode integrated terminal.
I ran it using my system terminal and it worked correctly. So VSCode is the problem here.
I ran it using my system terminal and it worked correctly. So VSCode is the problem here.
So could you rename the title then?
I recently update devcontainers to use the poetry flow. I can't reproduce this in my devcontainer (dind) using the integrated terminal.
(testcontainers-_53phrdA-py3.11) vscode ➜ /workspaces/testcontainers-python (feature/nats) $ poetry run pytest -v --cov=testcontainers.$* modules/kafka/tests
===================================================================== test session starts =====================================================================
platform linux -- Python 3.11.8, pytest-7.4.3, pluggy-1.4.0 -- /home/vscode/.cache/pypoetry/virtualenvs/testcontainers-_53phrdA-py3.11/bin/python
cachedir: .pytest_cache
rootdir: /workspaces/testcontainers-python
configfile: pyproject.toml
plugins: cov-4.1.0, anyio-4.3.0
collected 3 items
modules/kafka/tests/test_kafka.py::test_kafka_producer_consumer
------------------------------------------------------------------------ live log call ------------------------------------------------------------------------
INFO testcontainers.core.container:container.py:63 Pulling image confluentinc/cp-kafka:5.4.3
INFO testcontainers.core.container:container.py:75 Container started: c07cd20bfa32
... elided....
/home/vscode/.cache/pypoetry/virtualenvs/testcontainers-_53phrdA-py3.11/lib/python3.11/site-packages/pytest_cov/plugin.py:312: CovReportWarning: Failed to generate report: No data to report.
warnings.warn(CovReportWarning(message))
---------- coverage: platform linux, python 3.11.8-final-0 -----------
================================================================ 3 passed in 78.54s (0:01:18) =================================================================
Running from the test explorer works as well
INFO kafka.conn:conn.py:380 <BrokerConnection node_id=1 host=172.18.0.1:32773 <connecting> [IPv4 ('172.18.0.1', 32773)]>: connecting to 172.18.0.1:32773 [('172.18.0.1', 32773) IPv4]
INFO kafka.conn:conn.py:410 <BrokerConnection node_id=1 host=172.18.0.1:32773 <connecting> [IPv4 ('172.18.0.1', 32773)]>: Connection complete.
INFO kafka.conn:conn.py:919 <BrokerConnection node_id=bootstrap-0 host=172.18.0.1:32773 <connected> [IPv4 ('172.18.0.1', 32773)]>: Closing connection.
PASSED [100%]
============================== 3 passed in 12.98s ==============================
Finished running tests!