From a54ee3c909a8602d9b43f812908cc47fe0ecdfb5 Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Sat, 13 Aug 2022 16:21:23 +0100 Subject: [PATCH] Project working with docker and docker-compose --- BlueWest.Api/BlueWest.Api.csproj | 1 + BlueWest.Api/Dockerfile | 24 ++++++++++++++++++++++++ BlueWest.Api/Program.cs | 1 + BlueWest.Api/Startup.cs | 2 +- BlueWest.Api/appsettings.json | 2 +- Dockerfile | 28 ---------------------------- Dockerfile_old | 30 ------------------------------ README.md | 19 +++++++++++++++++++ docker-compose.yml | 31 +++++++++++++++++++++++++++++++ 9 files changed, 78 insertions(+), 60 deletions(-) create mode 100644 BlueWest.Api/Dockerfile delete mode 100644 Dockerfile delete mode 100644 Dockerfile_old create mode 100644 docker-compose.yml diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index 971d059..857f5ac 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -14,6 +14,7 @@ + diff --git a/BlueWest.Api/Dockerfile b/BlueWest.Api/Dockerfile new file mode 100644 index 0000000..a965d8e --- /dev/null +++ b/BlueWest.Api/Dockerfile @@ -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" ] diff --git a/BlueWest.Api/Program.cs b/BlueWest.Api/Program.cs index b7c4288..ad93092 100644 --- a/BlueWest.Api/Program.cs +++ b/BlueWest.Api/Program.cs @@ -51,6 +51,7 @@ namespace BlueWest.WebApi } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); diff --git a/BlueWest.Api/Startup.cs b/BlueWest.Api/Startup.cs index 3eb97c4..0d1769b 100644 --- a/BlueWest.Api/Startup.cs +++ b/BlueWest.Api/Startup.cs @@ -79,7 +79,7 @@ namespace BlueWest.WebApi app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueWest.Api v1")); } - app.UseHttpsRedirection(); + //app.UseHttpsRedirection(); app.UseRouting(); app.UseCors(MyAllowSpecificOrigins); diff --git a/BlueWest.Api/appsettings.json b/BlueWest.Api/appsettings.json index cadbc89..053cd92 100644 --- a/BlueWest.Api/appsettings.json +++ b/BlueWest.Api/appsettings.json @@ -8,6 +8,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "LocalMySQL": "server=127.0.0.1;user=blueuser;password=JwuWxhWxhh$X1;database=bluedb;" + "LocalMySQL": "server=db;user=blueuser;password=dXjw127124dJ;database=bluedb;" } } diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 5cc3279..0000000 --- a/Dockerfile +++ /dev/null @@ -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"] \ No newline at end of file diff --git a/Dockerfile_old b/Dockerfile_old deleted file mode 100644 index a1c21b2..0000000 --- a/Dockerfile_old +++ /dev/null @@ -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"] diff --git a/README.md b/README.md index e69de29..7e0b3f0 100644 --- a/README.md +++ b/README.md @@ -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` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5971941 --- /dev/null +++ b/docker-compose.yml @@ -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" + + + + \ No newline at end of file