简体   繁体   English

带有 Docker 的 Angular 项目——客户端应用程序不复制

[英]Angular Project with Docker -- Client App not copying

So I'm admittedly new to Docker, and containerization in general.所以我承认我对 Docker 和一般的容器化很陌生。 But I am having a problem--my docker image is not saving my Client App file, my angular file, at all.但是我遇到了一个问题——我的 docker 映像根本没有保存我的客户端应用程序文件、我的 angular 文件。 I believe this because on my docker desktop, the file size is 208.59 mbs, which is only a mb (and some change) bigger than the base image I am using (mcr.micrsofot.com/dontet/aspnet:6.0).我相信这是因为在我的 docker 桌面上,文件大小为 208.59 mbs,仅比我使用的基本映像(mcr.micrsofot.com/dontet/aspnet:6.0)大 1 mb(和一些变化)。 I could be completely wrong here, but I cannot find my client app when I run the image in a container, and it only loads the default tutorial.我在这里可能完全错了,但是当我在容器中运行图像时找不到我的客户端应用程序,它只加载默认教程。

To Build...建立...

docker build -t containerName .

To Run...跑步...

docker run -tdp 443:3000 containerName

Then I would open localhost:3000 and it gives me an error saying it cannot load the page.然后我会打开 localhost:3000 它给我一个错误,说它无法加载页面。 There are no problems in the build process, and my Dockerfile is as follows...构建过程没有任何问题,我的Dockerfile如下...

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

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

ENV NODE_VERSION=16.13.0
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version

WORKDIR /src
COPY ["projectName.csproj", "."]
RUN dotnet restore "./projectName.csproj"
COPY . .
WORKDIR "/src/."

RUN dotnet build "projectName.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "projectName.csproj" -c Release -o /app/publish

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

I created the dockerfile using the add -> dockersupport function off of Visual Studio, and then added this section to install NodeJs, NVM and NPM.我使用 Visual Studio 的 add -> dockersupport 函数创建了 dockerfile,然后添加了这个部分来安装 NodeJs、NVM 和 NPM。 I thought maybe it was something in the csproj file, so I will include that as well.我想这可能是 csproj 文件中的内容,所以我也会将其包含在内。 I have tried uninstalling and reinstalling dockerDesktop, I have tried building with this Dockerfile more than once, and I have ran ng build --prod to create the dist file, although I am aware that the csproj file does that for me.我已经尝试卸载并重新安装 dockerDesktop,我曾多次尝试使用此 Dockerfile 进行构建,并且我已经运行 ng build --prod 来创建 dist 文件,尽管我知道 csproj 文件为我做了这些。 csproj file ... csproj 文件...

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <SpaProxyServerUrl>https://localhost:44401</SpaProxyServerUrl>
    <SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>alot of letters and numbers</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <DockerfileContext>.</DockerfileContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="6.0.4" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
  </ItemGroup>

  <ItemGroup>
    <!-- Don't publish the SPA source files, but do show them in the project files list -->
    <Content Remove="$(SpaRoot)**" />
    <None Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
  </ItemGroup>

    <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>
  
  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>wwwroot\%(RecursiveDir)%(FileName)%(Extension)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>
</Project>

I also thought maybe the fact that my client app is not in the wwwroot folder could be a problem, but I believe that things in the wwwroot are only for live versions of the site, and I am simply trying to create an image that has my site on it.我还认为我的客户端应用程序不在 wwwroot 文件夹中的事实可能是一个问题,但我相信 wwwroot 中的内容仅适用于网站的实时版本,我只是想创建一个包含我的图像网站就可以了。 Any help/resources is much appreciated.非常感谢任何帮助/资源。

Uninstalling and reinstalling docker did the trick.卸载并重新安装 docker 就可以了。 I realized after the first uninstall that localhost:80 was still loading the tutorial page, so I've come to the conclusion that it was just a weird bug--regardless, the issue was resolved after uninstalling and reinstalling docker, and rebuilding/rerunning the image.我在第一次卸载后意识到 localhost:80 仍在加载教程页面,所以我得出的结论是这只是一个奇怪的错误——无论如何,在卸载并重新安装 docker 并重建/重新运行后问题得到了解决图片。 I did not change the dockerfile commands, and I did not change the csproj file, simply uninstalled, reinstalled, and ran the following commands我没有改dockerfile命令,也没有改csproj文件,简单卸载,重新安装,运行如下命令

docker build -t myimageName .

and then for the run然后运行

docker run -dp 80:80/tcp myimageName:latest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM