Docker compose set container name for stacks

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.


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