I'm going too preface this saying, I'm new to Docker.
I am using three docker containers:
SQL Server on standard ports
Coldfusion on standard ports
And I also have DBeaver on my host (correct term?).
Independently they both work correctly. I am able to see SQL Server from my host using dbeaver.
My issue is I cannot see SQL Server from the Coldfusion Docker container. The normal connection to databases from coldfusion is jdbc.
The error is:
Connection verification failed for data source: nft-gis
java.sql.SQLNonTransientConnectionException: [Macromedia][SQLServer JDBC Driver]Error establishing socket to host and port: localhost:1433. Reason: Connection refused (Connection refused)
Under normal circumstances, this feels like a firewall port issue, but again, I'm new to docker, and don't know how to get these two to see each other.
Is there a way to tell the ColdFusion container where the SQL Server container is, so I can connect?
Thanks
establishing socket to host and port: localhost:1433
It seems like you used the same connection string from DBeaver in ColdFusion? That won't work because localhost is the ColdFusion container.
At a high-level, you'll need to use commands like so
NETWORK_NAME="app-development" # for example
docker network create $NETWORK_NAME
docker run --network $NETWORK_NAME ... sqlserver
docker run --network $NETWORK_NAME --link sqlserver:sqlserver ... coldfusion
And then you should be able to connect to sqlserver:1433 from ColdFusion.
Related
I am trying to connect to a Docker image SQL Server database through another containerized application (microservice) built with .NET Core 3.1 but I get the following error thrown by Docker:
fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
An error occurred using the connection to database 'ProductDb' on server 'localhost,1433'.
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'Infrastructure.ProductContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
The connection string I am using is:
"ProductDb": "Server=localhost,1433;Database=ProductDb;User Id=sa;Password=********;"
I need to mention that this same application works fine when it's hosted on a IIS server.
If you want to link two docker containers on the same machine you have three options:
Use docker-compose
run image with --network
Advanced scenario: Docker Swarm / Kubernetes
Your question:
Your containers is not linked and (I suppose you don't have run its with docker-compose or --network switch) you have also specified localhost where localhost refer to the same machine, in this case ASP.NET Core container instance.
Use docker-compose (preferred and simple) or --network and specify the connection string with Server=container_name\instancename_ifany,1433
When you run the SQL image you can optionally redirect 1433 port to another and check / monitor the sql instance --ports 9001:1433; with this you can use SSMS and connect to localhost, 9001
Target the container name as opposed to localhost. Localhost will try to connect to itself (container).
I could download and run the microsoft/mssql-server-windows-express
So this is works:
docker run -d -p 1433:1433 -e sa_password=MyPassword1 -e ACCEPT_EULA=Y microsoft/mssql-server-windows-express
I can connect it via the powershell and via the Sql server management studio (localhost, 1433 and SA and MyPass1)
So I can see the databases and play with them.
However in .NET core API application I cannot connect to the dockerized mssql server express (windows dockerized and not linux)
"ConnectionStrings": {
"Default": "Server=127.0.0.1,1433;Database=master;User=sa;Password=MyPassword1 ;"
},
Unfortunately I get this sql exception always:
System.Data.SqlClient.SqlException: 'A network-related or
instance-specific error occurred while establishing a connection to
SQL Server. The server was not found or was not accessible. Verify
that the instance name is correct and that SQL Server is configured to
allow remote connections. (provider: TCP Provider, error: 0 - No
connection could be made because the target machine actively refused
it.)'
So the mssql-server-windows-express container is running I can reach it by the powershell command and by the sql server management studio, but when I run the .net core api applicaion in the visual studio 2017 I get this sql exception always.
Localhost in terms of your computer is not the same as localhost in terms of the running Docker instance. By using 127.0.0.1, you're telling it to connect to itself, not your local machine. You need to change this to your local machine's actual IP.
On the same virutal machine (remote, ubuntu), I have
An SQL Server running in a Docker
An .NET Core 2.2 (IdentityServer) application running in a docker
An instance of jwilder.nginx-proxy serving as a reverse-proxy for every web application on the machine
A multitude of other .NET Core apps
I am able to connect to all of my websites using both machine IP + port and domain name, which means the reverse proxy works as expected and the dockers are well-configured
I am able to connect to the SQL Server using SSMS from my local machine, which means that the SQL Server docker properly forwards the TCP connection on port 1433
The IdentityServer .NET Core 2 web application is able to connect to the SQL Server when run on my local machine.
The remote-docker IdentityServer application can't reach the SQL Server instance with the following error (shortened for clarity - removed stack trace)
System.Data.SqlClient.SqlException (0x80131904):
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider, error: 40 - Could not open a connection to SQL Server) at [...]
I know that the SQL server is running and reachable from the internet, and I know that the application's code is not at fault because I tested both.
So I deduced it had to be the IdentityServer docker that was blocking the connexion. So I tried:
Using the --expose 20 command on the IdentityServer docker
Opening mapping the port 20 inside the container to some port outside -p 45264:20 in addition to the already exposed port 80
I originally worked on using the port 1433 on both sides of the mapping but since it didn't work I tried using an other port on the outside (20). Didn't change anything
Here is the connexion string used by the IdentityServer (sensitive data hidden):
Data Source=***.***.***.***,20;Initial Catalog=Identity;Persist Security Info=True;User ID=**;Password=******************
Why can't my IdentityServer docker reach the SQL Server docker while the SQL Server itself is perfectly reachable? How can I make this setup work?
When wrapping SQL server into Docker, the first thing to anticipate is the way you connect. SQL Server prefers named pipes and you have to explicitly set mode to tcp.
If connection is done locally, don't use localhost, change it to 127.0.0.1. Also writing explicit tcp: prefix may help, like this: Server=tcp:x.y.z.q,1433
As I understood you run Sql Server and IdentityServer (which has connection problem) in separate docker containers.
If this is so then referring to localhost (i.e. 127.0.0.1) is not correct. Because in this case IdentityServer tries to connect to itself. This would work if the IdentityServer have run on the host machine, since you forward SQL server port to it. But in your case, you should connect to the SQL server container IP instead.
Considering all above I see three options for you to solve this:
You can get ip address of SQL Server container by running docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <sql_container_name_or_id>
Run SQL container with static IP via docker run --ip <static_ip_value> <sql_container_name_or_id> and then use static ip you have specified in connection string.
Run SQL container with specified host name via docker run --hostname <sql_host_name> <sql_container_name_or_id> and then use specified hostname in the connection string.
It is up to you which way to go.
Use tcp, 127.0.0.1 and host port to connect. Mention in the identity server docker settings that it depends on sql database server container. Like this,
identityservice:
...
depends_on:
- sqldataservice
This way the database container will be made available first.
"ConnectionString": "Server=tcp:127.0.0.1,8433;Database=dbname;User Id=sa;Password=abc#1234;"
I ended up giving up on getting this to work using a single host, so I simply decided to have the SQL Server run in a separate machine.
I created a mssql-server-linux container in Docker that maps to port 4000 on the host.
Now, I'm connecting to this database, but I'm having an error.
How can i solve this problem?
Error message
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or could not be accessed. Verify that the instance name is correct, and that SQL Server is configured to allow remote connections. (provider:named Pipes provider, error:40-Unable to open connection to SQL Server)
enter image description here
I suggest to check your server port in iptables if it is in ACCEPT state. Maybe your server is blocking or rejecting others.
And try to ping your server ip to check if you can reach the server.
This is the first troubleshooting when i tried connecting Docker containers in MongoDB.
Thanks!
I have a .NET Core 1.1 app running in a Docker container on Ubuntu 14.04, and it fails to connect to the SQL Server database running on a separate server.
The error is:
Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 25 - Connection string is not valid)
I have deployed the same image with the same command line on another Ubuntu 14.04 server, and it connects fine.
A console app running on the problem server (outside of Docker) can connect with the same connection string.
As far as I can see from the documentation, an app running in a container has access to the external network by default, so what could be blocking this connection?
an app running in a container has access to the external network by default
It could have access only if a valid IP address is assigned to the container. Sometimes the IP which Docker choose for the container can conflict with external networks.
By default, containers run in bridge network, so look at it:
docker network inspect bridge
Find the container and check its IP.
To resolve conflicts, it is possible to customize bridge network and set bip parameter to change the network's IP range (config file location depends on host's OS):
"bip": "192.168.1.5/24"
Or create a new docker network.
Or experiment with net=host option: docker run network settings
Does this help?
Connect to SQL Server database from a docker container
Also, Googling this "docker connect to sql server database" seems to return a lot of helpful results.