簡體   English   中英

在容器構建時使用.NET Core 2中的Docker運行Setup SQL腳本

[英]Running a Setup SQL script with Docker in .NET Core 2 upon container build

我一直在看如何使用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,但是對於我來說,我一直不知道該怎么辦。

也許可以在創建映像時創建自定義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 

不要忘了將dockerfile旁邊的database.sql(以及您的腳本)放置在dockerfile旁邊,因為它已復制到映像中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM