NetCore Docker 打包 推送 一些问题记录

一、docker 打包时有私有nuget库 与 官方nuget库 或者多个仓库时 在dockerfile中 ,要怎么保证restore正常

  nuget.config文件如下:

<?xml version="1.0" encoding="utf-8"?><configuration> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="mynuget" value="http://192.168.18.189:1086/nuget/" /> </packageSources></configuration>
 mynuget 为私有库地址

  1、单项目时

  项目结构

  /Test    /Dockerfile    /Tools/Nuget/nuget.config --创建一个nuget.config 文件

  Dockerfile 文件

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS baseWORKDIR /appEXPOSE 80EXPOSE 443FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS buildWORKDIR /srcCOPY . .WORKDIR "/src/."

#主要是 --configfile "./tools/nuget/nuget.config" 参数

 RUN dotnet build "Test.csproj" --configfile "./Tools/Nuget/nuget.config" -c Release -o /app/build

FROM build AS publishRUN dotnet publish "Test.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .ENTRYPOINT ["dotnet", "Test.dll"]

 

  2、如果是一个项目下有多个项目

  项目结构

  /app    /Test1      Dockerfile      Test1.sln    /Test2      Dockerfile      Test2.sln    /Tools/nuget/nuget.config --添加一个nuget.config 文件    /docker-compose.yml  --添加文件    /.env          --添加文件 说明:如果docker-compose.yml里面有使用到变量时,可在这个文件中设置变量的值;反之 不用加这个文件

 

  在根目录(app/下)添加 docker-compose.yml、nuget.config、.env 文件

docker-compose.yml文件内容如下

version: "3.7"services: Test1: image: "Test1:${TAG:-latest}" build: context: . dockerfile: Test1/Dockerfile Test2: image: "Test2:${TAG:-latest}" build: context: . dockerfile: Test2/Dockerfile

Dockerfile文件内容如下

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS baseWORKDIR /appEXPOSE 80EXPOSE 443FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS buildWORKDIR /srcCOPY . .WORKDIR "/src/."#主要是 --configfile "../tools/nuget/nuget.config" 参数,注意这里 变成了两个点,因为 nuget.config 在根目录下RUN dotnet build "Test1.csproj" --configfile "../Tools/Nuget/nuget.config" -c Release -o /app/build
FROM build AS publishRUN dotnet publish "Test1.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --from=publish /app/publish .ENTRYPOINT ["dotnet", "Test1.dll"]

.env 文件内容如下,主要是给上面的 docker-compose.yml中的TAG 设置版本号

TAG=1.0.4

 

最后 在app根目录 输入命令,就会生成镜像了

docker-compose up

 

二、docker for windows  docker push 私有仓库的时候 发生异常

 docker push 192.168.18.188:5000/test:1.0.1

server gave HTTP response to HTTPS client

设置本机 docker for windows 即可

相关文章