16

I am deploying a small stack onto a UCP

One of the issues I am facing is naming the container for service1.

I need to have a static name for the container, since it's utilized by mycustomimageforservice2

The container_name option is ignored when deploying a stack in swarm mode with a (version 3) Compose file.

I have to use version: 3 compose files.

version: "3"
services:

  service1:
    image: dockerhub/service1
    ports: 
      - "8080:8080"
    container_name: service1container
    networks:
      - mynet

  service2:
    image: myrepo/mycustomimageforservice2
    networks:
      - mynet
    restart: on-failure

networks:
  mynet:

What are my options?

Igor L.
  • 3,159
  • 7
  • 40
  • 61

5 Answers5

11

You can't force a containerName in compose as its designed to allow things like scaling a service (by updating the number of replicas) and that wouldn't work with names. One service can access the other using servicename (http://serviceName:internalServicePort) instead and docker will do the rest for you (such as resolving to an actual container address, load balancing between replicas....).

This works with the default network type which is overlay

herm
  • 14,613
  • 7
  • 41
  • 62
  • 1
    To be a little more explicit for others: reference your services like `stack_service` where `stack` is the name you gave your stack, and `service` is the name of the service in the compose file. In the OP's example, this would be `mystack_service1` if the stack's name was `mystack` – radicand May 13 '19 at 14:32
  • @radicand Where do you define the stack name? – Dojo May 25 '20 at 21:39
  • @Dojo, when you deploy the stack: https://docs.docker.com/engine/reference/commandline/stack_deploy/ – radicand May 26 '20 at 01:12
1

You can face your problem linking services in docker-compose.yml file. Something like:

version: "3"
services:

  service1:
    image: dockerhub/service1
    ports: 
      - "8080:8080"
    networks:
      - mynet

  service2:
    image: myrepo/mycustomimageforservice2
    networks:
      - mynet
    restart: on-failure
    links:
      - service1

networks:
  mynet:

Using links arguments in your docker-compose.yml you will allow some service to access another using the container name, in this case, service2 would establish a connection to service1 thanks to the links parameter. I'm not sure why you use a network but with the links parameter would not be necessary.

Yor Jaggy
  • 405
  • 4
  • 11
  • Its so weird... If I start the docker containers on my node with "docker run" instead of a "docker stack deploy" I can ping the containers even without links. But with stack deploy I need link. Strange. – smith64fx Jul 08 '19 at 13:26
  • Ah I just saw that when I enter "stack deploy..." Ignoring unsupported options: links Seems random why it's working lol – smith64fx Jul 08 '19 at 13:30
1

container_name option is ignored when deploying a stack in swarm mode since container names need to be unique. https://docs.docker.com/compose/compose-file/#container_name

SuperUser Sudo
  • 295
  • 2
  • 6
0

If you do have to use version 3 but don't work with swarms, you can add --compatibility to your commands.

-3

Specify a custom container name, rather than a generated default name.

container_name: my-web-container

see this in the full docker-compose file

version: '3.9'
services:
  
  node-ecom:
    build: .
    image: "node-ecom-image:1.0.0"
    container_name: my-web-container
    ports:
      - "4000:3000"
    volumes:
      - ./:/app:ro
      - /app/node_modules
      - /config/.env
    env_file:
      - ./config/.env

know more

MD SHAYON
  • 7,001
  • 45
  • 38