Update docker compose for multiple projects

This commit is contained in:
Leendert de Borst
2024-06-08 01:03:08 +02:00
parent b7aa3e5782
commit 957ec87313
13 changed files with 206 additions and 25 deletions

25
.dockerignore Normal file
View File

@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View File

@@ -1,11 +1,33 @@
services:
blazorapp:
wasm:
image: aliasvault
build:
context: .
dockerfile: Dockerfile
dockerfile: src/AliasVault.WebApp/Dockerfile
ports:
- "80:8080"
restart: always
environment:
- API_URL=http://localhost:81
server:
image: aliasvault-server
build:
context: .
dockerfile: src/AliasVault/Dockerfile
ports:
- "82:8082"
volumes:
- ./database:/database
restart: always
api:
image: aliasvault-api
build:
context: .
dockerfile: src/AliasVault.Api/Dockerfile
ports:
- "81:8081"
volumes:
- ./database:/database
restart: always

View File

@@ -5,10 +5,15 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>AliasVault.Api</RootNamespace>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.6.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.6.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
@@ -20,4 +25,10 @@
<ProjectReference Include="..\AliasVault.Shared\AliasVault.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,41 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
# Copy the project files and restore dependencies
COPY ["src/AliasVault.Api/AliasVault.Api.csproj", "src/AliasVault.Api/"]
COPY ["src/AliasDb/AliasDb.csproj", "src/AliasDb/"]
COPY ["src/AliasVault.Shared/AliasVault.Shared.csproj", "src/AliasVault.Shared/"]
COPY ["src/AliasGenerators/AliasGenerators.csproj", "src/AliasGenerators/"]
RUN dotnet restore "src/AliasVault.Api/AliasVault.Api.csproj"
# Copy the rest of the application code
COPY . .
# Build the WebApi project
WORKDIR "/src/src/AliasVault.Api"
RUN dotnet build "AliasVault.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build
# Publish the application to the /app/publish directory in the container
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "AliasVault.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# Create the migration bundle
# Install the Entity Framework Core CLI tool and run migrations to create the database
RUN dotnet tool install --global dotnet-ef --version 8.0.5
RUN /root/.dotnet/tools/dotnet-ef migrations bundle -o /app/migrationbundle
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY /src/AliasVault.Api/entrypoint.sh /app
RUN chmod +x /app/entrypoint.sh
EXPOSE 8081
ENV ASPNETCORE_URLS=http://+:8081
ENTRYPOINT ["/app/entrypoint.sh"]

View File

@@ -2,8 +2,8 @@
# Apply database migrations using the bundle
echo "Running database migrations..."
/app/AliasVault/migrationbundle
/app/migrationbundle
# Start the application
echo "Starting application..."
dotnet /app/AliasVault/AliasVault.dll
dotnet /app/AliasVault.Api.dll

View File

@@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
@@ -19,6 +20,9 @@
<Content Update="wwwroot\appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
<ItemGroup>
@@ -27,4 +31,10 @@
<ProjectReference Include="..\AliasVault.Shared\AliasVault.Shared.csproj" />
<ProjectReference Include="..\Utilities\FaviconExtractor\FaviconExtractor.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="nginx.conf">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,39 @@
# Use the official ASP.NET Core runtime image as the base image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
# Use the official .NET SDK image to build the app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
# Copy the project files and restore dependencies
COPY ["src/AliasVault.WebApp/AliasVault.WebApp.csproj", "src/AliasVault.WebApp/"]
COPY ["src/AliasVault.Shared/AliasVault.Shared.csproj", "src/AliasVault.Shared/"]
COPY ["src/AliasDb/AliasDb.csproj", "src/AliasDb/"]
COPY ["src/AliasGenerators/AliasGenerators.csproj", "src/AliasGenerators/"]
COPY ["src/Utilities/FaviconExtractor/FaviconExtractor.csproj", "src/Utilities/FaviconExtractor/"]
RUN dotnet restore "src/AliasVault.WebApp/AliasVault.WebApp.csproj"
# Copy the rest of the application code
COPY . .
# Build the WebApp project
WORKDIR "/src/src/AliasVault.WebApp"
RUN dotnet build "AliasVault.WebApp.csproj" -c $BUILD_CONFIGURATION -o /app/build
# Publish the WebApp project
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "AliasVault.WebApp.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# Final stage: start nginx and serve static html files that were published in the previous stage
FROM nginx:alpine AS final
WORKDIR /usr/share/nginx/html
COPY --from=publish /app/publish/wwwroot .
COPY /src/AliasVault.WebApp/nginx.conf /etc/nginx/nginx.conf
COPY /src/AliasVault.WebApp/entrypoint.sh /app/
RUN chmod +x /app/entrypoint.sh
EXPOSE 8080
ENV ASPNETCORE_URLS=http://+:8080
ENTRYPOINT ["/app/entrypoint.sh"]

View File

@@ -7,6 +7,9 @@ using AliasVault.WebApp.Services;
using AliasVault.WebApp.Auth.Services;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
@@ -19,7 +22,7 @@ builder.Services.AddScoped(sp =>
{
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("AliasVault.Api");
httpClient.BaseAddress = new Uri("http://localhost:5092");
httpClient.BaseAddress = new Uri(builder.Configuration["ApiUrl"]);
return httpClient;
});

View File

@@ -0,0 +1,8 @@
#!/bin/sh
# Replace placeholder with the actual API URL
sed -i "s|http://localhost:5092|${API_URL}|g" /app/wwwroot/appsettings.json
# Start the application
nginx -g "daemon off;"

View File

@@ -0,0 +1,13 @@
events { }
http {
include mime.types;
server {
listen 8080;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html =404;
}
}
}

View File

@@ -1,12 +1,3 @@
{
"Local": {
"Authority": "http://localhost:5092",
"ClientId": "BlazorWasmApp",
"DefaultScopes": [
"openid",
"profile",
"email"
],
"PostLogoutRedirectUri": "http://localhost:5092/"
}
"ApiUrl": "http://localhost:5092"
}

View File

@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View File

@@ -18,11 +18,6 @@ COPY src/. ./
WORKDIR /src/AliasVault
RUN dotnet publish -c Release -o /app --verbosity detailed
# Create the migration bundle
# Install the Entity Framework Core CLI tool and run migrations to create the database
RUN dotnet tool install --global dotnet-ef --version 8.0.5
RUN /root/.dotnet/tools/dotnet-ef migrations bundle -o /app/migrationbundle
# Use the official ASP.NET Core runtime image to run the app
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app/AliasVault
@@ -31,10 +26,8 @@ WORKDIR /app/AliasVault
COPY --from=build /app ./
# Expose the port the app runs on
EXPOSE 8080
EXPOSE 8082
ENV ASPNETCORE_URLS=http://+:8082
# Copy entrypoint script
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["dotnet", "AliasVault.dll"]
ENTRYPOINT ["/entrypoint.sh"]