f-nunez / VeterinaryClinic

Veterinary Clinic in .NET 7 with clean architecture for Blazor Web Assembly ASP.NET Core hosted model and built with Radzen Components
MIT License
6 stars 0 forks source link

Dockerize Improvements #32

Open f-nunez opened 1 year ago

f-nunez commented 1 year ago

As a product owner, I want improve the current Docker files and composes involved on each project. Should be applying best practices to care about security and performance. Inpsect and find standard conventions used on Docker Compose files that could had been missed into existing.

Criteria:

Plus (Extra):

f-nunez commented 1 year ago

Hello @harshit82, if you are still interested you can take this task related to Docker. Feel free to contribute beyond what is mentioned in this. And no matter the time it takes, just enjoy.

harshit82 commented 1 year ago

Thanks a lot for this, I will need some guidance.

harshit82 commented 1 year ago

Any file you want me to start from?

f-nunez commented 1 year ago

My bad I didn't tell you the context about the solution.

Well in short there are APIs and Web Apps to manage a Vet Clinic. An Identity Api (IdentityServer) caring about authentication/authorization. A Clinic Management API (clinicmanagement-api) and Blazor App (clinicmanagement-blazor) performing CRUD operations about Patients, Clients (pet owners) and Doctors. A Scheduling API and Blazor App perfoming CRUDs Patient's appointments. A Public Web App (public-web) handling appointment confirmations requested from Client's emails. And more APIs behind.

You may find it useful to see the README file about how deploy and test the entire solution.

f-nunez commented 1 year ago

For this Feature/Task. We can get focus in docker-compose.DockerDevelopment.yml and docker-compose.DockerNginx.yml at repository folder. Any of those files will show us how the containers (Services) are configured and where their Dockerfiles are located to build their images.

f-nunez commented 1 year ago

To achive the third Criteria we can explore, compare and select related official images based on different Linux OS like debian, alpine, ubuntu, etc.

As example, if we look into a docker compose file at the entry labeled service called public-web, we can see where its Dockerfile is (.../Public/src/Fnunez.VeterinaryClinic.Public.Web/Dockerfile).

Working the third Criteria (Build smaller and secure images): We can look into Dockerfile at the first line (FROM mcr.microsoft.com/dotnet/aspnet:7.0) and see a keyword "FROM" that specifies from which parent image we are building.

Then we could do some search at Docker's hub site for smallest official lighter images related to our current parent images.

Surfing on https://hub.docker.com/_/microsoft-dotnet we can see that there are a block called "Featured Repos" where we could get the ASP NET Core Runtime repo image (https://hub.docker.com/_/microsoft-dotnet-aspnet) and explore for a Tag Listing based on Linux which alpine is used to be the smallest and optimized image.

So taking the latest alpine version (7.0.203-alpine3.17-amd64) we just need to specify as the following line: FROM mcr.microsoft.com/dotnet/aspnet:7.0.5-alpine3.17-amd64 AS base

After that, we will do the same for NET SDK (https://hub.docker.com/_/microsoft-dotnet-sdk) which currently and latest version is 7.0.203-alpine3.17-amd64.

And would looks like: FROM mcr.microsoft.com/dotnet/sdk:7.0.203-alpine3.17-amd64 AS build

f-nunez commented 1 year ago

Now the local Docker image for public-web would be smaller than before. The final result on our Dockerfile would be like:

# Setup for docker-compose
FROM mcr.microsoft.com/dotnet/aspnet:7.0.5-alpine3.17-amd64 AS base # <--- changed
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:7.0.203-alpine3.17-amd64 AS build # <--- changed
WORKDIR /app
# run this from repository root
COPY ./ ./ 
# RUN ls -lha .

RUN echo 'Building Public Web container'

WORKDIR /app/Public/src/Fnunez.VeterinaryClinic.Public.Web
# RUN ls -lha .
RUN dotnet restore

RUN dotnet build "Fnunez.VeterinaryClinic.Public.Web.csproj" -c Development -o /app/build

FROM build AS publish
RUN dotnet publish "Fnunez.VeterinaryClinic.Public.Web.csproj" -c Development -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "Fnunez.VeterinaryClinic.Public.Web.dll"]
f-nunez commented 1 year ago

Just comparing and playing a bit with old and new public-web image

Building and deploying the current/previous image through terminal:

docker-compose -f docker-compose.DockerNginx.yml build public-web --no-cache
docker-compose -f docker-compose.DockerNginx.yml up public-web -d
docker image list

We will see the current public-web image size is around 223 mb.

After edit the Dockerfile just re-build and re-deploy it:

docker-compose -f docker-compose.DockerNginx.yml down
docker-compose -f docker-compose.DockerNginx.yml build public-web --no-cache
docker-compose -f docker-compose.DockerNginx.yml up public-web -d
docker image list

We will see again the new public-web image size is around 119 mb.

In fact we reduced the image sizing from 223 mb to 119 mb.