Can not connect to the azure-sql-edge. Why? - sql-server

I have the following docker-compose.yml:
version: "3.9"
services:
# Database instance
mssql:
image: mcr.microsoft.com/azure-sql-edge:latest
volumes:
- events_mssql:/var/opt/mssql
ports:
- 1433:1433
environment:
- ACCEPT_EULA=1
- MSSQL_SA_PASSWORD=Passw#rd
volumes:
events_mssql:
setup.sql:
CREATE DATABASE $(MSSQL_DB);
CREATE DATABASE $(MSSQL_DB_AUDIT_LOG);
GO
USE $(MSSQL_DB);
GO
CREATE LOGIN $(MSSQL_USER) WITH PASSWORD = '$(MSSQL_PASSWORD)';
GO
CREATE USER $(MSSQL_USER) FOR LOGIN $(MSSQL_USER);
GO
ALTER SERVER ROLE sysadmin ADD MEMBER [$(MSSQL_USER)];
GO
Here is the running container:
I am trying to connect to the database through the Visual Studio Code extension:
Using this connection string: Server=http://localhost:1433/;Database=master;User Id=sa;Password=Passw#rd;
I am getting the error:
mssql: Error: Unable to connect using the connection information provided. Retry profile creation?
What am I doing wrong here?

Related

Connecting to host mysql database using via sock

I am trying to connect to a mysql database on my host using a docker container (so I can use it in other containers).
I would like to do it this way, because I cannot connect to the database from a docker container -> throws Connection refused because the IP is not allowed.
I tried mounting the sock file using following compose:
version: '3'
services:
mysql:
image: mariadb:10.3
volumes:
- /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
networks:
- database
networks:
database:
external: true
but it's failing on (even though the host database already contains many databases):
2022-06-07 20:14:46+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of MARIADB_ROOT_PASSWORD, MARIADB_ALLOW_EMPTY_ROOT_PASSWORD and MARIADB_RANDOM_ROOT_PASSWORD,
and when I added the volume: - /var/lib/mysql:/var/lib/mysql, I am getting a Connection refused error (as if I was connecting to it normally and not via unix)
Here you go, consider checking the documentation before asking thoses types of questions
https://hub.docker.com/_/mariadb
version: 3
services:
sql:
image: mariadb:10.3
container_name: <container_name>
restart: always
networks:
- database
volumes:
- <volume>:/var/lib/mysql
- /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock
environment:
MARIADB_RANDOM_ROOT_PASSWORD: 1
MARIADB_USER: <USER_TO_BE_CREATED>
MARIADB_DATABASE: <DATABASE_TO_BE_CREATED>
MARIADB_PASSWORD: <PASSWORD_TO_BE_CREATED>
networks:
database:
external: true

Why can't connect my .net core container to the SQL Server docker container?

It fails to connect to the SQL Server container, even if the backend is running in local and the database in docker.
I tested with Azure SQL and everything worked perfectly.
Error(s):
After docker-compose up, and try to add element to the list:
2021-07-13 21:43:32.79 Logon Login failed for user 'sa'. Reason: Failed to open the explicitly specified database 'AsdDB'. [CLIENT: XXX.168.XX.3]
backend | fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
backend | An error occurred using the connection to database 'AsdDB' on server 'sqlserver'.
backend | fail: Microsoft.EntityFrameworkCore.Query[10100]
backend | An exception occurred while iterating over the results of a query for context type 'Asd.Db.AppDbContext'.
backend | Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "AsdDB" requested by the login. The login failed.
backend | Login failed for user 'sa'.
dotnet ef update database
dotnet ef database update --project Asd.Api.csproj
Build started...
Build succeeded.
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Backend Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /app
EXPOSE 80
COPY *.sln .
COPY Asd.Api/*.*.csproj ./Asd.Api/
COPY Asd.Db/*.*.csproj ./Asd.Db/
RUN dotnet restore
COPY Asd.Api/. ./Asd.Api/
COPY Asd.Db/. ./Asd.Db/
WORKDIR /app/Asd.Api
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS runtime
WORKDIR /app
COPY --from=build /app/Asd.Api/out ./
ENTRYPOINT ["dotnet", "Asd.Api.dll"]
Docker compose:
version: "3.8"
services:
backend:
container_name: backend
build:
context: ./backend/
dockerfile: Dockerfile
depends_on:
- db
ports:
- "5000:80"
db:
image: "mcr.microsoft.com/mssql/server:2017-latest"
container_name: sqlserver
hostname: sqlserver
environment:
SA_PASSWORD: "ASDFGHJ12345"
ACCEPT_EULA: "Y"
restart: unless-stopped
ports:
- "1433:1433"
Connection string in the appsettings.json
"ConnectionStrings": {
"DefaultConnection": "Server=sqlserver;Database=AsdDB;user id=sa;password=ASDFGHJ12345;"
},
In the connection string, the Server had to be passed the following value:
Server=host.docker.internal;Database=AsdDB;user id=sa;password=ASDFGHJ12345%;
So now it works, it took 2 days of messing with it, but after I posted the question, I found the answer.

PostgreSQL database is not created using keycloak

I need to create a new external PostgreSQL database to replace the default H2 database that Keycloak uses by default. When I create a database using the keycloak file, the database is not created when running Keycloak:
Caused by: org.postgresql.util.PSQLException: FATAL: database "sso" does not exist
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2559)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:133)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:250)
at org.postgresql.jdbc#42.2.5//org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc#42.2.5//org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.jdbc#42.2.5//org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.jdbc#42.2.5//org.postgresql.Driver.connect(Driver.java:256)
at org.jboss.ironjacamar.jdbcadapters#1.4.22.Final//org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:321)
... 57 more
When I am giving the application database as the value I am able to connect to the database. But my intention is to connect to a different database:
cat Keycloak
CREATE DATABASE IF NOT EXISTS sso;
docker-compose.yaml
data-keycloak:
image: jboss/keycloak:$KEYCLOAK_TAG
container_name: data-keycloak-container
ports:
- "$KEYCLOAK_EXTERNAL_PORT:$KEYCLOAK_INTERNAL_PORT"
depends_on:
- data-keycloak-db
environment:
KEYCLOAK_USER: $KEYCLOAK_USER
KEYCLOAK_PASSWORD: $KEYCLOAK_PASSWORD
DB_VENDOR: $KEYCLOAK_DB_VENDOR
DB_USER: $KEYCLOAK_DB_USER
DB_PASSWORD: $KEYCLOAK_DB_PASSWORD
DB_DATABASE: $KEYCLOAK_DB_DATABASE
DB_ADDR: $KEYCLOAK_DB_ADDR
DB_SCHEMA: public
WAIT_HOSTS: data-keycloak-db:$data_KEYCLOAK_DB_PORT
KEYCLOAK_IMPORT: /tmp/keycloak-realm.json
volumes:
- ./keycloak-realm.json:/tmp/keycloak-realm.json
- ./keycloak:/tmp/keycloak
networks:
- $data_NETWORK

Cannot access to dockerized SQL Server instance

I have simple docker-compose.yml which contains two services only, my-api and sql-server.
version: '3.0'
services:
sql-server:
image: mcr.microsoft.com/mssql/server:2019-latest
hostname: sql-server
container_name: sql-server
ports:
- "1433:1433"
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=MyPassword01*
- MSSQL_PID=Express
my-api:
ports:
- "8080:5000"
depends_on:
- sql-server
... ommited for clarity
When I docker-compose up --build the containers are ready (I can verify with docker ps)
a7a47b89a17a mcr.microsoft.com/mssql/server:2019-latest "/opt/mssql/bin/perm…" 12 minutes ago Up 11 minutes 0.0.0.0:1433->1433/tcp sql-server
but I cannot access my SQL Server using SSMS.
SSMS login window:
Server Name: localhost,1433
Authentication: SQL Server Authentication
Username: sa
Password: MyPassword01*
Error:
Cannot connect to localhost,1433.
Login failed for user 'sa'. (.Net SqlClient Data Provider)
PS: I also tried with
Server Name: sql-server,1433
but still cannot access
Execute the below code which will display the public ipaddress.
Instead of localhost use this ipaddress
docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" mssqltrek-con1.
I followed the below link to achieve the same.
https://www.sqlshack.com/sql-server-with-a-docker-container-on-windows-server-2016/
I guess you have to debug further. The first thing I guess you can do is to open the bash inside the container and try to connect to your SQL database from the container.
docker exec -it sql-server "bash"
Once inside the container bash, then connect with sqlcmd,
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "<YourNewStrong#Passw0rd>"
If you fail to connect inside the container, then I have to assume your SA password is somehow different when you set up the SQL server. But if this is not an issue, that means, you can connect to SQL server inside the container, then you can be rest assured it is something wrong with SSMS on port 1433 in your computer. Make sure the server name is correct.

.NET Core API can't connect to database via docker-compose

I'm developing a .NET core web API with a MSSQL Server database. I tried to containerize this into a Docker container and use Docker Compose to spin up this service. But my API cannot connect to the database.
The following error occurs:
Application startup exception: 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)
Code in my startup:
services.AddDbContext<ProductServiceDbContext>(options =>
options.UseSqlServer("server=sqlserver;port=1433;user id=sa;password=docker123!;database=ProductService;"));
Dockerfile:
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore dockerapi.csproj
COPY . ./
RUN dotnet publish dockerapi.csproj -c Release -o out
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "dockerapi.dll"]`
Docker-compose:
version: '3.4'
services:
productservice:
image: productservice/api
container_name: productservice_api
build:
context: ./ProductService/ProductService
depends_on:
- sqlserver
ports:
- "5000:80"
sqlserver:
image: microsoft/mssql-server-linux:latest
container_name: sqlserver
ports:
- "1433"
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=docker123!
I've tried several things:
Add links in the docker-compose file (sqlserver in productservice)
Add networks to the docker-compose file
Changed the connection string to
"server=sqlserver;user id=sa;password=docker123!;database=ProductService;" or "server=sqlserver,1433;user id=sa;password=docker123!;database=ProductService;"
Hey I was having a similar issue and stumbled upon your post, this was not the same issue I was having but I think I know the problem you were having (sorry this reply is so late)
Looking at your connection string server=sqlserver;port=1433;user id=sa;password=docker123!;database=ProductService;, you actually have to specify the port another way: server=sqlserver,1433;user id=sa;password=docker123!;database=ProductService;
Hope this helps!
Try these changes:
sqlserver:
image: microsoft/mssql-server-linux:latest
container_name: sqlserver
ports:
- "1433:1433" # map the ports
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=docker123!
sqlserver:
image: "microsoft/mssql-server-linux:latest"
extra_hosts:
- "localhost:192.168.65.2"
container_name: sqlserver
hostname: sqlserver
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "Your_password123"
ports:
- "49173:1433"
Changed the connection string to
"localhost,49173;Database=ProductService;User
Id=sa;Password=Your_password123;
Try another port because it can cause a conflict with an existing one. e.g. "1433:1401";
Disable your antivirus for a while and see if it works;
Create a catalog with the name "ProductService" like defined in your appSettings.json before run the container;
I have not tested this. Try at your own risk.

Resources