testcontainers-python icon indicating copy to clipboard operation
testcontainers-python copied to clipboard

Some containers fail to start due to exposed port race condition

Open jmorales4 opened this issue 4 years ago • 3 comments

DbContainer.start() calls @wait_container_is_ready()DbContainer._connect() calls virtual DbContainer.get_connection_url() implemented in derived classes (e.g. postgres) calls DbContainer._create_connection_url(...) in derived classes (e.g. postgres) calls DockerContainer.get_exposed_port(...) calls DockerClient.port(...) which returns None if called too quickly after container creation (on FAST machines?) raises RuntimeError('port mapping ... is not available') which is NOT defined as TRANSIENT_EXCEPTION by wait_container_is_ready so no retry and container creation fails

Exception trace:

Failure
Traceback (most recent call last):
  File ".../services/spec/containers.py", line 50, in postgres
    postgres_container = PostgresContainer(image='public.ecr.aws/docker/library/postgres:14.3-alpine3.16',
  File ".../venv/lib/python3.8/site-packages/testcontainers/core/generic.py", line 55, in start
    self._connect()
  File ".../venv/lib/python3.8/site-packages/testcontainers/core/waiting_utils.py", line 49, in wrapper
    return wrapped(*args, **kwargs)
  File ".../venv/lib/python3.8/site-packages/testcontainers/core/generic.py", line 32, in _connect
    engine = sqlalchemy.create_engine(self.get_connection_url())
  File ".../venv/lib/python3.8/site-packages/testcontainers/postgres.py", line 57, in get_connection_url
    return super()._create_connection_url(dialect="postgresql+{}".format(self.driver),
  File ".../venv/lib/python3.8/site-packages/testcontainers/core/generic.py", line 44, in _create_connection_url
    port = self.get_exposed_port(port)
  File ".../venv/lib/python3.8/site-packages/testcontainers/core/container.py", line 105, in get_exposed_port
    mapped_port = self.get_docker_client().port(self._container.id, port)
  File ".../venv/lib/python3.8/site-packages/testcontainers/core/docker_client.py", line 46, in port
    raise RuntimeError(f'port mapping for container {container_id} and port {port} is not '
RuntimeError: port mapping for container fe4534d511876b29ff3dc9d0c351bf328f25bd25e820f5f0e79a66da9235b1d5 and port 5432 is not available

Fix is to add RuntimeError to waiting_utils.py::TRANSIENT_EXCEPTIONS?
or raise ConnectionError in docker_client.py line 46?

jmorales4 avatar Jul 22 '22 19:07 jmorales4

Thanks for reporting! Raising a ConnectionError as you suggested sounds like a good idea. Would you be happy to submit a PR?

tillahoffmann avatar Jul 24 '22 16:07 tillahoffmann

https://github.com/testcontainers/testcontainers-python/pull/230 Note that the above suggested fix worked for DbContainers but the behavior was found on other container types in your unit tests. The PR fixes it for all container types in the unit tests.

jmorales4 avatar Jul 25 '22 20:07 jmorales4

#230 Note that the above suggested fix worked for DbContainers but the behavior was found on other container types in your unit tests. The PR fixes it for all container types in the unit tests.

Would it be possible to get https://github.com/testcontainers/testcontainers-python/pull/230 reviewed and merged? I just ran into this issue myself and the PR fixed it for me. Tx.

dboreham avatar Aug 03 '22 17:08 dboreham