Skip to content
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

'DockerContainer' object has no attribute '_container' #399

Closed
theopfr opened this issue Dec 3, 2023 · 5 comments · Fixed by #423
Closed

'DockerContainer' object has no attribute '_container' #399

theopfr opened this issue Dec 3, 2023 · 5 comments · Fixed by #423

Comments

@theopfr
Copy link

theopfr commented Dec 3, 2023

Creating Postgres Container instance results in error: 'PostgresContainer' object has no attribute '_container'

I tried starting a Postgres container and got the following error:

Exception ignored in: <function DockerContainer.__del__ at 0x7f1da5122b80>
Traceback (most recent call last):
  File "/.venv/lib/python3.9/site-packages/testcontainers/core/container.py", line 80, in __del__
AttributeError: 'PostgresContainer' object has no attribute '_container'

I already saw an issue #353 which was about the same error but it seems to persist.
See the code I used below.

To Reproduce

from testcontainers.postgres import PostgresContainer

def get_postgres_uri():
    # Use a PostgreSQL Docker container for testing
    with PostgresContainer("postgres:latest") as postgres:
        return postgres.get_connection_url()

I also tried out the code in the postgres/tests/test_postgres.py file and it failed with the same error message.

Runtime environment

OS: Pop!_OS 22.04 LTS x86_64
Python version: Python 3.9.12
Docker version: 24.0.5
testcontainers version: 3.7.1

@alexanderankin
Copy link
Member

im trying and failing to reproduce (on ubuntu) with:

uname -a # Linux z14-1 6.2.0-36-generic #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct  9 15:34:04 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
python -V # Python 3.9.18
mkdir testcontainers-python-issues-399 && cd $_
python -m venv .venv && . .venv/bin/activate
cat > test.py <<EOF
from testcontainers.postgres import PostgresContainer

def get_postgres_uri():
    # Use a PostgreSQL Docker container for testing
    with PostgresContainer("postgres:16-alpine") as postgres:
        return postgres.get_connection_url()


print(get_postgres_uri())
EOF
python test.py

and i get these logs:

$ python test.py 
Pulling image postgres:16-alpine
Container started: 84479badddc7
Waiting to be ready...
Waiting to be ready...
Waiting to be ready...
Waiting to be ready...
Waiting to be ready...
postgresql+psycopg2://test:test@localhost:32769/test

docker version is also 24:

$ docker -v
Docker version 24.0.7, build afdd53b

i dont think this means there isn't an issue - maybe spending some time staring at the code of the library will show some obvious codepaths which cause the issue

@tobiaseisenschenk
Copy link

This happened for me as well on OSX. As I am using Rancher I attributed it to the unsupported runtime, but could be wrong of course.

@danb27
Copy link

danb27 commented Feb 3, 2024

Same issue!

@totallyzen
Copy link
Collaborator

heyo, I think this isn't OS dependent, but makes for some flaky testing
I bumped into this right here on our CI.

So I think this is defo a bug related to failing to create the container
See an example run
https://github.com/testcontainers/testcontainers-python/actions/runs/7889768439/job/21530402642

@totallyzen
Copy link
Collaborator

In fact I'm getting more convinced this issue is present when we encounter issues with the docker client. Some refactoring and defensive coding is needed as it seems.

@totallyzen totallyzen changed the title 'PostgresContainer' object has no attribute '_container' 'DockerContainer' object has no attribute '_container' Feb 13, 2024
totallyzen added a commit that referenced this issue Feb 13, 2024
# change

Fixes #399. Applied a bit of defensive coding and attempted to create
some tests for it, however reproducing it with a local dev machine is
not easy. I did my best to reproduce the issue with garbage collection
in the new test.

---------

Co-authored-by: Balint Bartha <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants