In Windows Server 2019 to deploy Asp.Net Core by Docker Compose

First, install Docker Enterprise

Installation documentation is:  https://docs.docker.com/install/windows/docker-ee/

After installation is complete, as shown below

 

 

 

Second, first of all, pull a mirror image of Windows

This will be Microsoft's Nano Server Docker download the image to your environment. The image is the smallest Windows server operating system, the package can be run as a Docker container. You can use it as a basis for your own applications, you can also run directly from the container.

docker image pull mcr.microsoft.com/windows/nanoserver:1809

 

Third, and then run a simple test container

docker container run mcr.microsoft.com/windows/nanoserver:1809 hostname

This will run a new container Windows Nano Server image and tell it to run the hostname command. Output of the machine name of the container, effectively random ID Docker provided. Repeat the command, you'll see each time with a different host name.

 

 

 

Fourth, the first image set pushed Docker Hub docker ID (account is docker)

Settings docker id

$dockerId = '<your-docker-id>'

 

 

 

Fifth, test generation DockerFile

Docker image is built using a simple script called Dockerfile by the docker image build command. Dockerfile describes the complete deployment of the application and all of its dependencies.

Use powershell production DockerFile

'FROM mcr.microsoft.com/windows/nanoserver:1809' | Set-Content Dockerfile
'CMD echo Hello World!' | Add-Content Dockerfile

六、Build Image

Now run docker image build, image a label that uses your Docker ID identified:

docker image build --tag $dockerId/hello-world . 

Note: "." Build back that is necessary. If there is permission to build a directory, it is best to create a new folder. Or even if production can also build DockerFile correct unsuccessful. I guess this should not have administrator privileges

 

 

 

Seven, run container

docker container run $dockerId/hello-world

 

 

Eight, push the mirror to Docker Hub

Now, with Docker image for a simple Hello World application. The image is a portable unit - You can set an image pushed to Docker Hub, anyone can pull it out and run your own applications.

First docker login with your credentials to run for authentication through the registry. Then push the image:

docker image push $dockerId/hello-world

 

 

No authority. . . First landing

docker login -u user name -p password

After pushing over on my local windows can be seen in Fig.

 

 

You can try the local pull:

docker pull 13797054045/hello-world

 

Nine, using Docker Compose deployment

我拿了一个测试项目来做测试。先把项目通过git拉到了服务器上。

下载Docker Compose

Invoke-WebRequest https://github.com/docker/compose/releases/download/1.23.2/docker-compose-Windows-x86_64.exe -UseBasicParsing -OutFile $env:ProgramFiles\docker\docker-compose.exe

写DockerFile。我偷了个懒,通过VS自动生成的,然后修改的

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 8018  --我这里只改了端口

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Gas.Monitoring.Web/Gas.Monitoring.Web.csproj", "Gas.Monitoring.Web/"]
COPY ["Gas.Monitoring.Business.Application/Gas.Monitoring.Business.Application.csproj", "Gas.Monitoring.Business.Application/"]
COPY ["Gas.Monitoring.Business.Repository/Gas.Monitoring.Business.Repository.csproj", "Gas.Monitoring.Business.Repository/"]
COPY ["ZB.Infrastructure.Base/ZB.Infrastructure.Base.csproj", "ZB.Infrastructure.Base/"]
COPY ["ZB.Infrastructure/ZB.Infrastructure.csproj", "ZB.Infrastructure/"]
COPY ["Gas.Monitoring.Business.Domain/Gas.Monitoring.Business.Domain.csproj", "Gas.Monitoring.Business.Domain/"]
COPY ["ZB.Infrastructure.FileSystem/ZB.Infrastructure.FileSystem.csproj", "ZB.Infrastructure.FileSystem/"]
COPY ["ProjectBuilder/ProjectBuilder.csproj", "ProjectBuilder/"]
COPY ["Gas.Monitoring.MP.Application/Gas.Monitoring.MP.Application.csproj", "Gas.Monitoring.MP.Application/"]
COPY ["Gas.Monitoring.MP.Repository/Gas.Monitoring.MP.Repository.csproj", "Gas.Monitoring.MP.Repository/"]
COPY ["Gas.Monitoring.MP.Domain/Gas.Monitoring.MP.Domain.csproj", "Gas.Monitoring.MP.Domain/"]
COPY ["ZB.UEditor.Core/ZB.UEditor.Core.csproj", "ZB.UEditor.Core/"]
RUN dotnet restore "Gas.Monitoring.Web/Gas.Monitoring.Web.csproj"
COPY . .
WORKDIR "/src/Gas.Monitoring.Web"
RUN dotnet build "Gas.Monitoring.Web.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Gas.Monitoring.Web.csproj" -c Release -o /app

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

然后修改后。这里我改的时候碰了不少壁啊。还好都解决了。

FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1809 AS base
WORKDIR /biogas
EXPOSE 8018

FROM microsoft/dotnet:2.2-sdk-nanoserver-1809 AS build
WORKDIR /src
COPY ["Gas.Monitoring.Web/Gas.Monitoring.Web.csproj", "Gas.Monitoring.Web/"]
COPY ["Gas.Monitoring.Business.Application/Gas.Monitoring.Business.Application.csproj", "Gas.Monitoring.Business.Application/"]
COPY ["Gas.Monitoring.Business.Repository/Gas.Monitoring.Business.Repository.csproj", "Gas.Monitoring.Business.Repository/"]
COPY ["ZB.Infrastructure.Base/ZB.Infrastructure.Base.csproj", "ZB.Infrastructure.Base/"]
COPY ["ZB.Infrastructure/ZB.Infrastructure.csproj", "ZB.Infrastructure/"]
COPY ["Gas.Monitoring.Business.Domain/Gas.Monitoring.Business.Domain.csproj", "Gas.Monitoring.Business.Domain/"]
COPY ["ZB.Infrastructure.FileSystem/ZB.Infrastructure.FileSystem.csproj", "ZB.Infrastructure.FileSystem/"]
COPY ["ProjectBuilder/ProjectBuilder.csproj", "ProjectBuilder/"]
COPY ["Gas.Monitoring.MP.Application/Gas.Monitoring.MP.Application.csproj", "Gas.Monitoring.MP.Application/"]
COPY ["Gas.Monitoring.MP.Repository/Gas.Monitoring.MP.Repository.csproj", "Gas.Monitoring.MP.Repository/"]
COPY ["Gas.Monitoring.MP.Domain/Gas.Monitoring.MP.Domain.csproj", "Gas.Monitoring.MP.Domain/"]
COPY ["ZB.UEditor.Core/ZB.UEditor.Core.csproj", "ZB.UEditor.Core/"]
RUN dotnet restore "Gas.Monitoring.Web/Gas.Monitoring.Web.csproj"
COPY . .
WORKDIR "/src/Gas.Monitoring.Web"
RUN dotnet build "Gas.Monitoring.Web.csproj" -c Release -o /biogas/build

FROM build AS publish
RUN dotnet publish "Gas.Monitoring.Web.csproj" -c Release -o /biogas/publish

FROM base AS final
WORKDIR /biogas
COPY --from=publish /biogas/publish .
ENTRYPOINT ["dotnet", "Gas.Monitoring.Web.dll"]

这样改的好处是我服务器上是不用下任何环境的,.Net Core SDK和Runtime都在容器里面了。

然后,编写docker-compose.yml。因为我这里只有一个项目,比较简单。唯一要说的是空格要注意!!!

version: '3.4'

services:
    biogas:
      image: biogas
      build:
        context: .
        dockerfile: Gas.Monitoring.Web/Dockerfile
      ports:
        - "8018:8018"

顺便贴一个多项目的

version: '3.4'

services:
    yopm:
      image: yopm
      build:
        context: .
        dockerfile: YO.PM.Web/Dockerfile
      ports:
        - "8081:8081"

    yopmclub:
      image: yopmclub
      build:
        context: .
        dockerfile: YO.Clubs.Web/Dockerfile
      ports:
        - "8082:8082"

CD into your project directory execute the command

docker-compose build

 

 

Then perform

docker-compose up -d

 

Check what is not the last to success

 

 emmmm ...... seems pretty simple Ha! I have two days to get their own way out, too dishes. . .

 

Guess you like

Origin www.cnblogs.com/zhaoshang/p/11951762.html