Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack service containers dynamic hostname

I am migrating away from Docker Cloud to pure Docker Swarm setup. One thing that I am missing is the nice way of how the containers got the hostname set as $SERVICE_NAME-$SLOT_NUMBER.

Is it possible to dynamically set the hostname/container name in a swarm stack service?

like image 749
pjotr_dolphin Avatar asked Apr 08 '18 16:04

pjotr_dolphin


People also ask

How do I assign a hostname to a container?

Basic idea is to use docker inspect to obtain the pid of the container, then enter the uts namespace of the container via nsenter . Running hostname inside that namespace will change the hostname for the docker instance that shares that namespace.

What is the hostname of a container?

The hostname of a Container is the name of the Pod in which the Container is running. It is available through the hostname command or the gethostname function call in libc.

What is stack in container?

Whereas stack weight is the maximum amount of weight of the containers that can be loaded on top of the containers. Stack weight as the name suggests it is a weight that can be stacked upon a container. Load Density is often associated with bulk carriers and stack weight with containers.

What is a docker container stack?

Docker Stack sits at a higher level than Docker containers and helps to manage the orchestration of multiple containers across several machines. Docker Stack is run across a Docker Swarm, which is essentially a group of machines running the Docker daemon, which are grouped together, essentially pooling resources.


1 Answers

In your stack/compose file, use this format:

services:
  thaservice:
    ...
    hostname: "{{.Service.Name}}-{{.Task.Slot}}"

Link to documentation for available template variables: https://docs.docker.com/engine/swarm/services/#create-services-using-templates

like image 180
pjotr_dolphin Avatar answered Oct 05 '22 23:10

pjotr_dolphin