简体   繁体   中英

Flask application running in a Docker Container unable to access MSSQL Database

I am trying to run a Flask application running off Linux in a Docker container. When I try logging into the website, I get the below error message as my user is not found in the MSSQL database, even though the user exists:

sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user ''. (18456) (SQLDriverConnect)")

When I run the Flask application locally on my Windows computer and I try logging into the website, my user is found in the MSSQL database, and I am able to login to the website successfully. My connection string is set to 'Trusted Connection = Yes'. Below is my working connection string on my local Windows environment:

SQLALCHEMY_DATABASE_URI = f"mssql+pyodbc:///?odbc_connect={quote_plus(os.getenv('SQLALCHEMY_DATABASE_URI','Driver={ODBC Driver 17 for SQL Server};Server=tcp:****.database.windows.net,1433;Database=****;Trusted_Connection=yes;'))}"

Since 'Trusted Connection' is only used for Windows authentication and not Linux, I tried removing 'Trusted_Connection=yes' from the connection string, but now we get the above Login failed for user ' ' error.

Below is my dockerfile:

FROM python

COPY requirements.txt requirements.txt
RUN apt-get update
RUN apt-get install -y curl apt-transport-https
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17 unixodbc-dev
RUN pip install -r requirements.txt
COPY . .
EXPOSE 80 80
ENTRYPOINT ["python"]
CMD ["run.py"]

Can anyone provide insight as to how I can get this connection string to work in a Linux environment? Thanks!

I have not developed applications with flask, so I can only speak from my experience with nodejs and postgresql. Have you accounted for the fact that you need to be using a bridge network to communicate between docker containers? If you have your sql database in another container you will have to either use the default bridge network or create your own bridge network . I hope this helped in any way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM