DBAPI Error; SSL SYSCALL error: EOF detected when running docker image

I am trying to deploy a python app that filters my database on Redshift then inserts the aggregated rows into a new table. The insert works fine from the python script but when running the docker image it returns sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) SSL SYSCALL error: EOF detected.

According to the Sqlalchemy docs: This error is a DBAPI Error and originates from the database driver (DBAPI), not SQLAlchemy itself. https://docs.sqlalchemy.org/en/13/errors.html#operationalerror

def exec_query(eng,db_user,db_pw,query):
    conn = eng.connect()
            print('Connection closed.')
        except exc.DBAPIError as e:
            if e.connection_invalidated:
                print("Connection was invalidated!")

Even in a try except block, e.connection_invalidated returns true every time when running the docker image.


FROM python:3.7-alpine
COPY . /app
RUN apk add postgresql-dev 
RUN apk add build-base
RUN pip install --trusted-host pypi.python.org -r req.txt
ENV NAME weekly_insert
CMD ["python", "script.py"]

sqlalchemy version is 1.3.5 and have set the memory available to the docker engine to 7 G.B.
Any help fixing the DBAPI issue is appreciated.

Source: StackOverflow