5

I'm using lwieske/java-8:server-jre-8u121-slim with Alpine Linux

I'd like to set hostname from a text file to be seen globally (for all shells)

/ # env
HOSTNAME=2fa4a43a975c

/ # cat /etc/afile
something

/ # hostname -F /etc/afile
hostname: sethostname: Operation not permitted

everything running as a service in swarm

i want every node to have unique hostname based on container id.

shellter
  • 33,781
  • 7
  • 75
  • 89
madi
  • 150
  • 1
  • 12

4 Answers4

4

You can provide the --hostname flag to docker run as well:

docker run -d --net mynet --ip 162.18.1.1 --hostname mynodename
rubenafo
  • 479
  • 1
  • 6
  • 23
2

As for workaround, can use docker-compose to assign the hostnames for multiple containers.

Here is the example docker-compose.yml:

version: '3'
services:
  ubuntu01:
    image: ubuntu
    hostname: ubuntu01
  ubuntu02:
    image: ubuntu
    hostname: ubuntu02
  ubuntu03:
    image: ubuntu
    hostname: ubuntu03
  ubuntu04:
    image: ubuntu
    hostname: ubuntu04

To make it dynamic, you can generatedocker-compose.yml from the script.

Then run with: docker-compose up.

kenorb
  • 118,428
  • 63
  • 588
  • 624
0

docker service create has a --hostname parameter that allows you to specify the hostname. On a more personal note, if you'll connect to one of your services, then any other service on the same network will be pingable and accessible using the service name, with the added benefit of allowing you multiple replicas without worrying about what those will be named.

omu_negru
  • 4,294
  • 3
  • 21
  • 36
  • 1
    I need to use unique hostname for each node in java runtime parameter and in some other cases. Having same name on all is bad. – madi Aug 02 '17 at 13:39
  • if your service only has one replica then it doesn't matter. If it has multiple replicas, then you shouldn't need to tell between each of them – omu_negru Aug 02 '17 at 13:59
  • well, i need to tell them in app mon for example, want to use this in app logs too. – madi Aug 02 '17 at 14:19
0

Better to be late than never. Found this Q trying to find the same thing myself.

The answer is to give the docker container SYS_ADMIN capability and 'hostname -F' will now set the hostname properly.

docker-compose: cap_add: - SYS_ADMIN