Tag Archives: How to Modify Your Hostname

Docker: How to Modify Your Hostname

docker change hostname

When docker is running, it is the same as the container id, and the host name is randomly generated. If you want to fix the host name, I will summarize several common methods.

specify hostname

When docker run, specify the hostname parameter, this parameter will directly write the corresponding hostname to the /etc/hostname file of the machine

docker run -itd --network $netname --hostname $host --name $name yourimage

Specify other machine information

You can also add hostname: use the add-host=$hostname:$ip parameter, which will add a line of mapping to the /etc/hostname file in the container

docker run -itd --network $netname --add-host=$host:$ip --name $name yourimage

If it is through docker-compose, you need to pass the extra_hosts parameter

# docker-compose.yml
extra_hosts:
 - "host1:172.18.0.3"
 - "host2:172.18.0.4"

If deployed through k8s