0

I am trying to get a python docker container to run. The container starts up, but I am unable to get pyodbc to login to an external MSSQL server. I get the error login timed out.

When I connect to the docker container with

docker exec -it 7f4 /bin/bash

and run ping 192.168.10.3, which is the IP Address of the host machine and where SQL Server is running, I get nothing back. Not even, no reply.

If I ping localhost that works just fine, but obviously that is just the localhost inside the container.

Here is my Dockerfile

FROM python:3.7.4-stretch

RUN apt-get update \

# && apt-get install g++ \

&& apt-get install --yes unixodbc-dev \

&& pip install pyodbc==4.0.27

# --- Install Microsoft ODBC Driver

# https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

RUN curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list

# Need some certificate fix to install msodbcsql17

RUN apt-get install --yes apt-transport-https ca-certificates \

&& apt-get update && ACCEPT_EULA=Y apt-get install --yes msodbcsql17

WORKDIR /app



COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . /app
CMD [ "uvicorn", "app:app", "--port", "8000"]

and here is the error that I get

Traceback (most recent call last):
  File "/usr/local/bin/uvicorn", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/uvicorn/main.py", line 355, in main
    run(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/uvicorn/main.py", line 379, in run
    server.run()
  File "/usr/local/lib/python3.7/site-packages/uvicorn/main.py", line 407, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.7/site-packages/uvicorn/main.py", line 414, in serve
    config.load()
  File "/usr/local/lib/python3.7/site-packages/uvicorn/config.py", line 300, in load
    self.loaded_app = import_from_string(self.app)
  File "/usr/local/lib/python3.7/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./app.py", line 21, in <module>
    cursor = db.get_cursor(db.get_db_connection())
  File "./db.py", line 8, in get_db_connection
    return pyodbc.connect(f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={os.getenv('DB_HOST')},{os.getenv('DB_PORT')};DATABASE={os.getenv('DB_DATABASE')};UID={os.getenv('DB_USERNAME')};PWD={os.getenv('DB_PASSWORD')}")
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
PrestonDocks
  • 4,851
  • 9
  • 47
  • 82
  • Try this: https://stackoverflow.com/a/60836732/8405123 – Kapil Khandelwal Oct 01 '20 at 16:54
  • 1
    Is your routing correct and are you certain there is no firewall blocking your connections? e.g. network timeout suggests an issue with the networking. Nothing back for `ping` requests could just mean you're dropping ICMP packets (can't trust `ping`), can `telnet server port` of the database? – masseyb Oct 01 '20 at 17:21
  • I think it must have something to do with my Macbook. I have put the same image on my Synology NAS and it works fine. Unfortunately, I don't have the time to do any more testing at the moment. I will have to come back to this at a later date. Thanks for your suggestion though. – PrestonDocks Oct 01 '20 at 18:40
  • I don't have a Macbook to test with but I'm aware of [documentation specifically for `docker` networking on Mac](https://docs.docker.com/docker-for-mac/networking/). I suspect that there is a firewall running inside the VM in which `docker` is running that's blocking your connections. – masseyb Oct 01 '20 at 21:26

0 Answers0