Skip to main content
When you start a Docker container, you often assign it a name with --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

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:
Inside that container, checking the hostname shows the truncated ID:
Here, 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:
Now, the hostname command returns your custom name:
Your application can now reference a predictable, human-readable hostname.

References

Watch Video