Igor L.
Igor L.

Reputation: 3475

How to change the service name generated by Docker stack in docker-compose

When deploying a stack of this compose file using:

docker stack deploy -c docker-compose.yml myapp

service-name:
    image: service-image
    namelike-property: my-custom-service-name // here I would like to know the property

The generated service name will be myapp_service-name

I would want it to be named and referenced by my-custom-service-name

Upvotes: 28

Views: 59213

Answers (4)

byram
byram

Reputation: 1

If you need this for referencing from other services by service registry, you can use the following definition.

service-name:
  image: ....
  network:
    my-network:
      aliases:
        - ref-name

Upvotes: 0

Leandro Sá
Leandro Sá

Reputation: 609

I think you can use "container_name" to specify the name of container in your yml file. Some like:

service-name:
    image: service-image
    container_name: my-custom-service-name

Upvotes: 25

Jonathan Komar
Jonathan Komar

Reputation: 3096

I do not think it is supported (yet).

docker service does support this

#!/bin/bash

SERVICE_NAME=myservice

docker service create \
  --name $SERVICE_NAME \
  --hostname $SERVICE_NAME \
  --network myoverlayattachableswarmscopednetwork \
  # IMAGE GOES HERE

I was able to log into a service and ping another one on the same network by the name I provided.

For example

docker exec -it $(docker ps -q -f name=myotherservice) bash
ping myservice

note that container_name is not supported in the docker-compose.yml file when using docker stack up|deploy

Upvotes: 1

herm
herm

Reputation: 16335

For communication between services you can use the serviceName as defined in the compose file (in your case your service name is service-name) if both services communicating are in the same network.

When you do docker service ls the stackname will be shown before every service. That's done because its possible to have two services with the same name which are not in a shared network. You can't change that and it wouldn't make sense to do that because that name is not important and is actually just an ID. You can however change the name of your stack to get ${StackNameILike}:${ServiceNameILike}

Upvotes: 3

Related Questions