k4l4m
k4l4m

Reputation: 453

Set hostname of service container to host's hostname

I run a docker swarm with docker swarm mode. Let's say I have 4 nodes, 1 manager, 3 worker. The hostnames are:

I start the service in global mode, so every node runs the service once. Let's say the command looks like this:

docker service create --name myservice --mode global --network mynetwork ubuntu wait 3600

mynetwork is an overlay network.

Now I am trying to access the hostname of the docker host in the containers, so I can pass the hostname to an application in the container.

I tried to pass the hostname with the environment variables (--env hostname=$(hostname)), but actually ${hostname} is only executed on the manager and the hostname is set to manager0 for all nodes.

Is there a way to access the hostname or pass the hostname to the containers?

Upvotes: 6

Views: 16820

Answers (2)

Ich
Ich

Reputation: 1378

Here is the feature request, that has been implemented in docker version 17.10

https://github.com/moby/moby/issues/30966

Upvotes: 1

Kane
Kane

Reputation: 8172

You can use latest naming templates to create service with hostname.

Upvotes: 4

Related Questions