简体   繁体   English

docker-compose 容器没有启动

[英]docker-compose container not firing up

New to docker and I have an empty .NET Core API project and an empty vuejs frontend project. docker 新手,我有一个空的 .NET Core API 项目和一个空的 vuejs 前端项目。 I simply want both to be up and running before beginning development.我只想在开始开发之前启动并运行两者。 I have a project that has two docker files (one for the backend and one for the frontend).我有一个项目有两个 docker 文件(一个用于后端,一个用于前端)。 I have a docker-compose.yml file in which I am running the docker-compose up command and my frontend launches fine while my backend does not.我有一个 docker-compose.yml 文件,我在其中运行 docker-compose up 命令,我的前端可以正常启动,而我的后端则不能。 I am running in Linux containers.我在 Linux 容器中运行。

My project structure is the following我的项目结构如下

在此处输入图片说明

docker-compose.yml docker-compose.yml

version: '3.4'

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

Backend 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"]

Frontend Dockerfile前端 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" ]

Error when running docker-compose up运行 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

I'd guess you should look at the copy statements in your backend Dockerfile before the build step.我猜你应该在构建步骤之前查看后端 Dockerfile 中的复制语句。 Looks like youre copying your source files or your csproj file into the wrong directories.看起来您正在将源文件或 csproj 文件复制到错误的目录中。

Lets break down whats happening in the build stage of your backend Dockerfile:让我们分解一下后端 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

You are copying the entire directory into your build image and the resulting file structure would look something like this:您将整个目录复制到构建映像中,生成的文件结构如下所示:

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

You're then executing a dotnet build against the file /src/backend/ which based on the folder structure you shared above expects your source files to be in the same folder.然后,您将对文件/src/backend/执行dotnet build ,该文件基于您在上面共享的文件夹结构,希望您的源文件位于同一文件夹中。

In your backend Dockerfile try:在您的后端 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