Docker compose set container name for stacks

Igor L. picture Igor L. · Sep 29, 2017 · Viewed 21.6k times · Source

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?

Answer

herm picture herm · Sep 29, 2017

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