10 Şubat 2023 Cuma

.Net Core Application and SQL Server with Docker and Docker Compose

 Cmd açılıp aşağıdaki kod çalıştırılır. Böylece sql server  dockerda kurulmus olur.

docker run -it 
    -e "ACCEPT_EULA=Y" 
    -e "SA_PASSWORD=A&VeryComplex123Password" 
    -p 1433:1433 
    --name sql-server-2022 
    mcr.microsoft.com/mssql/server:2022-latest
  • -e "ACCEPT_EULA=Y", sets an environment variable read by SQL Server stating that you accept the end user license agreement
  • -e "SA_PASSWORD=A&VeryComplex123Password", sets an environment variable for the system administrator password
  • -p 1433:1433, forwards the SQL Server port from inside the container to outside the container, without this you could not access SQL Server from your host computer
  • --name sql-server-2022, give the container instance a name
  • mcr.microsoft.com/mssql/server:2022-latest, the image to download from Docker hub

Docker Compose 

Projenin ana dizininde Dockerfile ve docker-compose.yml isminde iki farklı dosya oluşturulur ve bunların içerisi aşağıdaki gibi configura edilir. Swagger .net core projesinde default olarak development ortamda çalışır. Onun için ENV olarak dockerfile içerisinde Development eklendi.

DockerFile
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
ENV ASPNETCORE_ENVIRONMENT=Development 
COPY ["WebApi/WebApi.csproj", "WebApi/"]
COPY ["Application/Application.csproj", "Application/"]
COPY ["Domain/Domain.csproj", "Domain/"]
COPY ["Infrastructure/Infrastructure.csproj", "Infrastructure/"]
RUN dotnet restore "WebApi/WebApi.csproj"
COPY . .
WORKDIR "/src/WebApi"
RUN dotnet build "WebApi.csproj" -c Release -o /app/build

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

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

docker-compose.yml

version: "3.9"  # optional since v1.27.0
services:
  web_api:
    build: . # build the Docker image 
    container_name: web_api_application
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "5000:80"
  sql:
    image: "mcr.microsoft.com/mssql/server:2022-latest"
    container_name: sql_server2022
    ports: # not actually needed, because the two services are on the same network.
      - "1433:1433" 
    environment:
      - ACCEPT_EULA=y
      - SA_PASSWORD=A&VeryComplex123Password


Appsetting.js
  "ConnectionStrings": {
    "Default": "Server=sql_server2022;Database=SalesDb;User Id=SA;Password=A&VeryComplex123Password;MultipleActiveResultSets=true;TrustServerCertificate=True;Encrypt=false"
  }


Uygulama ana dizini cmd ile açılır ve "docker-compose up" komutuyla ayağa kaldırılır.





PUSH IMAGES TO DOCKER HUB

Projede Dockerfile dizininr gelip aşağıdaki kodalrı çalıştıyoruz. İkinci satırda 8ad ile başlayan yeri kendi image Idmiz ile değiştiriyoruz.
docker build --rm -f "Dockerfile" -t efcleanarchitecturetemplate:latest .

docker tag 8ad130b70635 aytac3737/efcleanarchitecturetemplate:latest
docker push aytac3737/efcleanarchitecturetemplate


Gİthub Örnek Proje






















Hiç yorum yok:

Yorum Gönder