25

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

Igor L.
  • 3,159
  • 7
  • 40
  • 61
  • looking at the docs this doesn't seem to be possible. docker compose has the project-name option which can override the prefix, but stack doesn't seem to have something similar. – yamenk Oct 03 '17 at 14:44

4 Answers4

17

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
Leandro Sá
  • 529
  • 5
  • 4
  • 5
    As said in the doc: https://docs.docker.com/compose/compose-file/#container_name "The container_name option is ignored when deploying a stack in swarm mode" – Ivan Gonzalez May 09 '20 at 20:30
3

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}

herm
  • 14,613
  • 7
  • 41
  • 62
  • 1
    How can I change the container name which is created with services in docker-compose 3 – ugur Sep 13 '18 at 06:20
  • 1
    As far as I know you can't. Services support having many replicas and container names do not fit in that world – herm Sep 24 '18 at 08:34
  • All I need is to remove the underscore (_) that separates the stack from the service name; is this possible in any way? – A.J Alhorr Mar 31 '21 at 13:08
1

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

Jonathan Komar
  • 2,678
  • 4
  • 32
  • 43
0

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
byram
  • 1
  • 1