【问题标题】:Running a Setup SQL script with Docker in .NET Core 2 upon container build在构建容器时在 .NET Core 2 中使用 Docker 运行安装 SQL 脚本
【发布时间】:2018-01-23 05:56:35
【问题描述】:

我一直在了解如何使用 Entity Framework 执行此操作,但我没有使用 EF。我想运行我创建的 SQL 设置脚本,该脚本在我构建安装了 SQL Server 的 docker 容器时创建表并在表中插入数据。我正在使用带有 .NET Core 2.0 的 Visual Studio 2017。

我的 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:

我的 Docker 文件:

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】:

    也许您可以创建一个自定义的 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(带有您的脚本),因为它会复制到映像中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      相关资源
      最近更新 更多