通过Docker中的脚本创建SQL

通过Docker中的脚本创建SQL

本文介绍了通过Docker中的脚本创建SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个简单的问题。我什么都找不到。

Simple question I hope. I cannot find anything anywhere.

如何在 Microsoft SQL Server中创建数据库 Docker 容器?

Dockerfile

我正在查看以下 Dockerfile

FROM microsoft/mssql-server-windows-developer:latest

ENV sa_password ab873jouehxaAGR

WORKDIR /
COPY /db-scripts/create-db.sql .

# here be the existing run commnd in the image microsoft/mssql-server-windows-developer:latest
#CMD ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';", ".\\start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs \\\"$env:attach_dbs\\\" -Verbose" ]

RUN (sqlcmd -S localhost -U SA -P ab873jouehxaAGR -i create-db.sql)

docker-compose.yml

我整理了以下docker compose文件:

I have put together the following docker compose file:

version: '3.4'

services:
  sql.data:
    image: ${DOCKER_REGISTRY}myfirst-mssql:latest
    container_name: myfirst-mssql_container
    build:
      context: .
      dockerfile: ./Dockerfile
    environment:
      SA_PASSWORD: ab873jouehxaAGR
      ACCEPT_EULA: Y

将它们放在一起

我正在运行命令 docker-compose up 针对以上内容。并假设 create-db.sql 文件将仅创建一个与此处无关的数据库,以使事情保持最少。

I am running the command docker-compose up against the above. And assuming create-db.sql file will simply create a database which is not relevant here to keep things minimal.

错误

我上面得到的错误是,当SA运行 .sql时,SA的登录无效。 / code>脚本:

The error I get above is that the login for SA is invalid when it runs the .sql script:

Step 7/7 : RUN (sqlcmd -S localhost -U SA -P ab873jouehxaAGR -i create-db.sql)
 ---> Running in 2ac5644d0bd9
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'SA'..

在密码更改为ab873jouehxaAGR之前,它看起来像是在运行,它通常看起来像是 mssql-server-windows-developer:latest.json 从检查vscode中的映像开始- \start -sa_password $ env:sa_password -ACCEPT_EULA $ env:ACCEPT_EULA -attach_dbs 实际上是

It looks like this runs before the password has been changed to ab873jouehxaAGR which typically looks like the command from mssql-server-windows-developer:latest.json from inspecting the image in vscode - \start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs actually does.

环境

我正在运行docker Docker版本18.06.1-ce,构建e68fc7a 在Windows 10上。

I am running docker Docker version 18.06.1-ce, build e68fc7a on Windows 10.

附加或脚本

我没有指定使用环境变量 attach_dbs 附加数据库,在许多示例中都可以看到。

I am not specifying attaching a database using the environment variable attach_dbs of which I see in many examples.

我是从端到端测试的角度试图找到一种管理sql容器的最佳实践,很多文章似乎都没有涵盖ta方面的部分-即

I am trying to find a best practice for managing a sql container from a point of view of end to end testing and a lot of articles seem to not cover the data aspect part - ie Development Workflow

我很想听听关于Docker世界中这两种方法的评论。

I would be interested to hear in comments thoughts on these two approaches in the Docker world.

推荐答案

使用以下命令可以解决您的问题

using following commands can solve your problem

docker-compose up --build -d

version: '3.4'
services:
  sql.data:
    image: ${DOCKER_REGISTRY}myfirst-mssql:latest
    container_name: myfirst-mssql_container
    environment:
      SA_PASSWORD: ab873jouehxaAGR
      ACCEPT_EULA: Y

之后:

docker exec myfirst-mssql_container sqlcmd
  -d master
  -S localhost
  -U "sa"
  -P "ab873jouehxaAGR"
  -Q 'select 1'

这篇关于通过Docker中的脚本创建SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:37