Unable to get a fresh aspnetcore .net5 react project to deploy using docker

467 Views Asked by At

I'm quite new to docker, and have spent many hours trying to get this problem fixed. I am unable to deploy my aspnetcore react docker image to docker. I have successfully deployed many aspnetcore images, but when it comes to the SPA I cannot seem to figure it out. I started a fresh .net5 react project to test the docker file. I created a docker-compose file for the build. Each time i get the following error. Any help would be greatly appreciated.

The project is called dockerSPA - I created this specifically for testing purposes. Please let me know if i can supply additional information to solve this issue

Dockerfile

ARG NODE_IMAGE=node=16:0
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

ENV NODE_ENV=production

FROM ${NODE_IMAGE} as node-build
WORKDIR /web
COPY DockerSPA/ClientApp/package.json .
COPY DockerSPA/ClientApp/package-lock.json .
COPY DockerSPA .

RUN npm i [email protected] -g && npm update && npm install && npm run build:prod
# RUN npm install -production

FROM mcr.microsoft.com/dotnet/sdk:5.0.102-ca-patch-buster-slim AS build
WORKDIR /src

COPY ["DockerSPA/DockerSPA.csproj", "DockerSPA/"]
RUN dotnet restore "DockerSPA/DockerSPA.csproj"
COPY . .
WORKDIR "/src/DockerSPA"
RUN dotnet build "DockerSPA.csproj" -c Release -o /app/build

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

COPY . .
COPY --from=node-build /web .

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

package.json scrips

 "scripts": {
    "start": "rimraf ./build && react-scripts start",
    "build": "react-scripts build",
    "build:prod": "npm start build",
    "test": "cross-env CI=true react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "lint": "eslint ./src/"
  },

docker-compose.yml

version: "3.9"  # optional since v1.27.0

services:

  nodejs:
    image: node:16

  webspa:
    container_name: dockerSPA
    build:
      context: .
      dockerfile: DockerSPA/Dockerfile
      args:
        NODE_IMAGE: ${NODE_IMAGE:-node:16.0}
    ports:
      -  "5200:80"
    depends_on:
      - nodejs

full docker error after using 'docker-compose up'

Building webspa
[+] Building 9.4s (23/26)
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                            0.0s 
 => => transferring dockerfile: 1.15kB                                                                                                                                                                                                                          0.0s 
 => [internal] load .dockerignore                                                                                                                                                                                                                               0.0s 
 => => transferring context: 35B                                                                                                                                                                                                                                0.0s 
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:5.0.102-ca-patch-buster-slim                                                                                                                                                                      0.4s 
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:5.0                                                                                                                                                                                            0.0s 
 => [internal] load metadata for docker.io/library/node:16.0                                                                                                                                                                                                    0.5s 
 => [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:5.0.102-ca-patch-buster-slim@sha256:1b832feb890536fdd31e0553587c40e33f9f0c7ce62bec3be6f1d2b4307c14f7                                                                                                          0.0s 
 => [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:5.0                                                                                                                                                                                                         0.0s 
 => [internal] load build context                                                                                                                                                                                                                               0.0s 
 => => transferring context: 3.42kB                                                                                                                                                                                                                             0.0s 
 => [node-build 1/6] FROM docker.io/library/node:16.0@sha256:25516f3de85ebf588e29d81052495d2e1177b55cddbd7ddab2f5ff2c4496dd5e                                                                                                                                   0.0s 
 => CACHED [build 2/7] WORKDIR /src                                                                                                                                                                                                                             0.0s 
 => CACHED [base 2/2] WORKDIR /app                                                                                                                                                                                                                              0.0s 
 => CACHED [final 1/2] WORKDIR /app                                                                                                                                                                                                                             0.0s 
 => [build 3/7] COPY [DockerSPA/DockerSPA.csproj, DockerSPA/]                                                                                                                                                                                                   0.0s 
 => CACHED [node-build 2/6] WORKDIR /web                                                                                                                                                                                                                        0.0s 
 => CACHED [node-build 3/6] COPY DockerSPA/ClientApp/package.json .                                                                                                                                                                                             0.0s 
 => CACHED [node-build 4/6] COPY DockerSPA/ClientApp/package-lock.json .                                                                                                                                                                                        0.0s 
 => CACHED [node-build 5/6] COPY DockerSPA .                                                                                                                                                                                                                    0.0s 
 => CANCELED [node-build 6/6] RUN npm i [email protected] -g && npm update && npm install && npm run build:prod                                                                                                                                                        8.7s 
 => [build 4/7] RUN dotnet restore "DockerSPA/DockerSPA.csproj"                                                                                                                                                                                                 3.0s 
 => [build 5/7] COPY . .                                                                                                                                                                                                                                        0.1s 
 => [build 6/7] WORKDIR /src/DockerSPA                                                                                                                                                                                                                          0.1s 
 => [build 7/7] RUN dotnet build "DockerSPA.csproj" -c Release -o /app/build                                                                                                                                                                                    3.6s 
 => ERROR [publish 1/3] RUN dotnet publish "DockerSPA.csproj" -c Release -o /app/publish                                                                                                                                                                        1.8s 
------
 > [publish 1/3] RUN dotnet publish "DockerSPA.csproj" -c Release -o /app/publish:
#17 0.632 Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
#17 0.632 Copyright (C) Microsoft Corporation. All rights reserved.
#17 0.632
#17 1.014   Determining projects to restore...
#17 1.200   All projects are up-to-date for restore.
#17 1.674   DockerSPA -> /src/DockerSPA/bin/Release/net5.0/DockerSPA.dll
#17 1.674   DockerSPA -> /src/DockerSPA/bin/Release/net5.0/DockerSPA.Views.dll
#17 1.719   /bin/sh: 2: /tmp/tmpbc0abd804da1470aa4bbde26647a85f4.exec.cmd: npm: not found
#17 1.721 /src/DockerSPA/DockerSPA.csproj(38,5): error MSB3073: The command "npm install" exited with code 127.
------
executor failed running [/bin/sh -c dotnet publish "DockerSPA.csproj" -c Release -o /app/publish]: exit code: 1
ERROR: Service 'webspa' failed to build : Build failed

EDIT

I have managed to finally get the react spa client to deploy by instead installing nodejs by following the old .net3 recommendations from microsoft with some small updates. Now My nodejs service exits right away with code 0;

DOCKER FILE

#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/sdk:5.0.102-ca-patch-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN curl -sL https://deb.nodesource.com/setup_16.x |  bash -
RUN apt-get install -y nodejs


FROM mcr.microsoft.com/dotnet/sdk:5.0.102-ca-patch-buster-slim AS build
RUN curl -sL https://deb.nodesource.com/setup_16.x |  bash -
RUN apt-get install -y nodejs

WORKDIR /src
COPY ["DockerSPA/DockerSPA.csproj", "DockerSPA/"]
RUN dotnet restore "DockerSPA/DockerSPA.csproj"
COPY . .
WORKDIR "/src/DockerSPA"
RUN dotnet build "DockerSPA.csproj" -c Release -o /app/build

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

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

errors:

dockerSPA | warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
dockerSPA |       Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
dockerSPA | warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
dockerSPA |       No XML encryptor configured. Key {494d834e-82ff-4408-beab-6c6ad51cb722} may be persisted to storage in unencrypted form.
dockerSPA | warn: Microsoft.AspNetCore.Server.Kestrel[0]
dockerSPA |       Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
dockerSPA | warn: Microsoft.AspNetCore.Server.Kestrel[0]
dockerSPA |       Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.

Something tells me i need to setup redis to handle these issues but i could be wrong.

0

There are 0 best solutions below