Docker - Define the hostname of a container using the docker run -h or --hostname command
by
Jeremy Canfield |
Updated: February 13 2022
| 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.
In this example, a container is created using the foo:latest image, and the hostname of the container will be foo.example.com.
docker run --hostname foo.example.com foo:latest
The docker container ls command can be used to ensure the container is up and running.
~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d937372c09ab9 b939aa938add9913 "/docker-entrypoin..." 6 minutes ago Up 6 minutes foo
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