我的文件夹结构如下:
[Services] | +--[IdentityService] (this folder contains Docker & docker-compose file) | +--[ProductService] (this folder contains Docker & docker-compose file) | +--[ApiGateway] (this folder contains Docker & docker-compose file) | +--docker-compose.yml
这是我不同的Docker文件:
1] ApiGateway-Docker文件
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 9000
ENV ASPNETCORE_URLS=http://+:9000
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY ["APIGateway.csproj", "./"]
RUN dotnet restore "./APIGateway.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "APIGateway.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "APIGateway.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
#ENTRYPOINT ["dotnet", "APIGateway.dll"]
ENTRYPOINT ["dotnet", "APIGateway.dll", "--server.urls", "http://0.0.0.0:9000"]
2] IdentityService-Dockerfile
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 9001
ENV ASPNETCORE_URLS=http://+:9001
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["WebApi.csproj", "./"]
RUN dotnet restore "./WebApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApi.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebApi.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
#ENTRYPOINT ["dotnet", "WebApi.dll"]
ENTRYPOINT ["dotnet", "WebApi.dll", "--server.urls", "http://0.0.0.0:9001"]
3] ProductService-Dockerfile
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 9002
ENV ASPNETCORE_URLS=http://+:9002
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Product.API.csproj", "./"]
RUN dotnet restore "./Product.API.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Product.API.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Product.API.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
#ENTRYPOINT ["dotnet", "Product.API.dll"]
ENTRYPOINT ["dotnet", "Product.API.dll", "--server.urls", "http://0.0.0.0:9002"]
我的 docker-compose.yml 如下所示:“
version: "3"
services:
api_gateway:
image: cog/mfp_api_gateway
build:
context: .
dockerfile: Services/ApiGateway/ApiGateway/Dockerfile
ports:
- "8082:9000"
depends_on:
- identity_service
- product_service
identity_service:
image: cog/mfp_identity_service
build:
context: .
dockerfile: Services/IdentityService/Dockerfile
ports:
- "8080:9001"
depends_on:
- db
product_service:
image: cog/mfp_product_service
build:
context: .
dockerfile: Services/ProductService/Dockerfile
ports:
- "8081:9002"
depends_on:
- db
db:
image: "mcr.microsoft.com/mssql/server:2017-latest"
environment:
SA_PASSWORD: "MYPASSWORD"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
volumes:
- ./.db:/var/opt/mssql/
- /var/opt/mssql/data
在这里,当我分别运行docker文件时,所有文件都在正常构建和运行。但是,当我可以运行外部 docker-compose.yml 时,出现了PATH相关错误。我是否需要在 docker-compose.yml 文件中指定相对路径?
答案 0 :(得分:2)
Dockerfile中的Docker命令(更具体地说是COPY
)是相对于当前Docker上下文的。
分别构建图像时,我猜您是从每个docker build
目录运行Services/<service>
的,这意味着上下文是该特定目录。
通过docker-compose构建它们时,您明确表示上下文为.
,即docker-compose.yml
所在的目录。
只需更改构建的上下文,您就应该得到预期的结果=> context: ./Services/<service>
。您可以使用默认名称删除dockerfile
声明。