Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose set container name for stacks

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?

like image 708
Igor L. Avatar asked Sep 29 '17 08:09

Igor L.


People also ask

Can two containers have same name?

A container with the same name is still existing. Containers can exist in following states, during which the container name can't be used for another container: created.


1 Answers

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

like image 139
herm Avatar answered Sep 18 '22 23:09

herm