Connect to SQL Server database from a docker container
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Connecting from a Docker container to SQL Server is usually a networking problem, not a database-driver problem. The main question is where SQL Server is running: on the host machine, in another container, or on a remote server. Once that is clear, the connection string and network setup become straightforward.
First Decide Where SQL Server Lives
There are three common setups:
- SQL Server runs on the host machine
- SQL Server runs in another container
- SQL Server runs on a different server entirely
The hostname you use in the connection string depends on that choice.
If SQL Server Runs on the Host Machine
The most common beginner mistake is using localhost inside the container. From the container's perspective, localhost means the container itself, not your laptop or server host.
On Docker Desktop, a common host alias is:
Example SQL Server connection string:
That works only if SQL Server is configured to accept TCP connections and the host firewall allows access.
Example with Python and pyodbc
A minimal container-side Python example:
This example assumes the SQL Server instance is reachable from the container and that the driver is installed inside the image.
If SQL Server Runs in Another Container
When both containers are on the same Docker network, use the other container's service name or container name as the server host.
Example Compose file:
Inside the app container, the server name becomes db:
This is often simpler than crossing from container networking back to the host.
If SQL Server Is Remote
If the database is on another server, use its DNS name or IP just like any other remote service.
Example:
At that point Docker is mostly irrelevant except that the container must be allowed to reach the destination network.
Make Sure SQL Server Is Listening on TCP/IP
Even with the right hostname, connection attempts fail if SQL Server is not configured for TCP/IP or if it listens on a different port.
Typical checks:
- SQL Server TCP/IP enabled
- port
1433open if using the default instance configuration - firewall rules allow inbound access
- authentication mode matches your credentials
Without those basics, the connection string can be perfectly written and still fail.
Test Connectivity Before Blaming the App
It is often faster to test raw network reachability first.
Inside the container:
If that fails, your application code is not the first thing to debug.
Use Environment Variables for Secrets
Do not hardcode database credentials in the image or source code.
Example:
Then pass those variables with Docker Compose, Docker run, or your orchestration platform.
Common Pitfalls
- Using
localhostfrom inside the container and accidentally targeting the container itself. - Forgetting that SQL Server on the host still needs firewall and TCP/IP configuration.
- Mixing up the hostname strategy for host-based, container-based, and remote SQL Server setups.
- Debugging application code before verifying basic network connectivity to port
1433. - Hardcoding credentials in the image instead of using environment-based configuration.
Summary
- The correct SQL Server hostname depends on where the database actually runs.
- '
localhostinside a container is not the Docker host.' - Use
host.docker.internalfor host-based SQL Server on Docker Desktop, or service names for container-to-container networking. - Verify TCP/IP, firewall, and port configuration before debugging code.
- Keep connection details configurable and keep secrets out of source code.

