Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alpine Linux docker set hostname

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.

like image 664
madi Avatar asked Aug 02 '17 13:08

madi


People also ask

How do I change the hostname on Alpine Linux?

On Alpine Linux, one can use the hostname command to control the system hostname and set a new name. It is the recommended method for all Alpine Linux users. Another option is to update the /etc/hostname file using a text editor such as nano or vi.

Is Alpine Linux good for Docker?

Alpine Linux is a super lightweight Linux distribution that's useful for Docker containers.


2 Answers

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

docker run -d --net mynet --ip 162.18.1.1 --hostname mynodename
like image 60
rubenafo Avatar answered Sep 27 '22 17:09

rubenafo


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.

like image 26
kenorb Avatar answered Sep 27 '22 17:09

kenorb