Skip to content

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

Closed

Description

@theopfr

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

Activity

  1. alexanderankin commented on Dec 12, 2023

    @alexanderankin
    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

  2. tobiaseisenschenk commented on Dec 18, 2023

    @tobiaseisenschenk

    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.

  3. danb27 commented on Feb 3, 2024

    @danb27

    Same issue!

  4. totallyzen commented on Feb 13, 2024

    @totallyzen
    Contributor

    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

  5. totallyzen commented on Feb 13, 2024

    @totallyzen
    Contributor

    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.

  6. changed the title [-]'PostgresContainer' object has no attribute '_container'[/-] [+]'DockerContainer' object has no attribute '_container'[/+] on Feb 13, 2024
  7. added a commit that references this issue on Feb 13, 2024
    b535ea2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions