Я создал основной веб-API asp.net с Sql-сервером. Запустив docker compose file, мне нужно создать базу данных и таблицы. Мои решения следующие:
Dockerfile
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY StarterPackMSSQL.StarterPackMSSQL.csproj StarterPackMSSQL/
RUN dotnet restore StarterPackMSSQL/StarterPackMSSQL.csproj
COPY . .
WORKDIR /src/StarterPackMSSQL
RUN dotnet build StarterPackMSSQL.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish StarterPackMSSQL.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "StarterPackMSSQL.dll"]
docker-compose.yml
version: '3.5'
networks :
localdev:
name: localdev
services:
starterpackmssql:
image: ${DOCKER_REGISTRY-}mssql
restart: always
build:
context: .
dockerfile:StarterPackMSSQL/Dockerfile
ports:
- "5001:80"
depends_on:
- db-server
networks:
- localdev
db-server:
image: microsoft/mssql-server-linux
container_name: db-server
environment:
- ACCEPT_EULA=Y
- MSSQL_SA_PASSWORD=password123
- MSSQL_TCP_PORT=1433
ports:
- "1400:1433"
networks:
- localdev
SQL Script
CREATE DATABASE [StarterPacksDB]
GO
USE [StarterPacksDB]
GO
/****** Object: Table [dbo].[Users] Script Date: 10/18/2019 10:42:39 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[id] [int] IDENTITY(1,1) NOT NULL,
[handle] [varchar](50) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Мне просто нужно автоматически создать базу данных и таблицы при запуске docker-compose up
И он должен работать как на Mac, так и на Windows.