Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerize Improvements #32

Open
f-nunez opened this issue Apr 18, 2023 · 8 comments
Open

Dockerize Improvements #32

f-nunez opened this issue Apr 18, 2023 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@f-nunez
Copy link
Owner

f-nunez commented Apr 18, 2023

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:

  • Apply docker best practices into existing files/configurations.
  • Apply standard conventions from offical docker guides.
  • Build smaller and secure images. (Improving performance minimizing images with trusted official sources like alpine/slim versions).

Plus (Extra):

  • Reduce image size copying only needed files or Ignoring not needed files to build images from Dockerfiles.
  • Implement free open sourced Scanners for vulnerabilities (if possible).
@f-nunez f-nunez converted this from a draft issue Apr 18, 2023
@f-nunez f-nunez added the enhancement New feature or request label Apr 18, 2023
@f-nunez f-nunez moved this from Done to Todo in VeterinaryClinic Apr 18, 2023
@f-nunez
Copy link
Owner Author

f-nunez commented Apr 18, 2023

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.

@f-nunez f-nunez assigned f-nunez and unassigned f-nunez Apr 18, 2023
@harshit82
Copy link

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

@harshit82
Copy link

Any file you want me to start from?

@f-nunez
Copy link
Owner Author

f-nunez commented Apr 19, 2023

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
Copy link
Owner Author

f-nunez commented Apr 19, 2023

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
Copy link
Owner Author

f-nunez commented Apr 19, 2023

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
Copy link
Owner Author

f-nunez commented Apr 19, 2023

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
Copy link
Owner Author

f-nunez commented Apr 19, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

2 participants