在容器构建时使用.NET Core 2中的Docker运行安装SQL脚本

问题描述 投票:0回答:1

我一直在看Entity Framework如何做到这一点,但我没有使用EF。我想运行我创建的SQL安装脚本,当我构建安装了SQL Server的docker容器时,该脚本会创建表并在表中插入数据。我正在使用Visual Studio 2017与.NET Core 2.0。

我的docker-compose.yml文件:

version: '3'

services:
  dockertest:
    image: dockertest
    build:
      context: .
      dockerfile: DockerTest/Dockerfile

  mssql:
    image: microsoft/mssql-server-linux
    volumes:
      - mssql:/var/opt/mssql/data/
    container_name: hscarddb
    environment:
      - MSSQL_SA_PASSWORD=Pass@word
      - ACCEPT_EULA=Y
      - MSSQL_PID=Developer
    ports:
      - "5434:1433"

volumes:
   mssql:

我的DockerFile:

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY DockerTest/DockerTest.csproj DockerTest/
RUN dotnet restore
COPY . .
WORKDIR /src/DockerTest
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DockerTest.dll"]

我的SQL安装脚本:

GO
USE master;
GO
CREATE TABLE Sets (Id int NOT NULL, SetName nvarchar(max), PRIMARY KEY (Id));
Go
CREATE TABLE Rarity (Id int NOT NULL, Name nvarchar(max), PRIMARY KEY(Id));
Go
CREATE TABLE Cards (Id int NOT NULL, Name nvarchar(max), ManaCost int, Attack int, Health int, RarityId int, SetsId int
PRIMARY KEY (Id),
FOREIGN KEY (RarityId) REFERENCES Rarity(Id),
FOREIGN KEY (SetsId) REFERENCES Sets(Id));
GO

INSERT INTO Rarity VALUES (1, 'Free');
INSERT INTO Rarity VALUES (2, 'Common');
INSERT INTO Rarity VALUES (3, 'Rare');
INSERT INTO Rarity VALUES (4, 'Epic');
INSERT INTO Rarity VALUES (5, 'Legendary');

INSERT INTO Sets VALUES (1, 'Basic');
INSERT INTO Sets VALUES (2, 'Classic');
INSERT INTO Sets VALUES (3, 'Hall of Fame');
INSERT INTO Sets VALUES (4, 'Curse of Naxxramas');
INSERT INTO Sets VALUES (5, 'Goblins vs Gnomes');
INSERT INTO Sets VALUES (6, 'Blackrock Mountain');
INSERT INTO Sets VALUES (7, 'The Grand Tournament');
INSERT INTO Sets VALUES (8, 'League of Explorers');
INSERT INTO Sets VALUES (9, 'Whispers of the Old Gods');
INSERT INTO Sets VALUES (10, 'One Night in Karazhan');
INSERT INTO Sets VALUES (11, 'Mean Streets of Gadgetzan');
INSERT INTO Sets VALUES (12, 'Journey to UnGoro');
INSERT INTO Sets VALUES (13, 'Knights of the Frozen Throne');
INSERT INTO Sets VALUES (14, 'Kobolds and Catacombs');

INSERT INTO Cards VALUES (1, 'Ragnaros the Firelord', 8, 8, 8, 5, 3);
INSERT INTO Cards VALUES (2, 'Call of the Wild', 9, null, null, 4, 11);
INSERT INTO Cards VALUES (3, 'Corridor Creeper', 7, 5, 5, 4, 14);

基本上,当我构建我的应用程序时,我希望它构建数据库容器,在其上运行SQL脚本,然后针对它运行我的应用程序(这只是一个简单的概念证明API)。

我想我需要在DockerFile中添加一些东西才能运行我的setup.sql,但对于我的生活,我无法弄清楚是什么。

sql-server docker docker-compose dockerfile asp.net-core-2.0
1个回答
0
投票

也许您可以创建自定义mssql映像并在构建映像时添加sql脚本?

我目前正在使用以下解决方案。你的'mssql'-dockerfile中的某个地方应该是:

RUN mkdir -p /opt/scripts
COPY database.sql /opt/scripts

ENV MSSQL_SA_PASSWORD=Passw@rd
ENV ACCEPT_EULA=Y

RUN /opt/mssql/bin/sqlservr --accept-eula & sleep 10 \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i /opt/scripts/database.sql \
&& pkill sqlservr 

不要忘记将dock.sql(带脚本)放在dockerfile旁边,因为它被复制到映像中。

© www.soinside.com 2019 - 2024. All rights reserved.