Docker not working with psycopg2 properly

Hello i have built an app using django 4.2.1 long ago before i heard about docker and have been facing a ton of problems with the environment and setting up the code on a new device for the longest time
and now that i found out about docker i want to add my app to docker

the problem im facing right now is that i have a feature in my app that connects the user to a database of his
when i run the app normally using terminal and “python manage.py runserver” the feature works normally
however when i try and run the same code through docker the container itself shuts down and i get the error code 245.
i use sqlalchemy for the engine and psycopg2 for postgres connections
when i try and connect to the database from the bash / running a python program into the container itself it works but whenever i run the code it completely shuts down the container and gives the error code 245

FROM python:3.12

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /Sibaway

RUN apt-get update && apt-get install -y \
    gcc \
    pkg-config \
    default-libmysqlclient-dev \
    libpq-dev \
    build-essential \
    libreoffice \
    libreoffice-common \
    uno-libs-private \
    python3-uno \
    libsm6 \
    libxext6 \
    libxrender1 \
    fonts-dejavu \
    && rm -rf /var/lib/apt/lists/*

COPY requirements_linux.txt .

RUN pip install --upgrade pip
RUN pip install -r requirements_linux.txt

COPY . .

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "--skip-checks" , "0.0.0.0:8000"]

this is my dockerfile ^ ^
Thank You

Where is error code 245 coming from? During container build, during execution? Is there a stacktrace?

There really isnt anything
this below is the function that the api reaches after the request

        print(connection_url)
        try:
            engine = create_engine(connection_url)
            with engine.connect() as connection:
                print('connection established')
                connection.close()

i think the container enters this function and hangs after the create_engine()

container3  | postgresql+psycopg2://{Myuser}:{Mypassword}@aws-0-us-east-2.pooler.supabase.com:5432/DBname
container3 exited with code 245

i edited the output so that nothing important gets out but this is literally everything in the terminal

from what i understood from chatgpt is that this error code is likely a library problem where it tries to raise an error but the error doesnt get a chance to come out (i dont really understand this part) and instead it segfaults and crashes the entire container

You can try to add the environment variable PYTHONFAULTHANDLER with any value (as long as the value is not empty) to your container. It should give you more detailed output about the error.

See https://docs.python.org/3/using/cmdline.html#envvar-PYTHONFAULTHANDLER

i tried it before and i got this output

container3  | Fatal Python error: Segmentation fault
container3  | 
container3  | Thread 0x000071682bfff6c0 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/socket.py", line 720 in readinto                                                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 237 in handle_one_request                                       
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 229 in handle                                                   
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 766 in __init__                                                                               
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 362 in finish_request                                                                         
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 697 in process_request_thread                                                                 
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1012 in run                                                                                      
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap                                                                               
container3  |                                                                                                                                                        
container3  | Thread 0x0000716838a4f6c0 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/socket.py", line 720 in readinto                                                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 237 in handle_one_request
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 229 in handle                                                   
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 766 in __init__                                                                               
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 362 in finish_request                                                                         
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 697 in process_request_thread                                                                 
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1012 in run                                                                                      
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap                                                                               
container3  | 
container3  | Thread 0x00007168399506c0 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/socket.py", line 720 in readinto                                                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 237 in handle_one_request                                       
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 229 in handle                                                   
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 766 in __init__                                                                               
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 362 in finish_request                                                                         
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 697 in process_request_thread
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1012 in run                                                                                      
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap                                                                               
container3  |                                                                                                                                                        
container3  | Thread 0x000071683a2516c0 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/socket.py", line 720 in readinto                                                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 237 in handle_one_request                                       
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 229 in handle                                                   
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 766 in __init__                                                                               
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 362 in finish_request
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 697 in process_request_thread                                                                 
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1012 in run                                                                                      
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap                                                                               
container3  |                                                                                                                                                        
container3  | Thread 0x0000716840ffc6c0 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/socket.py", line 720 in readinto                                                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 237 in handle_one_request                                       
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 229 in handle                                                   
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 766 in __init__
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 362 in finish_request                                                                         
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 697 in process_request_thread                                                                 
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1012 in run                                                                                      
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap                                                                               
container3  |                                                                                                                                                        
container3  | Thread 0x000071688f5ff6c0 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/threading.py", line 359 in wait
container3  |   File "/usr/local/lib/python3.12/threading.py", line 655 in wait                                                                                      
container3  |   File "/usr/local/lib/python3.12/site-packages/tqdm/_monitor.py", line 60 in run                                                                      
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap                                                                               
container3  |                                                                                                                                                        
container3  | Current thread 0x000071689faa96c0 (most recent call first):                                                                                            
container3  |   File "/usr/local/lib/python3.12/site-packages/psycopg2/__init__.py", line 122 in connect                                                             
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 629 in connect
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 661 in connect                                                      
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 895 in __connect                                                        
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 673 in __init__                                                         
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 388 in _create_connection                                               
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 175 in _do_get                                                          
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 711 in checkout                                                         
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 1264 in _checkout                                                       
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 447 in connect
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3301 in raw_connection                                                
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 143 in __init__                                                       
container3  |   File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3277 in connect                                                       
container3  |   File "/Sibaway/projects/views.py", line 2131 in CRUD_DatabaseConnection                                                                              
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/base.py", line 197 in _get_response                                               
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/corsheaders/middleware.py", line 56 in __call__                                                        
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner
container3  |   File "/Sibaway/_keenthemes/middleware.py", line 10 in __call__                                                                                       
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/deprecation.py", line 134 in __call__                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/deprecation.py", line 134 in __call__                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/deprecation.py", line 134 in __call__                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/deprecation.py", line 134 in __call__                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/deprecation.py", line 134 in __call__                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner
container3  |   File "/usr/local/lib/python3.12/site-packages/whitenoise/middleware.py", line 123 in __call__                                                        
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/deprecation.py", line 134 in __call__                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/deprecation.py", line 134 in __call__                                                     
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55 in inner                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/base.py", line 140 in get_response                                                
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/handlers/wsgi.py", line 124 in __call__                                                    
container3  |   File "/usr/local/lib/python3.12/site-packages/django/contrib/staticfiles/handlers.py", line 80 in __call__                                           
container3  |   File "/usr/local/lib/python3.12/wsgiref/handlers.py", line 137 in run                                                                                
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 252 in handle_one_request                                       
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 229 in handle                                                   
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 766 in __init__                                                                               
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 362 in finish_request
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 697 in process_request_thread                                                                 
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1012 in run                                                                                      
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap                                                                               
container3  |                                                                                                                                                        
container3  | Thread 0x00007169effff6c0 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/selectors.py", line 415 in select                                                                                    
container3  |   File "/usr/local/lib/python3.12/socketserver.py", line 235 in serve_forever                                                                          
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 281 in run                                                      
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 140 in inner_run                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 64 in wrapper                                                        
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1012 in run
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap                                                                               
container3  |                                                                                                                                                        
container3  | Thread 0x0000716a70ce96c0 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/threading.py", line 359 in wait                                                                                      
container3  |   File "/usr/local/lib/python3.12/queue.py", line 180 in get                                                                                           
container3  |   File "/usr/local/lib/python3.12/site-packages/posthog/consumer.py", line 107 in next                                                                 
container3  |   File "/usr/local/lib/python3.12/site-packages/posthog/consumer.py", line 76 in upload                                                                
container3  |   File "/usr/local/lib/python3.12/site-packages/posthog/consumer.py", line 65 in run                                                                   
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1075 in _bootstrap_inner                                                                         
container3  |   File "/usr/local/lib/python3.12/threading.py", line 1032 in _bootstrap
container3  |                                                                                                                                                        
container3  | Thread 0x0000716abfb27b80 (most recent call first):                                                                                                    
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 405 in tick                                                          
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 350 in run_loop                                                      
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 344 in run                                                           
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 660 in start_django                                                  
container3  |   File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 671 in run_with_reloader                                             
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 118 in run                                         
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 111 in handle                                      
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 458 in execute                                                   
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 74 in execute                                      
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 412 in run_from_argv
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 436 in execute                                               
container3  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 442 in execute_from_command_line                             
container3  |   File "/Sibaway/manage.py", line 18 in main                                                                                                           
container3  |   File "/Sibaway/manage.py", line 22 in <module>

as you can see this is somewhat long and not understandable but from the scan i did i saw that the problem as i said is from the connect_engine() as said by container3 | File "/Sibaway/projects/views.py", line 2131 in CRUD_DatabaseConnection
it is probably some configuration that i dont know about but i cant seem to get the reason for this issue which is why i came here for help

Did you check your connection string if that works from within the container?

Yes i did and it works
I also ran the connect engine command through docker exec -it container3 python manage.py python and then the part pf the code that breaks and it worked just fine so i cant really see the problem

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.