如何在Docker Compose中为容器和图像命名?

时间:2019-04-03 04:07:28

标签: docker visual-studio-2017 docker-compose

我有一个简单的带有SQL Server项目的ASP.NET Core Web,该项目名为PostOptimizer,由Visual Studio 2017创建并部署到Docker容器。我想给Docker-compose创建的每个图像和容器起一个名字,因为它会自动给出这样的名称: “ dockercompose7669752967822022310_postoptimizer_1” “ dockercompose7669752967822022310_sql.data_1”

即使在阅读Docker Compose的Service configuration reference之后,我仍然不知道该怎么做,我希望能够放置 build 的子项,例如 名称:“容器名称” ,但不可用。怎么做?

这是我的 docker-compose.yml 文件的内容:

version: '3.4'

services:
  sql.data:
    image: microsoft/mssql-server-linux:2017-latest
    environment:
      - SA_PASSWORD=Sql_Password1
      - ACCEPT_EULA=Y
    ports:
      - "5434:1433"

  postoptimizer:
    image: ${DOCKER_REGISTRY-}postoptimizer
    build:
      context: .
      dockerfile: PostOptimizer/Dockerfile
    depends_on:
      - sql.data

这是Dockerfile之一:

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

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

1 个答案:

答案 0 :(得分:2)

您可以使用子键container_name,它将使用您的给定名称命名容器。

version: '3.4'

services:
  sql.data:
    image: microsoft/mssql-server-linux:2017-latest
    environment:
      - SA_PASSWORD=Sql_Password1
      - ACCEPT_EULA=Y
    ports:
      - "5434:1433"
    container_name: sqldata

  postoptimizer:
    image: ${DOCKER_REGISTRY-}postoptimizer
    build:
      context: .
      dockerfile: PostOptimizer/Dockerfile
    depends_on:
      - sql.data
    container_name: postoptimizer