Running MS SQL server in docker refuses connection - sql-server

I have issues connecting with MSSQL Server Management Studio to my running docker container.
A beginner in docker.
I have created a following docker-compose.yml file:
version: '3.4'
services:
sqlserver:
image: "mcr.microsoft.com/mssql/server:2017-latest"
volumes:
- /var/lib/docker/volumes/sql_volume/_data
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "pa55w0rd!"
MSSQL_PID: "Express"
ports:
- "1533:1534"
adventureworks_service:
image: ${DOCKER_REGISTRY-}adventureworksservice
build:
context: .
dockerfile: AdventureWorks_Service/Dockerfile
this docker file should start a MSSQL Database and the adventureworks_service project located in my solution.
this docker-compose is located in it's own Docker project, in the solution. created with the "add docker support" for my Web API project
assuming everything is correctly setup, I should be able to connect to the sql server on localhost:1533, using SQL server authentication with login Express and password pa55w0rd!.
I get following error right after attempting to connect (no timeouts, etc.)
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider : TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host) (Microsoft SQL Server, Error: 10054)
Does anyone know how I can access this server / container?
Thanks in advance

The port in mssql should be 1433 rather than 1534, try this
version: '3.4'
services:
sqlserver:
image: "mcr.microsoft.com/mssql/server:2017-latest"
volumes:
- /var/lib/docker/volumes/sql_volume/_data
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "pa55w0rd!"
MSSQL_PID: "Express"
ports:
- "1433:1433" # the port behind should always be 1433
adventureworks_service:
image: ${DOCKER_REGISTRY-}adventureworksservice
build:
context: .
dockerfile: AdventureWorks_Service/Dockerfile
then your connection string should be like this
Data Source=sqlserver,1433;User Id=SA;Password=pa55w0rd!

Try adding network_mode: "host" to your compose file.
version: '3.4'
services:
sqlserver:
image: "mcr.microsoft.com/mssql/server:2017-latest"
volumes:
- /var/lib/docker/volumes/sql_volume/_data
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "pa55w0rd!"
MSSQL_PID: "Express"
ports:
- "1533:1534"
network_mode: "host"
adventureworks_service:
image: ${DOCKER_REGISTRY-}adventureworksservice
build:
context: .
dockerfile: AdventureWorks_Service/Dockerfile

Related

Docker compose - can't connect to SQL Server database in SQL Server Management Studio

I am trying to connect to my DB that has been created in my Docker Compose file. It looks like the DB has been successfully created but I am unable to connect to it using SQL Server management studio.
Here is my Docker Compose file:
version: "3.9"
services:
web:
image: ${DOCKER_REGISTRY-}umbracoapp
build:
context: .\umbracoapp
dockerfile: Dockerfile
ports:
- "8000:80"
depends_on:
- db
db:
image: "mcr.microsoft.com/mssql/server"
environment:
SA_PASSWORD: "Testing1234!!"
ACCEPT_EULA: "Y"
Thanks
To access the database from outside the bridge network that Docker creates, you need to map it's port to a port on the host.
In the container, MSSQL uses port 1433. If that port is available on your host, you can map it to 1433 on the host by adding 'ports' to your db service like this
db:
image: "mcr.microsoft.com/mssql/server"
ports:
- "1433:1433"
environment:
SA_PASSWORD: "Testing1234!!"
ACCEPT_EULA: "Y"

How can I run SQL commands against dockerized SQL Server via docker-compose?

I have a SQL Server running inside a Docker container. It is setup via docker-compose file and tied to my .NET Core API:
version: '3.4'
services:
appdb:
image: mcr.microsoft.com/mssql/server
container_name: ${SQL_SERVER_CONTAINER_NAME}
restart: unless-stopped
ports:
- "${SQL_SERVER_HOST_PORT}:1433"
environment:
SA_PASSWORD: ${SQL_SERVER_USER_PASSWORD}
ACCEPT_EULA: "Y"
volumes:
- ./../../.microsoft/data:/var/opt/mssql/data
- ./../../.microsoft/log:/var/opt/mssql/log
- ./../../.microsoft/secrets:/var/opt/mssql/secrets
demoapi.website:
image: ${DOCKER_REGISTRY-}demoapiwebsite
build:
context: .
dockerfile: DemoAPI.Website/Dockerfile
container_name: ${APP_NAME}-app
environment:
- ASPNETCORE_ENVIRONMENT=Development
- "ConnectionStrings__AppConnectionString=Server=${SQL_SERVER_CONTAINER_NAME},${SQL_SERVER_HOST_PORT}; Initial Catalog=${APP_NAME};User ID=${SQL_SERVER_USER_ID};Password=${SQL_SERVER_USER_PASSWORD}"
depends_on:
- appdb
ports:
- "8000:80"
I also have a bunch of SQL script files inside my .NET Core API project. These scripts basically define how to create and seed the database when the project is started.
What I would like to do is to execute these scripts somehow once the dockerized SQL Server is up and running. Is it possible to do this via the docker-compose file?
I see a lot of tutorials which use the actual .NET app and do this during the app start up with EF migrations, but I would prefer to get this done via docker-compose, if possible?

How can I use Drupal8 by using SQL Server with Docker

I am trying use docker build drupal-8, but I want SQL Server to be my database
Now, I using docker-compose to setting but it's doesn't work. It's still only
here is my docker-compose.yml file txt
version: "2"
services:
web:
image: drupal:8
ports:
- "83:80"
links:
- db
restart: always
db:
image: microsoft/mssql-server-linux
volumes:
- mssqlTest:/var/opt/mssql/data
volumes:
mssqlTest:

How to create Laravel project with docker and connect with local sqlserver db

How to build Laravel project with Docker, but connect to already created sqlserver database. So I have sqlserver database on my local machine, and I starting container with docker-compose.
time-track:
tty: true
image: bitnami/laravel:6-debian-9
ports:
- 3002:3000
volumes:
- ../time-track/:/app
# privileged: true # Privileged mode could be required to run this container under Windows
If you create a custom Docker network bridge like:
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 my_network
_
services:
time-track:
tty: true
image: bitnami/laravel:6-debian-9
ports:
- 3002:3000
volumes:
- ../time-track/:/app
networks:
- my_network
networks:
my_network:
external: true
And using your IP and your port binded for SQLServer ? Is that working ?

.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