一、下载镜像

docker pull microsoft/mssql-server-linux

二、启动容器

docker-compose

version: '3'
services:
  sqlserver:
    image: microsoft/mssql-server-linux:latest
    restart: always
    privileged: true
    container_name: sqlserver
    ports:
    - 1433:1433
    volumes:
      - ./data:/var/opt/mssql/data
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: aA123456
# 启动
docker-compose up -d && docker logs -f sqlserver
image-20201224142122787

验证是否成功

-- 进入容器
docker exec -it sqlserver /opt/mssql-tools/bin/sqlcmd -S localhost -U sa
-- 查询版本
SELECT @@VERSION
go
-- 查询所有数据库
SELECT Name FROM Master..SysDatabases ORDER BY Name
go
-- 查询相关的数据表
SELECT Name FROM master..SysObjects Where XType='U' ORDER BY Name
go
image-20201224142949446

三、连接服务

测试连接

image-20201224112722835

找到navicat目录下的sqlncli_x64.msi文件

image-20201224135955326

点击安装

image-20201224140027831

接受许可

image-20201224140243952

下一步

image-20201224140255980

安装完整功能

image-20201224140440169

安装

image-20201224140536631

完成

image-20201224140551021

连接成功

image-20201224140746841

测试是否成功

-- 查询版本
SELECT @@VERSION
image-20201224141201819

四、常见问题

4.1 内存太小

问题:

docker run --name sqlserver --restart=always -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=aA123456' -p 1433:1433 -d microsoft/mssql-server-linux
image-20201224142203667

解决:

# 提示内存太小的,修改内存限制,这里改成512m
docker cp sqlserver:/opt/mssql/bin data/bin
# 备份文件
mv sqlservr sqlservr.old
# 使用python修改
python
oldfile = open("sqlservr.old", "rb").read()
newfile = oldfile.replace("\x00\x94\x35\x77", "\x00\x80\x84\x1e")
open("sqlservr", "wb").write(newfile)
exit()
# 授权
chmod 755 sqlservr
image-20201224144414110

挂载文件到容器

version: '3'
services:
  sqlserver:
    image: microsoft/mssql-server-linux:latest
    restart: always
    privileged: true
    container_name: sqlserver
    ports:
    - 1433:1433
    volumes:
      - ./data/data:/var/opt/mssql/data
      - ./data/bin:/opt/mssql/bin
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: aA123456