0

When I use

docker run --it --name myContainer --hostname myShortHostName ubuntu bash

That will add myShortHostName to /etc/hostname file. And 172.169.X.X myShortHostName in /etc/hosts file.

but in this case, I use hostname -f command to fetch my container's FQDN, because there is no FQDN entry in /etc/hosts file. And I cannot edit it because it gets replaced each time the container boots.

And if I used:

docker run --it --name myContainer --hostname myShortHostName.DOMAIN ubuntu bash

That will add the FQDN to /etc/hostname file, which is not the recommended convention by hostname rules, and it is not the convention I used to use in my system I am administrating.

The recommended method of setting the FQDN is to make the hostname be an alias for the fully qualified name using /etc/hosts, DNS, or NIS. For example, if the hostname was "ursula", one might have a line in /etc/hosts which reads

         127.0.1.1    ursula.example.com ursula

Is there any way (actually I need ideal way, not editing the files with a script after booting the container) to only add the short myShortHostName to /etc/hostname file, and the FQDN to /etc/hosts file using docker run command or any Docker's built in commands or techniques?

What I need is to be able to get the ShortHostName by hostname -s or hostname, and the ShortHostName.DOMAIN (FQDN) by using hostname -f inside the Docker container without adding the FQDN to /etc/hostname file.

1 Answer 1

0

You can ensure the FQDN is added to /etc/hosts in your Docker container by passing the --domainname argument to your docker run command. For example running your container with the following command:

docker run --it --name myContainer --hostname ursula --domainname example.com ubuntu bash

will result in the following entry being created in /etc/hosts:

172.169.X.X ursula.example.com ursula

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .