Project working with docker and docker-compose

This commit is contained in:
CodeLiturgy 2022-08-13 16:21:23 +01:00
parent 9cfbd700d4
commit a54ee3c909
9 changed files with 78 additions and 60 deletions

View File

@ -14,6 +14,7 @@
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.2.3" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

24
BlueWest.Api/Dockerfile Normal file
View File

@ -0,0 +1,24 @@
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
#EXPOSE 80
#EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["BlueWest.Api/BlueWest.Api.csproj", "BlueWest.Api/"]
COPY [".", "."]
RUN dotnet restore "BlueWest.Api/BlueWest.Api.csproj"
RUN dotnet build "BlueWest.Api/BlueWest.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "BlueWest.Api/BlueWest.Api.csproj" -c Release -o /app/publish --self-contained false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
#ENV ASPNETCORE_URLS http://0.0.0.0:80
WORKDIR /app
ENTRYPOINT ["dotnet", "BlueWest.Api.dll" ]

View File

@ -51,6 +51,7 @@ namespace BlueWest.WebApi
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();

View File

@ -79,7 +79,7 @@ namespace BlueWest.WebApi
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1")); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1"));
} }
app.UseHttpsRedirection(); //app.UseHttpsRedirection();
app.UseRouting(); app.UseRouting();
app.UseCors(MyAllowSpecificOrigins); app.UseCors(MyAllowSpecificOrigins);

View File

@ -8,6 +8,6 @@
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"ConnectionStrings": { "ConnectionStrings": {
"LocalMySQL": "server=127.0.0.1;user=blueuser;password=JwuWxhWxhh$X1;database=bluedb;" "LocalMySQL": "server=db;user=blueuser;password=dXjw127124dJ;database=bluedb;"
} }
} }

View File

@ -1,28 +0,0 @@
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
#COPY BlueWest/*.csproj ./BlueWest/
#COPY BlueWest.Api/*.csproj ./BlueWest.Api/
#COPY BlueWest.Data/*.csproj ./BlueWest.Data/
#
#COPY include/Math-Expression-Evaluator/SimpleExpressionEvaluator/*.csproj ./include/Math-Expression-Evaluator/SimpleExpressionEvaluator/
#COPY include/Math-Expression-Evaluator/SimpleExpressionEvaluator.Tests/*.csproj ./include/Math-Expression-Evaluator/SimpleExpressionEvaluator.Tests/
#COPY include/MapTo/src/BlueWest.MapTo/*.csproj ./include/MapTo/src/BlueWest.MapTo/
#COPY include/MapTo/src/BlueWest.MapTo/MapTo.props ./include/MapTo/src/BlueWest.MapTo/
COPY ["BlueWest.Api/BlueWest.Api.csproj", "BlueWest.Api/"]
RUN dotnet restore "BlueWest.Api/BlueWest.Api.csproj"
COPY . .
WORKDIR "/src/BlueWest.Api"
RUN dotnet build "BlueWest.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "BlueWest.Api.csproj" -c Release -o /app/publish --self-contained false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BlueWest.Api.dll"]

View File

@ -1,30 +0,0 @@
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source
# copy csproj and restore as distinct layers
COPY *.sln .
COPY BlueWest/*.csproj ./BlueWest/
COPY BlueWest.Api/*.csproj ./BlueWest.Api/
COPY BlueWest.Data/*.csproj ./BlueWest.Data/
COPY include/Math-Expression-Evaluator/SimpleExpressionEvaluator/*.csproj ./include/Math-Expression-Evaluator/SimpleExpressionEvaluator/
COPY include/Math-Expression-Evaluator/SimpleExpressionEvaluator.Tests/*.csproj ./include/Math-Expression-Evaluator/SimpleExpressionEvaluator.Tests/
COPY include/MapTo/src/BlueWest.MapTo/*.csproj ./include/MapTo/src/BlueWest.MapTo/
COPY include/MapTo/src/BlueWest.MapTo/MapTo.props ./include/MapTo/src/BlueWest.MapTo/
RUN dotnet restore
# copy everything else and build app
COPY BlueWest/. ./BlueWest/
WORKDIR /source/BlueWest.Api
#RUN dotnet publish -c release -o /app --no-restore
RUN mkdir -p xo
RUN dotnet publish "BlueWest.Api.csproj" -c Release -o xo --no-restore
workdir xo
# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
ENTRYPOINT ["dotnet", "BlueWest.Api.dll"]

View File

@ -0,0 +1,19 @@
# BlueWest Project
## Using docker
To build the BlueWest.Api image use:
`docker build -f BlueWest.Api/Dockerfile . -t myappdebug`
Copy the generated hash, and run:
` docker run -p 8080:80 image-hash`
## Using Docker Compose
Run the following command to instance a MySQL database and the API:
`docker-compose up --build --force-recreate`

31
docker-compose.yml Normal file
View File

@ -0,0 +1,31 @@
version: '3'
services:
bapi120:
build:
context: ./
dockerfile: ./BlueWest.Api/Dockerfile
ports:
- 8080:80
environment:
VIRTUAL_HOST: localhost
restart: always
links:
- db:db
container_name: BAPI281
db:
image: mysql/mysql-server:8.0
environment:
MYSQL_ROOT_HOST: db
MYSQL_USER_HOST: db
MYSQL_ROOT_PASSWORD: dXjw127124dJ
MYSQL_USER: blueuser
MYSQL_PASSWORD: dXjw127124dJ
MYSQL_DATABASE: bluedb
volumes:
- ./docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d/
# ports:
# - "3308:3306"