为全栈项目设置 Dockerfile 和 Docker Compose

问题描述 投票:0回答:1

我正在尝试使用 .Net 6 作为后端 API 和 React FE 来完成一个完整的堆栈项目。

我的项目结构是这样的:

.
├── MySolution.sln
├── docker-compose.yml
└── api
    ├── MyApi.csproj
    └── Dockerfile
└── web
    └── Dockerfile

我已设置并运行它,以便我的数据库正在运行,并且我可以在本地计算机上访问它,并且 FE 在浏览器中正常旋转。但是,我不知道如何挂载 api,以便可以通过 FE 的 API 调用来访问它。

当我运行

docker-compose up -d
时,当它到达恢复 nuget 包部分时,它会挂起并尝试从
Dockerfile
进行构建时超时。

如果我从

Dockerfile
中删除该行,它就会卡在下面的构建线上。

我的

docker-compose.yml

version: "3.8"

services:
  web: 
    build: ./web
    ports:
      - 3000:3000
  api:
    build: ./api
    ports:
      - 5001:5001
    environment:
      DB_URL: http://localhost:5001/api/
  db:
    container_name: apidb
    hostname: mssql-db
    image: mcr.microsoft.com/mssql/server:2019-latest
    environment:
      ACCEPT_EULA: "Y"
      MSSQL_SA_PASSWORD: "PassW0rd!"
      MSSQL_PID: "Express"
    ports: 
      - "1433:1433"

我的

Dockerfile
(api)

# Use an official .NET runtime as a base image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

# Set the working directory in the container to /app
WORKDIR /

# Expose ports 80 and 443 in the container
EXPOSE 80
EXPOSE 443

# Pull the .NET 6.0 SDK image from Microsofts container registry
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

# Set the working directory in the container to /src
WORKDIR /

# Copy the source code from the host machine to the container
COPY ["MyApi.csproj", "./"]
# Restore Nuget packages for the project
RUN dotnet restore "./MyApi.csproj"
# Copy the rest of your application code to the container
COPY . .
WORKDIR "/."
# Build the project in Release configuration and output the application to /app/build
RUN dotnet build "MyApi.csproj" -c Release -o /app/build
# Name the current stage of the build as 'publish'
FROM build AS publish
# Publish the applicationin Release configuration to a folder named 'publish'
RUN dotnet publish "MyApi.csproj" -c Release -o /app/publish

# NAme the current stage of the build as final
FROM base AS final
# Change working directory in the container to '/app'
WORKDIR /
# Copy the application from the 'publish' stage to the current stage
COPY --from=publish /publish .
# Set the command that will be run when Docker container is started
ENTRYPOINT ["dotnet", "MyApi.dll"]
c# docker docker-compose dockerfile .net-6.0
1个回答
0
投票

我认为问题与在

/
阶段将工作目录设置为
build
有关。尝试使用
/app
。例如,您的
Dockerfile
的顶部可能如下所示:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

WORKDIR /

EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

WORKDIR /app

COPY MyApi.csproj .

RUN dotnet restore ./MyApi.csproj

您应该会发现效果很好。但为了检验我的假设,你可以将

WORKDIR /app
更改为
WORKDIR /
,你会发现它再次卡住了。

© www.soinside.com 2019 - 2024. All rights reserved.