Ushbu maqola ASP.NET docker tasvirini yaratish va uni Docker Hub-ga joylashtirish haqida. Docker Hub docker tasvirlarni ro‘yxatga olish xizmati bo‘lib, u sizga kod omborlariga ulanish, tasvirlarni yaratish, saqlash, sinab ko‘rish va tasvirlarni serverga joylashtirishingiz uchun Docker Cloud-ga ulanish imkonini beradi.

 

ASP.NET ilovasini yaratish

dotnet CLI buyrug’u yordamida Web dasturimizni yaratamiz.

Diqqat! Buning uchun sizda .NET SDK o’rnatilgan bo’lishi kerak.

dotnet new web -n SimpleWebApp

 

Dasturimizdagi Program.cs fayl quyidagi ko’rinishga ega:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

Dockerfile yaratish

ASP.NET Core ilovasini yaratgandan so'ng, tasvirni yaratish uchun docker faylini qo'shishingiz mumkin. Ushbu maqola uchun men quyidagi docker faylini qo'shdim.

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["SimpleWebApp.csproj", "."]
RUN dotnet restore "./SimpleWebApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "SimpleWebApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "SimpleWebApp.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SimpleWebApp.dll"]

 

Docker tasvirini yaratish

Docker tasvirni docker build buyrug'i yordamida yaratishingiz mumkin: 

docker build --rm -t simplewebapp:latest .

Yaratilgan tasvirni `docker images` buyrug’i yordamida tekshiring:

docker images
REPOSITORY      TAG        IMAGE ID        CREATED           SIZE

simplewebapp    latest     9b7672646417    11 minutes ago    212MB

 

Docker tasvirini Docker Hub-ga joylashtirish

Docker tasvirini Docker Hub-da joylashtirish uchun siz o’zingizni Docker Hub foydalanuvchi nomi bilan belgilashingiz kerak. Mening Docker Hub foydalanuvchi nomim idilshod. Docker tasvirni foydalanuvchi nomi bilan belgilash buyrug’i quyidagicha:

docker tag simplewebapp:latest idilshod/simplewebapp:latest

Bu yerda teg buyrug'i argumentidan keyin docker image nomi yoki identifikatoridan foydalanish kerak, keyin idilshod o'rniga o'z docker hub foydalanuvchi nomingiz bila almashtirng. Endi siz docker images buyrug'ini bajarsangiz, yangi tasvirni ko'rishingiz mumkin.

REPOSITORY      TAG IMAGE ID        CREATED           SIZE

idilshod/simplewebapp       latest 9b7672646417    11 minutes ago    212MB

 

Endi siz tasvirni Docker Hub-ga joylashtirishingiz mumkin. Buning uchun docker login buyrug'i yordamida Docker Hub tizimiga kirishingiz kerak. 

Buyruqni bajarganingizda sizdan Docker Hub foydalanuvchi ma’lumotlarini kiritishingizni so'raydi. Foydalanuvchi ma'lumotlari docker logout buyrug'i yordamida tizimdan chiqmaguningizcha saqlanadi.

Tizimga muvaffaqiyatli kirganingizdan so'ng, tasvirni Docker Hub-ga joylashtirish uchun docker push buyrug'idan foydalanishingiz mumkin:

docker push idilshod/simplewebapp:latest

 

Docker tasvirini yuklash va ishga tushirish

Docker Hub-dan docker pull buyrug’i yordamida yuqorida joylashtirgan tasvirni yuklab oling:

docker pull idilshod/simplewebapp

 

Tasvirni ishga tushiring:

docker run --rm -p 5000:80 idilshod/simplewebapp:latest

 

Natijani tekshirish uchun brauzerda http://localhost:5000 ni tering.

 

Tasvirni o’chirish uchun docker image rm buyrug’idan foydalanish mumkin:

docker image rm idilshod/simplewebapp

 

Salomat bo'ling!