我试图在网络上找到如何正确地管理Dockerfile,以使尽可能最好的形象,但不幸的是,没有好的方式出现在我面前。所以我才在这里问。
这是我的背景:
进行了一些修改
这是我的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/core/aspnet:3.1-buster-slim AS base
RUN apt-get update;apt-get install libfontconfig1 -y
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Src/API/API.csproj", "Src/API/"]
RUN dotnet restore "Src/API/API.csproj"
COPY . .
WORKDIR "/src/Src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]
这是我的解决方案结构:
.
| .dockerignore
| mySolution.sln
+---Docs
+---Src
| \---API
| | API.csproj
| | API.csproj.user
| | appsettings.Development.json
| | appsettings.json
| | appsettings.Staging.json
| | Dockerfile
| | Dockerfile.original
| | Program.cs
| | Startup.cs
| +---.config
| | dotnet-tools.json
| +---bin
| +---Controllers (source files)
| +---Data (source files)
| +---Database (source files)
| +---Dtos (source files)
| +---Helpers (source files)
| +---Mail (source files)
| +---Migrations (EF source files)
| +---Models (source files)
| +---obj
| +---PDF (source files)
| +---Properties
| | | launchSettings.json
| +---Services (source files)
| \---wwwroot
| +---Templates
| \---uploads
\---Tests
如您所见,如果我想构建没有VS2019的映像,就必须将Dockerfile放到根目录( .sln文件所在的位置)。
现在,如果我使用这个Dockerfile,Docker将从Src目录中复制所有文件/目录,包括bin / obj目录和wwwroot目录,后者可以包含我上传测试中的一些文件。
如果我在Visual中签入容器中的文件结构:
你可以看到,我不需要所有的文件,只有我的资源,以建立和部署我的应用程序。
如何才能升级我的Dockerfile,以创建最合适的映像?
发布于 2020-12-12 08:12:01
一些小贴士:
以上端口的
为了构建目的,可以使用'-f‘来控制当前上下文,这样就可以将Dockerfile留在内部,但是可以从解决方案根目录中使用上下文,即使您有CI/CD pipelines.
https://stackoverflow.com/questions/65267020
复制相似问题