我创建了一个简单的NET CORE Web 应用程序,然后把自动生成的Dockerfile 复制到了.sln文件的同级目录,然后执行了docker build -t test001 . , 镜像可以生成成功。
然后我引用了Newtonsoft,再在项目里引用了它。
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
再执行了docker build -t test001,就报错了。
/src/WebApplication2/Controllers/HomeController.cs(3,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) [/src/WebApplication2/WebApplication2.csproj]
请问大佬们这个是什么原因
我的Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
RUN cp /etc/hosts /tmp/hosts
RUN mkdir -p -- /lib-override && cp /lib/x86_64-linux-gnu/libnss_files.so.2 /lib-override
RUN sed -i 's:/etc/hosts:/tmp/hosts:g' /lib-override/libnss_files.so.2
ENV LD_LIBRARY_PATH /lib-override
RUN echo "152.199.4.184 api.nuget.org" >> /tmp/hosts
COPY ["WebApplication2/WebApplication2.csproj", "WebApplication2/"]
RUN dotnet restore "WebApplication2/WebApplication2.csproj"
COPY . .
WORKDIR "/src/WebApplication2"
RUN dotnet build "WebApplication2.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication2.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication2.dll"]
nuget 引用么, 可以吧你测试的整个项目(或者简化后的)代码都放上来.
然后我引用了Newtonsoft,再在项目里引用了它。
是nuget引用,项目上传了,https://pan.baidu.com/s/1r2ZJdBbQ4e9PI9GByv7IoQ?pwd=74px
提取码:74px
@Qoal:
你上传的代码放我本地 build 成功了, 可以吧你失败的build log 也贴上来.
docker build -t mytag . --no-cache
[+] Building 57.7s (16/22)
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 38B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 35B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:5.0 0.3s
=> [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:5.0 0.3s
=> [build 1/12] FROM mcr.microsoft.com/dotnet/sdk:5.0@sha256:3ff465d940de3e2c727794d92fd7bb649c498d4abd91bc9213ea7831ebf01f1e 0.0s
=> [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:5.0@sha256:1a7d811242f001673d5d25283b3af03da526de1ee8d3bb5aa295f480b7844d44 0.0s
=> [internal] load build context 0.4s
=> => transferring context: 5.15kB [+] Building 65.3s (23/23) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 38B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 35B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:5.0 0.3s
=> [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:5.0 0.3s
=> [build 1/12] FROM mcr.microsoft.com/dotnet/sdk:5.0@sha256:3ff465d940de3e2c727794d92fd7bb649c498d4abd91bc9213 0.0s
=> [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:5.0@sha256:1a7d811242f001673d5d25283b3af03da526de1ee8d3bb5aa2 0.0s
=> [internal] load build context 0.4s
=> => transferring context: 5.15kB 0.3s
=> CACHED [build 2/12] WORKDIR /src 0.0s
=> [build 3/12] RUN cp /etc/hosts /tmp/hosts 0.3s
=> CACHED [base 2/2] WORKDIR /app 0.0s
=> CACHED [final 1/2] WORKDIR /app 0.0s
=> [build 4/12] RUN mkdir -p -- /lib-override && cp /lib/x86_64-linux-gnu/libnss_files.so.2 /lib-override 0.6s
=> [build 5/12] RUN sed -i 's:/etc/hosts:/tmp/hosts:g' /lib-override/libnss_files.so.2 0.5s
=> [build 6/12] RUN echo "152.199.4.184 api.nuget.org" >> /tmp/hosts 0.5s
=> [build 7/12] COPY [TestNuget/TestNuget.csproj, WebApplication2/] 0.1s
=> [build 8/12] COPY [WebApplication2/WebApplication2.csproj, WebApplication2/] 0.1s
=> [build 9/12] RUN dotnet restore "WebApplication2/WebApplication2.csproj" 56.5s
=> [build 10/12] COPY . . 0.1s
=> [build 11/12] WORKDIR /src/WebApplication2 0.1s
=> [build 12/12] RUN dotnet build "WebApplication2.csproj" -c Release -o /app/build 4.0s
=> [publish 1/1] RUN dotnet publish "WebApplication2.csproj" -c Release -o /app/publish 1.8s
=> [final 2/2] COPY --from=publish /app/publish . 0.1s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:c38c4536f296f9df0520cdf03253fdde0402f284085c5d3eb50e5c7998b5ab1e 0.0s
=> => naming to docker.io/library/mytag 0.0s
可以build的时候加配置 --progress=plain 在屏幕上保留 container 的log, 也许和你本地网络有关系. nuget 还原失败了.