How can I contact SQL Server running in a Windows Container from another Container?

I am running SQL Server Express inside of a Windows container on Windows 10. It runs fine, and I can connect to the instance of SQL Server if I either

  1. Provide the container’s IP address (I get this from 'docker inspect ')
  2. Use the first 12 characters of the container’s ID (for instance, "XYZ123ABC456\SQLSERVER)

What I would like to do is have a static DB connection string for all of the other containers to use in order to connect to the database. Of course, every time I start the SQL Server container, the container has a different container ID and IP address.

An important thing to note is that all of my containers are running on my laptop. I have heard that there is a problem with loopback in that you cannot use ‘localhost’ in the IP address.

I am sure there is an easy solution to this. Would you please give me some hints?

Edit: I am also using docker-compose to create the various services, including the SQL Server container.



docker ps will list the name of the container (probably an adjective_scientist); does that name work in this context? If it does, you can give an explicit docker run --name, which might solve your issue.

Thanks for the reply, David. Referring to the container name works when I connect to an ActiveMQ broker that is runing in another container. However, the same thing does not work with SQL Server. I tried with SQL Server Management Studio, and unfortunately, no luck.

If you want to access your container in this way you should assign him a static IP address. To do so you shouldn`t use NAT network (this is the default one). You can see more about networking here (MSDN - Windows Container Networking).

@magmasystems I can bet you are not running SSMS in a container! :slight_smile:

If you are using Compose to manage your services/containers then you should not have any problems to refer to the SQL Server by its name i.e. the Compose service name, from any of the containers that are in the same NAT network. Also you will be able to connect using the same name from the docker host. If you want to use SSMS from a different host you should map the SQL Server port on the host and use the docker host address to connect to the service.

You can share your compose file. It will make you use case clear!

Thanks to all for your answers. I managed to get a static address for SQL Server by using the docker-compose.yml below. I have tied SQL Server to, which is good for now. I wanted to use the “qsql” alias so that I would not have to use the explicit IP address, but I do not think that aliases are supported under Windows Docker.

By the way, I am running SQL Server Management Studio outside of a container. Maybe the alias would only work if you run SSMS inside of a container, which of course, you cannot do :slight_smile:

version: '2'
    image: microsoft/mssql-server-windows-express
    container_name: sqlserver2
      - c:\Databases:c:\Databases
      sa_password: P@ssW0rd
      accept_eula: Y
      attach_dbs: "[{'dbName':'trunk','dbFiles':['c:\\\\Databases\\\\trunk.mdf','c:\\\\Databases\\\\trunk_Log.ldf']}]"
          - qsql
      name: nat

Yes, the alias doesn`t work from outside of the containers. So If you want to access a SQL instance from SSMS you have to access via Container_IP,port. Or you can access using localhost,port / ContainerHost_IP,port but this is possible on Win Server 2016 only due to WinNAT restrictions on Win10.