Closed romanzdk closed 1 month ago
the postgres image does not support being run on a custom port. if you are trying to expose it on a specific port, please consider just letting the container runtime pick a random port and querying it at runtime to determine which external port to use.
it is not being considered right now to implement a custom internal port.
https://github.com/docker-library/docs/blob/master/postgres/README.md
for this use case:
mkdir testcontainers-python_696
cd testcontainers-python_696
python -m venv .venv
test -f .venv/bin/activate && . .venv/bin/activate || . .venv/Scripts/activate
pip install testcontainers pytest
from testcontainers.postgres import PostgresContainer
import pytest
@pytest.fixture(scope = 'session')
def get_pg():
with PostgresContainer('postgres:16-alpine') as postgres:
yield postgres
@pytest.fixture(scope = 'session')
def get_pg2():
with PostgresContainer('postgres:16-alpine') as postgres:
yield postgres
def test_main(get_pg, get_pg2):
print(f"get_pg has port {get_pg.get_exposed_port(5432)}")
print(f"get_pg2 has port {get_pg2.get_exposed_port(5432)}")
produces:
$ pytest script.py -s
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.11.9, pytest-8.3.2, pluggy-1.5.0
rootdir: ~/testcontainers-python_696
collected 1 item
script.py Pulling image testcontainers/ryuk:0.8.1
Container started: a08116c1c65a
Waiting for container <Container: a08116c1c65a> with image testcontainers/ryuk:0.8.1 to be ready ...
Pulling image postgres:16-alpine
Container started: 377144de6df4
Waiting for container <Container: 377144de6df4> with image postgres:16-alpine to be ready ...
Pulling image postgres:16-alpine
Container started: 447592a8ac89
Waiting for container <Container: 447592a8ac89> with image postgres:16-alpine to be ready ...
Waiting for container <Container: 377144de6df4> with image postgres:16-alpine to be ready ...
get_pg has port 32778
Waiting for container <Container: 447592a8ac89> with image postgres:16-alpine to be ready ...
get_pg2 has port 32779
Describe the bug
I need to run 2 postgres databases each on different port. Both containers start and from logs I can see that both are ready but testcontainers are just stuck on "Waiting for container...".
To Reproduce
and run
pytest file.py -sv
to get:docker ps
gives:Runtime environment