--name, but this doesn’t change the hostname inside the container. Understanding the difference between a container’s name and its hostname is crucial when your application uses hostname-based logic for logging, inter-service communication, or constructing URLs.
Container Name vs Hostname
| Option | Scope | Affects |
|---|---|---|
--name <name> | Docker Engine | User-friendly container identifier at the CLI |
--hostname <name> | Container OS | The hostname returned by hostname inside the container |
The container name and hostname serve different purposes. Some applications generate logs or metrics based on the hostname, so setting it appropriately simplifies debugging and monitoring.
Default Hostname Behavior
By default, Docker sets the hostname to the short version of the container’s unique ID. For example:3484d738 is the container ID—not the friendly webapp name you provided.
Overriding the Hostname
To assign a meaningful hostname inside your container, use the--hostname (or -h) flag. This helps when services rely on consistent hostnames:
hostname command returns your custom name: