我如何创建一个docker-compose.yml文件,该文件将在1个容器中运行Nuxt.js SPA和.NET Core Web API?到目前为止,我已经在docker中使用了Web Api,并且可以向其发送命令。但是每次我尝试将Nuxt SPA添加到docker-compose.yml中时,Visual Studio都会给我一个奇怪的错误(请参阅文章结尾)。
这是我到目前为止所拥有的:
docker-compose.yml
version: '3.4'
services:
webapi:
container_name: webapi
image: projectname/projectname_webapi
build:
context: .
dockerfile: src/WebAPI/Dockerfile
restart: always
ports:
- "5500:80"
environment:
ASPNETCORE_ENVIRONMENT: Development
。Net Core Web API Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
COPY . .
RUN dotnet test
FROM build AS publish
WORKDIR /src/WebAPI
RUN dotnet publish "WebAPI.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebAPI.dll"]
Nuxt.js SPA Dockerfile
# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 7000
CMD ["nginx", "-g", "daemon off;"]
[如果在docker-compose.yml中添加以下内容,则会收到错误:
webpui:
container_name: webpui
image: projectname/projectname_webui
build:
context: .
dockerfile: src/WebUI/Dockerfile
restart: always
ports:
- '7000:7000'
command: ["npm", "run", "serve"]
错误:
Error MSB4018 The "GetServiceReferences" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
File name: 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
at Microsoft.VisualStudio.Containers.Tools.Common.Prerequisites.DockerForWindowsDriveSharingPrerequisite.<EvaluateAsync>d__0.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.VisualStudio.Containers.Tools.Common.Prerequisites.DockerForWindowsDriveSharingPrerequisite.EvaluateAsync(CancellationToken cancellationToken)
at Microsoft.Docker.Prerequisites.DockerForWindowsDriveSharingPrerequisite.<EvaluateAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Docker.Prerequisites.DockerCompositePrerequisite.<EvaluateAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Docker.BuildTasks.DockerBuildTask.<EvaluateBuildPrerequisitesAsync>d__32.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Docker.BuildTasks.DockerBuildTask.<ExecuteAsync>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Docker.BuildTasks.DockerBuildTask.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
docker-compose C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets 202
任何建议都将不胜感激!
上面显示的特定问题是缺少Newtonsoft.Json DLL。搜索以找到解决方案中引用该软件包的项目,然后使用NuGet将“ Newtonsoft.Json”软件包安装到该项目。如果在安装软件包后仍然无法克服该错误,请尝试将该软件包降级到以前的版本,然后再将其升级回当前的稳定版本。 (这似乎使参考文献变得更直了。)有关更多信息,请参见this question的答案。
一旦您超过了这一点,您将能够找出在回答原始问题的过程中是否还有其他问题。祝好运!