From f1dbe553a2b2ce2a04a85c13e418782716670b0b Mon Sep 17 00:00:00 2001 From: Philipp Meier Date: Fri, 15 Sep 2023 19:28:08 +0200 Subject: [PATCH] feat: Added docker support. --- .gitignore | 1 + Chrono.sln | 11 +++++++++-- Dockerfile | 25 +++++++++++++++++++++++++ README.md | 24 +++++++++++++++--------- docker-compose.yml | 23 +++++++++++++++++++++++ setup.sh | 16 ++++++++++++++++ src/WebUI/Program.cs | 1 + 7 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100755 setup.sh diff --git a/.gitignore b/.gitignore index 25eb668..7f21c16 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.user *.userosscache *.sln.docstates +https/* .idea data/* .vscode diff --git a/Chrono.sln b/Chrono.sln index 4f6768a..93b97d3 100644 --- a/Chrono.sln +++ b/Chrono.sln @@ -13,7 +13,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "src\Applicat EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebUI", "src\WebUI\WebUI.csproj", "{9BA631F2-D082-437D-8100-99F826858941}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_docs", "_docs", "{79449CD2-6C8D-4E22-BD09-1AB18846078C}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{79449CD2-6C8D-4E22-BD09-1AB18846078C}" ProjectSection(SolutionItems) = preProject README.md = README.md LICENSE = LICENSE @@ -28,11 +28,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "static", "static", "{11D354 static\Start.png = static\Start.png EndProjectSection EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_config", "_config", "{C6C4884B-F513-4419-8326-6DA9F3CE3189}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{C6C4884B-F513-4419-8326-6DA9F3CE3189}" ProjectSection(SolutionItems) = preProject .editorconfig = .editorconfig .gitignore = .gitignore .gitkeep = .gitkeep + setup.sh = setup.sh EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{56763119-C6D6-40E9-9D27-A16C2A2C132E}" @@ -45,6 +46,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{ .github\workflows\dotnet.yml = .github\workflows\dotnet.yml EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docker", "docker", "{C21C3EBB-6435-432F-9291-F3165DD416AA}" + ProjectSection(SolutionItems) = preProject + Dockerfile = Dockerfile + docker-compose.yml = docker-compose.yml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6e8b7c1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS server-base +WORKDIR /app +EXPOSE 80 +EXPOSE 443 +RUN apt-get update +RUN apt-get install -y curl +RUN apt-get install -y libpng-dev libjpeg-dev curl libxi6 build-essential libgl1-mesa-glx +RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash - +RUN apt-get install -y nodejs + +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build +RUN apt-get update +RUN apt-get install -y curl +RUN apt-get install -y libpng-dev libjpeg-dev curl libxi6 build-essential libgl1-mesa-glx +RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash - +RUN apt-get install -y nodejs +WORKDIR /source +COPY Chrono.sln . +COPY src/. ./src +COPY tests/. ./tests +RUN dotnet publish -c release -o /app/publish + +FROM server-base AS server +COPY --from=build /app/publish . +ENTRYPOINT ["dotnet", "Chrono.WebUI.dll"] \ No newline at end of file diff --git a/README.md b/README.md index 9918264..3e73e2d 100644 --- a/README.md +++ b/README.md @@ -51,19 +51,25 @@ simple tool to keep track of useful ideas that positively impact your project(s) ## Getting started -You will need an empty `sqlite3`-database in the `data`-folder of the project. For this you can execute the following -command: +Run `setup.sh` to automatically create all required files (e.g. the SQLite3 database). +You also need an OAuth provider like [auth0](https://auth0.com) and configure the `Authority`, `ClientId` +and `ClientSecret` in the `appsettings.json` file of the `WebUI` project. -```sh -sqlite3 chrono.db "VACUUM;" -``` +Once this is done, you can run the application with (for example) `dotnet watch` in the `WebUI` folder. -Besides the database, you also need an OAuth provider like [auth0](https://auth0.com) and configure -the `Authority`, `ClientId` and `ClientSecret` in the `appsettings.json` file of the `WebUI` project. +**Swagger UI**: https://localhost:7151/swagger/index.html -Once that is done, you can run the application with (for example) `dotnet watch` in the `WebUI` folder. +### Docker -**Swagger UI**: https://localhost:7151/swagger/index.html +Chrono can also be hosted with Docker. You can configure the OAuth credentials in the `docker-compose.yml` or in +the `appsettings.json`-file. +You also have to provide a https certificate, if you want to serve Chrono directly via https using the Kestrel +web-server. + +A dev-certificate can be created using the following command or running `setup.sh`: +`dotnet dev-certs https -ep ./https/aspnetapp.pfx -p `. + +To start the application, you can simply use the `docker compose up` command. ## Useful scripts diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9474b0d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: "3.4" + +services: + app: + build: + context: . + dockerfile: Dockerfile + ports: + - "127.0.0.1:443:443" + #- "127.0.0.1:80:80" + environment: + - ASPNETCORE_ENVIRONMENT=Production + - ASPNETCORE_URLS=https://+:443 + #- ASPNETCORE_URLS=https://+:443;http://+:80 + - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx + - ASPNETCORE_Kestrel__Certificates__Default__Password=defaultPassword + - ConnectionStrings__DefaultConnection=Data Source=../data/chrono.db + - IdentityProvider__Authority=https://.auth0.com/ + - IdentityProvider__ClientId= + - IdentityProvider__ClientSecret= + volumes: + - ./data:/data + - ./https:/https diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..ae33726 --- /dev/null +++ b/setup.sh @@ -0,0 +1,16 @@ +# Ensures all required files are created. +mkdir -p ./data +mkdir -p ./https + +if [ ! -f ./data/chrono.db ]; then + sqlite3 data/chrono.db "VACUUM;" + echo "Created chrono.db." +else + echo "chrono.db already exists." +fi + +if [ ! -f ./https/aspnetapp.pfx ]; then + dotnet dev-certs https -ep ./https/aspnetapp.pfx -p defaultPassword +else + echo "Valid https certificate already exists." +fi \ No newline at end of file diff --git a/src/WebUI/Program.cs b/src/WebUI/Program.cs index fa24de6..d6be36f 100644 --- a/src/WebUI/Program.cs +++ b/src/WebUI/Program.cs @@ -1,6 +1,7 @@ using Chrono.Application.Infrastructure.Persistence; var builder = WebApplication.CreateBuilder(args); +builder.Configuration.AddEnvironmentVariables(); builder.WebHost.UseKestrel(option => option.AddServerHeader = false); // Add services to the container.