簡體   English   中英

docker-compose 容器沒有啟動

[英]docker-compose container not firing up

docker 新手,我有一個空的 .NET Core API 項目和一個空的 vuejs 前端項目。 我只想在開始開發之前啟動並運行兩者。 我有一個項目有兩個 docker 文件(一個用於后端,一個用於前端)。 我有一個 docker-compose.yml 文件,我在其中運行 docker-compose up 命令,我的前端可以正常啟動,而我的后端則不能。 我在 Linux 容器中運行。

我的項目結構如下

在此處輸入圖片說明

docker-compose.yml

version: '3.4'

services:
  backend:
    image: backend
    build:
        context: ./backend
        dockerfile: Dockerfile
  frontend:
      image: frontend
      build:
        context: ./frontend
        dockerfile: Dockerfile

后端 Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /src
COPY ["backend/backend.csproj", "backend/"]
RUN dotnet restore "backend/backend.csproj"
COPY . .
WORKDIR "/src/backend"
RUN dotnet build "backend.csproj" -c Release -o /app/build

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

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

前端 Dockerfile

FROM node:lts-alpine

# install simple http server for serving static content
RUN npm install -g http-server

# make the 'app' folder the current working directory
WORKDIR /app

# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./

# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .

# install project dependencies
RUN npm install

# build app for production with minification
RUN npm run build

CMD [ "http-server", "dist" ]

運行 docker-compose up 時出錯

=> ERROR [backend build 7/7] RUN dotnet build "backend.csproj" -c Release -o /app/build                                                                                                               8.1s 
 => [frontend 1/7] FROM docker.io/library/node:lts-alpine@sha256:dc92f36e7cd917816fa2df041d4e9081453366381a00f40398d99e9392e78664                                                                      0.0s 
 => CANCELED [frontend internal] load build context                                                                                                                                                    7.0s 
 => => transferring context: 35.13MB                                                                                                                                                                   7.0s 
------
 > [backend build 7/7] RUN dotnet build "backend.csproj" -c Release -o /app/build:
#19 1.015 Microsoft (R) Build Engine version 16.11.1+3e40a09f8 for .NET
#19 1.015 Copyright (C) Microsoft Corporation. All rights reserved.
#19 1.015
#19 2.634   Determining projects to restore...
#19 3.501   All projects are up-to-date for restore.
#19 7.322 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/backend/backend.csproj]
#19 7.334
#19 7.334 Build FAILED.
#19 7.334
#19 7.334 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/backend/backend.csproj]
#19 7.334     0 Warning(s)
#19 7.334     1 Error(s)
#19 7.334

我猜你應該在構建步驟之前查看后端 Dockerfile 中的復制語句。 看起來您正在將源文件或 csproj 文件復制到錯誤的目錄中。

讓我們分解一下后端 Dockerfile 的構建階段發生的事情:

FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build       # Use the public dotnet/sdk image as our base build image
WORKDIR /src                                               # Set the current working directory inside our build image to /src 
COPY ["backend/backend.csproj", "backend/"]                # Copy the file ./backend/backend.csproj from our host file system into the build image to the directory ./backend (i.e. /src/backend)
RUN dotnet restore "backend/backend.csproj"                # Restore our dotnet dependencies
COPY . .                                                   # Copy all files from the current directory on our host system to the current directory inside our build image (i.e. /src)
WORKDIR "/src/backend"                                     # Set our current directory inside the build image to /src/backend 
RUN dotnet build "backend.csproj" -c Release -o /app/build # Build our dotnet application

您將整個目錄復制到構建映像中,生成的文件結構如下所示:

/src/
 |- frontend/
 |  |- ...
 |- backend/
    |- Program.cs
    |- Startup.cs
    |- WeatherForcast.cs
    |- backend.csproj
    |...
    |- backend/
       |- backend.csproj
     

然后,您將對文件/src/backend/執行dotnet build ,該文件基於您在上面共享的文件夾結構,希望您的源文件位於同一文件夾中。

在您的后端 Dockerfile 中嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM