I've created a docker container using the following command:
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStr#ngPassw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 -d microsoft/mssql-server-linux:latest
I'd like to use SSMS to connect to this database from my host machine.
Which parameters should I use in the SSMS login dialog?
My current entries:
Server type: Database Engine
Server name: [machine-name]
Authentication: SQL Server authentication
Login: sa
Password: MyStr#ngPassw0rd
But I'm getting this error:
TITLE: Connect to Server
Cannot connect to localhost:.
ADDITIONAL INFORMATION:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)
network path not found
Seems I'm not able to 'see' the database.
Is there something wrong with my connection string or is it not possible to connect this way?
If you are using Docker CE on Windows with Linux containers, specify double-quotes around the environment variables so that they are recognized:
docker run -e "ACCEPT_EULA=Y" -e 'SA_PASSWORD=MyStr#ngPassw0rd" -e "MSSQL_PID=Express" -p 1433:1433 -d microsoft/mssql-server-linux:latest
You will then be able to connect using SSMS. With the default 1433 port in this case, you can specify . or localhost as the server name. You can also connect to the container instance from a remote host specifying only your machine name (assuming firewall rules allow incoming port 1433 traffic).
With single-quotes, the values are not recognized and the container will immediately stop. You can use docker logs <container-name> to view messages from the SQL Server error log for troubleshooting. Below is an example using an explict container name (which I recommend to facilitate tasks like this) using your original problem command:
docker run --name sql1 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStr#ngPassw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 -d microsoft/mssql-server-linux:latest
c3de002fc22106e53c9b165c7f444905b5ef1bb1596eddf1b097cdd6d60e6c75
docker logs sql1
The SQL Server End-User License Agreement (EULA) must be accepted
before SQL Server can start. The license terms for this product can be
downloaded from http://go.microsoft.com/fwlink/?LinkId=746388.
You can accept the EULA by specifying the --accept-eula command line
option, setting the ACCEPT_EULA environment variable, or using the
mssql-conf tool.
As you can see from the logged message, the SQL instance stopped (stopping the container) because the SQL Server did not recognize the EULA was accepted due to the single quotes.
Related
My Docker linux SQL Server is not working today at my machine.
I am not sure if this is firewall (I have off them all), or Docker settings (as I just upgraded to the latest Docker version), or a Docker SQL Server issue (but this was working fine on the same machine earlier).
Could anyone help?
I have tried using bash,
/opt/mssql-tools/bin/sqlcmd -S localhost,8010 -U SA -P Test123!
Error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
The docker compose file
version: '3'
services:
mssql:
network_mode: lsvc
image: microsoft/mssql-server-linux:2017-latest
container_name: mssql
hostname: mssql
volumes:
- ./.db:/var/opt/mssql/
- /var/opt/mssql/data
- ./sqlinit.sql:/scripts/sqlinit.sql
ports:
- 8010:1433
environment:
- ACCEPT_EULA=Y
- MSSQL_SA_PASSWORD=Test123!
command:
- /bin/bash
- -c
- |
# Launch MSSQL and send to background
/opt/mssql/bin/sqlservr &
# Wait for it to be available
echo "Waiting for MS SQL to be available"
/opt/mssql-tools/bin/sqlcmd -l 30 -S mssql -h-1 -V1 -U sa -P Test123! -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , ##servername"
is_up=$$?
while [ $$is_up -ne 0 ] ; do
echo -e $$(date)
/opt/mssql-tools/bin/sqlcmd -l 30 -S mssql -h-1 -V1 -U sa -P Test123! -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , ##servername"
is_up=$$?
sleep 1
done
# Run every script in /scripts
# TODO set a flag so that this is only done once on creation,
# and not every time the container runs
#for foo in /scripts/*.sql
/opt/mssql-tools/bin/sqlcmd -S mssql -U sa -P Test123! -l 30 -e -i /scripts/sqlinit.sql
#done
# So that the container doesn't shut down, sleep this thread
sleep infinity
I would suspect that the mssql instance had failed to start.
Looking at your docker file when the server fail to start it runs into in infinite loop. I would advice you to limit the number of retries on the loop so you will have an indication that the server is failing on startup.
Or better consider the use of the HEALTHCHECK option and not the loop script
see https://blog.couchbase.com/docker-health-check-keeping-containers-healthy/
To troubleshooting the problem try
docker logs <mssql-container-id>
if this doesn't provide enough information try connecting to the container as this was a simple machine.
docker exec -it <mssql-container-id> bash
Look for errors in /var/opt/mssql/log/errorlog
See https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-troubleshooting-guide?view=sql-server-ver15
i found the root cause, it is not due to docker mssql linux
but the latest Docker Desktop Community 2.2.0.0
after uninstall it then downgrade to old version, Docker Desktop Community 2.1.0.5.
it is working now, connect to mssql success.
not sure what is new in latest docker desktop, anyway...i will use old version now.
also one important point to take note, the ".db" folder (windows path where the yml file reside), must deleted before build the docker image.
I've downloaded and ran this Docker image mcr.microsoft.com/mssql/server:
docker run -d -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Password!1" -p 1433:1433 --name sqlserver-test -d mcr.microsoft.com/mssql/server
up this point everything is OK.
Then I go to the SQL Server Management Studio and try to connect to localhost with user sa, but something is wrong because SSMS throws an exception that says something about network error.
Any ideas?
Try connecting to localhost, 1433 Also, confirm you don't have a real install of SQL Server on your machine trying to use port 1433. You might need to map port 1434->1433 and then connect with localhost, 1434
To run SQL Server on my Mac, I installed Docker and SQL Server Management Studio (Azure Data Studio) and got connected to the server. However, when I tried to reconnect another time, I got the error below. I've found much advice about how to fix the issue on Windows, but not on Mac.
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)
Increase Docker memory from 2 GB to 4 Gb from Preferences then again run the following commands,
docker pull microsoft/mssql-server-linux:2017-latest
Now, launch an instance of the Docker image.
docker run -d --name name_your_container -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=P#55w0rd' -e 'MSSQL_PID=Developer' -p 1433:1433 microsoft/mssql-server-linux:2017-latest
I am trying to connect to sql server using docker. I have successfully enabled the container using this command
sudo docker run -d --name aakash -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Maximus6hero#" -p 1434:1434 microsoft/mssql-server-linux
docker ps
3e41723b93d5 microsoft/mssql-server-linux "/opt/mssql/bin/sqls…" About an hour ago Up About an hour 1433/tcp, 0.0.0.0:1434->1434/tcp aakash
It shows that my container is running.
But when i try to connect using db visualizer it throws an error.
The TCP/IP connection to the host localhost, port 1434 has failed. Error: "The driver received an unexpected pre-login response. Verify the connection properties and check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. This driver can be used only with SQL Server 2000 or later.". ClientConnectionId:6a802ae0-7203-411d-a599-4c1de997d843.
I also have windows pc with me, and i can connect easily using sql server management studio. Even i can connect others pc using that windows pc. But when i try to connect my mac to windows pc using its ip it also gives the same error.
I have enabled tcp/ip connection on windows using sql server configuration manager. But there is no such thing on mac.
You might be getting this error on your MAC because when you run the docker in the background, it uses only 2GB of memory by default which is insufficient to run the SQL server as it needs minimum of 3.25 GB and ideally, we should point it to 4GB of memory. Update the preferences section with above details on your docker. Save and restart the docker and you may check the below steps to see if that might help you for your MAC. I did this using Azure Data Studio and Docker.
Once you have Saved and restart the docker with 4GB of Memory Allocation for docker to run, all you'd need to do is pull the docker image of the sql server and download it. this can be done by below commands on your terminal . FYI, I am using bash commands below:
Command 1:
sudo docker pull mcr.microsoft.com/mssql/server:2017-latest
This will pull the latest vesion docker image and download. Once done, you need to set your SQL authentication on the server for your database. Follow below commands:
Command 2:
sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<SetYourPasswordHere>' \
-p 1433:1433 --name sql1 \
-d mcr.microsoft.com/mssql/server:2017-latest
This sets your password and uses the port 1433 for SQL server (which is the default port). To confirm if the image has been created and the SQL server is running on docker, execute the below command to check log(s).
Command 3:
docker ps
To check all instances in your history of dockers( i.e. if you already had dockers installed before you are attempting this SQL connection/execution), run the below command and it will give you all the logs of all instances you have created
Command 4:
docker ps -a
or
docker ps -all
Once, you have completed above steps and see that the docker has created SQL instance, you need to go to Azure Data Studio and set the below credentials to access the server that you just created above using Docker.
Server: localhost
Authentication Type: SQL Authentication
Username: sa
Password: <Check Command 2 to see what you entered in the password where it says SetYourPasswordHere>
Hope this helps in your tryst with running SQL server on your MAC. All the Best!
I have a .net core 2.0 project which uses mssql server. I have Created a docker image and container for my .net core 2.0 and running on 9090:9090. I created it like below.
docker container run --name mytestapp --publish 9090:9090 --detach my_.netapp_image_name
and below is my connection string in .net core 2.0 app.
"DefaultConnection": "Server=127.0.0.1;Database=mydatabase;UserId=SA;Password=mydbpassword"
before this, I created a container for mssql server with below,
docker container run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<YourStrong!Passw0rd>' \
-p 1433:1433 --name sql1 \
-d microsoft/mssql-server-linux:2017-latest
my .net core app has seeds for database. each time it gives me an error says
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: 35 - An internal exception was caught) --->
System.AggregateException: One or more errors occurred. (Connection
refused 127.0.0.1:1433) --->
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException:
Connection refused 127.0.0.1:1433
NOTE: this works fine when I run my .net app via IDE(visual studio) and use db as docker mssql container. I ran these two containers separately. then I tried to run using docker-compose, but didn't work.
What am I doing wrong here. hope your help with this.
Containers each have their own network namespace by default. Compose will place all containers on a shared network and set an alias in DNS for the service name. So to connect between containers, all you need to do is point to your service name instead of the 127.0.0.1 (assuming mysql is your service name):
"DefaultConnection": "Server=mysql;Database=mydatabase;UserId=SA;Password=mydbpassword"
This is more portable and handles containers scaling/updating better than to attaching containers to the same network namespace.
So the issue here is the docker sandboxing. For each container that you run you can think of as different virtual environment that has its own host name, IP address and network. While using -p only forwards port form that internal network to host. So while you are running from VS you can point to your db using localhost (127.0.0.1:1433) just because you have exposed that port to host and your application is starting on host directly. When it is runing inside its own container localhost no longer refers to host but rather to that docker environment. To fix this you can run both containers in the same network (--network argument on run) and refer from one to another by host name (--name argument on run).
docker container run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<YourStrong!Passw0rd>' \
-p 1433:1433 --name sql1 \
-d microsoft/mssql-server-linux:2017-latest \
--name sql_server
docker container run \
--name mytestapp
--publish 9090:9090
--detach my_.netapp_image_name
--network container:sql_server
and in your settings refer to your database as sql_server.
To make this process less painful you can research docker-compose.