Docker - Define the hostname of a container using the docker run -h or --hostname command

by
Jeremy Canfield |
Updated: January 26 2025
| Docker articles
When creating a container using the docker run command the -h or --hostname option can be used to define the hostname of the container.
sudo docker run --hostname foo.example.com --name my-container my-image:latest
Of if the container already exists the docker update command can be used.
sudo docker update --hostname foo.example.com my-container
You can use the docker inspect command to view the JSON for the container.
~]$ sudo docker inspect my-container | jq .[].Config.Hostname
"foo.example.com"
The docker exec command can be used to ensure the container has the hostname you declared.
~]# docker exec foo hostname
foo.example.com
Did you find this article helpful?
If so, consider buying me a coffee over at