Alpine Linux docker set hostname

Viewed 5423

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.

4 Answers

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.

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

Related