首页 > 数据库 >Run sql server in docker

Run sql server in docker

时间:2023-12-05 10:25:22浏览次数:39  
标签:Run name -- server 2022 docker mssql

Pull docker image

Pull the latest image of SQL Server 2022

docker pull mcr.microsoft.com/mssql/server:2022-latest

Run in container

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<password>" -p 11433:1433 --name <sqlserver2022> --hostname <sqlserver2022> -d mcr.microsoft.com/mssql/server:2022-latest

It's recommended to use the same string for 'name' and 'hostname', could be 'sqlserver2022' or anything you like.

Connect to SQL Server

1. Start an interactive bash shell inside your running container

docker exec -it sqlserver2022 "bash"

2. Inside the bash shell, connect locally with 'sqlcmd'

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "<password>"

3. You can connect with SSMS too,  using the public IP address, followed by comma separator and then the port (xxx.xx.xx.xxx,port)

 Persist DB

Using data volume containers

docker volume create sqlserver-2022
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<password>" -p 11433:1433 --name <sqlserver2022> --hostname <sqlserver2022> -v sqlserver-2022:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2022-latest

The target should be '/var/opt/mssql', otherwise it won't work.

Mount a host directory as data volume

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<password>" -p 11433:1433 --name <sqlserver2022> --hostname <sqlserver2022> -v <host directory>/data:/var/opt/mssql/data -v <host directory>/log:/var/opt/mssql/log -v <host directory>/secrets:/var/opt/mssql/secrets -d mcr.microsoft.com/mssql/server:2022-latest

Connect to SQL Server from another container

1. Run the container with the specified network <network-name>

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<password>" -p 11433:1433 --name <sqlserver2022> --hostname <sqlserver2022> -v sqlserver-2022:/var/opt/mssql --network <network-name> --network-alias <network-alias> -d mcr.microsoft.com/mssql/server:2022-latest

 2. Update app's DB connection string, the host name in 'Data Source' should be the <network-alias> defined in the previous step 

"ConnectionStrings": {
    "Xxx-Context": "Data Source=<network-alias>,1433;Database=<DB-name>;User ID=sa;Password=<password>;MultipleActiveResultSets=true;Encrypt=False" 
}

3. Run the app container in the same network <network-name>

docker run -p 32774:80 --name <app-name> --network <network-name> -d <app-image>

 

Instead of launching each container separately, you could also choose docker-compose to launch all the containers with a single command, and no need to specifically create the network, docker-compose will create it automatically for you.

services:
  <service-name>: //service-name automatically become a network alias 
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: <container-name>
    ports:
      - 21433:1433
    environment:
      - ACCEPT_EULA=y
      - MSSQL_SA_PASSWORD=<password>
    volumes:
      - <volume-name>:/var/opt/mssql

  <service-name>:
    image: <app-image-name>
    container_name: <container-name>
    ports:
      - 32774:80

volumes:
  <volume-name>: 
    external: true //mark external to 'true' to use a volume outside the compose project, otherwise a new [COMPOSE_PROJECT_NAME]_<volume-name> volume will be automatically created and used.

networks:
  default: // The auto created network is called [COMPOSE_PROJECT_NAME]_default by default, you can custmized it in networks attribute
    name: <customized-network-name>

The [COMPOSE_PROJECT_NAME] environment is the compose file's parent folder name by default, while you can set/redefine it through the command line parameter '-p', or use the .env file to make it trackable.

COMPOSE_PROJECT_NAME=<costomized-project-name>

Finally, run the compose.yaml file to launch all containers

docker compose up -d

 

References:

https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver16&pivots=cs1-bash

标签:Run,name,--,server,2022,docker,mssql
From: https://www.cnblogs.com/Tiger-Lu/p/17845215.html

相关文章

  • Docker CLI docker restart 常用命令
    Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows操作系统的机器上,也可以实现虚拟化。Docker是内核虚拟化,不使用Hypervisor是不完全虚拟化,依赖内核的特性实现资源隔离。本文主要介绍DockerCLI中......
  • Docker CLI docker rename 常用命令
    Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows操作系统的机器上,也可以实现虚拟化。Docker是内核虚拟化,不使用Hypervisor是不完全虚拟化,依赖内核的特性实现资源隔离。本文主要介绍DockerCLI中......
  • Docker部署lsky-pro
    一、简介LskyPro是一个用于在线上传、管理图片的图床程序,中文名:兰空图床,你可以将它作为自己的云上相册,亦可以当作你的写作贴图库。二、搭建lsky-pro绿联DX4600为例,首先我们打开Docker管理器,进入镜像管理,然后在镜像仓库中搜索halcyonazure/lsky-pro-docker​,选择latest......
  • docker 安装
    1.卸载历史版本。yumremovedocker\docker-client\docker-client-latest\docker-common\docker-latest\docker-latest-logrotate\docker-logrotate\docker-selinux\docker-engine-selinux\docker-engine\docker-ce2.安装所需软件包yuminstall-yyum-utils或......
  • SQL SERVER 查询死锁
    DECLARE@SessionNameSysNameSELECT@SessionName='system_health'IFOBJECT_ID('tempdb..#Events')ISNOTNULLBEGINDROPTABLE#EventsENDDECLARE@Target_FileNVarChar(1000),@Target_DirNVarChar(1000),@Target_File......
  • centos7.9以docker形式安装redmine,并配置
    一、准备准备服务器联网,并且安装好docker二、安装redmine执行命令拉取docker镜像dockerpullredmine:latestRunRedminewithSQLite3dockerrun-d--nameredmine-p3000:3000redmine:latest启动后访问:http://localhost:3000/,默认账号/密码:admin/admin  进入后......
  • docker发布ASPNETCore项目 yum
    docker手动发布ASP.NETCore7一、环境准备环节1、准备Linux系统Linux系统-CentOS7---基于虚拟机来安装IP:192.168.1.972、安装docker环境2.1、卸载依赖包$sudoyumremovedocker\docker-client\docker-client-latest\......
  • docker compose 一键部署zookeeper集群
    compose.yml文件services:zoo1:image:zookeeper:3.8.3restart:alwayshostname:zoo1ports:-2181:2181environment:ZOO_MY_ID:1ZOO_SERVERS:server.1=0.0.0.0:2888:3888;2181server.2=zoo2:2888:3888;2181server.3=z......
  • runsql
    CustomSqlMapper.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.runsh......
  • docker container中变更timezone
    当前使用了playwright官方python镜像: https://playwright.dev/python/docs/docker但在实际使用时,时间总是显示为UTC0时间 正好相差8个小时,前面是jenkins打印时间,后面部分是container内部时间查了网上各种方法,总共有几种:1,直接加命令行:dockerrun-eTZ=Asia/Shanghai2......