无法连接到Linux Docker主机上的SQL Server(远程主机强制关闭连接)

lp0sw83n  于 2023-05-22  发布在  Docker
关注(0)|答案(1)|浏览(392)

当我想连接到在Docker容器中运行的SQL Server示例时,我收到以下错误:
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.)

SQL Dockerfile:

FROM mcr.microsoft.com/mssql/server:2019-latest

USER root

RUN mkdir /var/opt/sqlserver
RUN mkdir /var/opt/sqlserver/sqldata
RUN mkdir /var/opt/sqlserver/sqllog
RUN mkdir /var/opt/sqlserver/sqlbackups

RUN chown -R mssql /var/opt/sqlserver

USER mssql

CMD /opt/mssql/bin/sqlservr

docker-compose

version: '3.7'

networks:
  personal-money-tracker-network:
    driver: bridge

services:

  api:
    container_name: personal-money-tracker-api
    image: ${DOCKER_REGISTRY-}personalmoneytrackerapi
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_CONNECTION_STRING=PersonalMoneyTrackerDbDocker
      - ASPNETCORE_URLS=https://+:443;http://+:80
    networks:
      - personal-money-tracker-network
    build:
      context: .
      dockerfile: ./server/PersonalMoneyTracker.API/Dockerfile
    depends_on:
      - db
    ports:
      - "8000:80"
      - "8001:443"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${USERPROFILE}\.aspnet\https:/root/.aspnet/https:ro

  client:
    container_name: personal-money-tracker-client
    stdin_open: true
    image: ${DOCKER_REGISTRY-}personalmoneytrackerclient
    build:
      context: .
      dockerfile: ./client/Dockerfile.prod
    environment:
      - NODE_ENV=production
      - CHOKIDAR_USEPOLLING=true
      - VITE_API_URL=https://localhost:8001
    networks:
      - personal-money-tracker-network
    ports:
      - "3000:3000"
    depends_on:
      - api
    volumes:
      - node-modules:/app/node_modules
      - ./PersonalMoneyTracker.Client/personal-money-tracker:/app

  db:
    container_name: personal-money-tracker-db
    image: ${DOCKER_REGISTRY-}personalmoneytrackerdb
    ports:
      - "1401:1433"
    build:
      context: .
      dockerfile: ./devops/sql/Dockerfile
    env_file:
      - ./devops/sql/SqlServer.env
    networks:
      - personal-money-tracker-network
    volumes:
      - sqlsystem:/var/opt/mssql/
      - sqldata:/var/opt/sqlserver/sqldata
      - sqllog:/var/opt/sqlserver/sqllog
      - sqlbackup:/var/opt/sqlserver/sqlbackups

volumes:
  node-modules:
  sqlsystem:
  sqldata:
  sqllog:
  sqlbackup:

SQL .env文件:

MSSQL_SA_PASSWORD=Testing1122!
ACCEPT_EULA=Y
MSSQL_AGENT_ENABLED=True
MSSQL_DATA_DIR=/var/opt/sqlserver/sqldata
MSSQL_LOG_DIR=/var/opt/sqlserver/sqllog
MSSQL_BACKUP_DIR=/var/opt/sqlserver/sqlbackups
MSSQL_PID='Developer'
MSSQL_TCP_PORT=1401

有趣的事情-一切工作与此配置,直到我不得不杀死WSL,以shring卷和更改CPU/RAM资源(.wslconfig),但我不明白为什么要连接。当我尝试从SSMS或Azure Data Studio连接时,也出现相同的错误。

flvlnr44

flvlnr441#

因此,该问题与已经在端口1433上运行SQL服务示例有关,同时尝试在同一外部端口上激活Docker。如果您遇到类似的问题,您可以使用TCPView检查端口(下面的屏幕截图)。

你可以杀死进程,然后运行容器,一切都会像魔法一样工作。

相关问题