Lupa
Lupa

Reputation: 822

How to give a name to containers and images in Docker compose?

I have a simple ASP.NET Core Web with SQL Server project named PostOptimizer created with Visual Studio 2017 and deployed to docker containers. I want to give a name to the each image and container created by Docker-compose because it gives automatically names like this: "dockercompose7669752967822022310_postoptimizer_1" and "dockercompose7669752967822022310_sql.data_1"

I could not figure out how to do it even after reading the Service configuration reference of Docker Compose where I expected to be able to put a subkey of build like name: "thecontainername", but it is not available. How to do it?

This is the content of my docker-compose.yml file:

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

And this is the one of the 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"]

Upvotes: 1

Views: 5409

Answers (1)

Lakshya Garg
Lakshya Garg

Reputation: 782

You can use the subkey container_name it will name the container with your given 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

Upvotes: 4

Related Questions