Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: January 17, 2025
In Docker, multiple containers can operate on the same network in a complex system. Therefore, having a custom hostname for Docker containers helps easily identify them over the Docker network.
In this tutorial, we’ll learn how the –hostname flag is used with the Docker run command and some of its practical use cases.
A hostname is a human-readable label used to identify a device on a network. On Linux systems, this is usually set to localhost by default.
In Docker, the hostname serves a similar purpose by allowing seamless identification of the Docker container within the network. Using this hostname, we can communicate with services and other containers within the Docker network. By default, Docker sets the container ID as the default hostname in a container.
We can explicitly define the hostname for a Docker container using the –hostname flag. First, let’s create a Docker network:
$ docker network create my-app-network
This command will create a bridge Docker network with the name “my-app-network”. Let’s now create a Docker container in this network:
$ docker run -itd --hostname backend --network my-app-network --name backend-container ubuntu
The above command will launch a Docker container with an Ubuntu image and set the hostname to “backend” overriding the default container ID. Let’s verify this using the docker exec command:
$ docker exec -it backend-container -- bash
root@backend-container:/# hostname
backend
By providing a specific hostname, we can refer to this container with the name “backend” within its Docker network.
Configuring the hostname doesn’t impact the networking isolation or DNS outside of Docker. This prevents other services from connecting to the container with the hostname outside the Docker network.
To access the container using the hostname, let’s launch another container within the same Docker network:
$ docker run -itd --network my-app-network --name frontend-container ubuntu
We can now ping the “backend-container” from inside the “frontend-container” container using the hostname instead of the IP address:
$ docker exec -it frontend-container bash
$ ping backend
PING backend (172.19.0.2) 56(84) bytes of data.
64 bytes from backend-container.my-app-network (172.19.0.2): icmp_seq=1 ttl=64 time=0.576 ms
64 bytes from backend-container.my-app-network (172.19.0.2): icmp_seq=2 ttl=64 time=0.085 ms
64 bytes from backend-container.my-app-network (172.19.0.2): icmp_seq=3 ttl=64 time=0.083 ms
Here, we ran a ping command using the hostname “backend” and the “backend-container” was reachable.
There are multiple scenarios when explicitly setting the Docker container hostname is useful.
One of them is service discovery. In a microservices architecture, multiple services communicate with each other. Using a hostname makes it easy for the services to discover each other dynamically. Moreover, using hostnames, we can easily update the location of a service without updating its dependent services.
Having a custom hostname also helps in the migration and scaling of the service. Configuring IP addresses in services becomes error-prone when moving services across hosts or scaling them up/down. Using hostnames simplifies infrastructure management by reducing the need for manual updates across potentially complex configuration files.
Moreover, hostnames also facilitate smooth integration testing by enabling developers to set up several environments with distinct configurations as they’re developing. This is achieved as no changes are required in the source code connecting to various services.
In general, hostnames are more readable and meaningful than IP addresses. By assigning logs meaningful identities and enabling monitoring based on service names rather than erratic IP addresses, hostnames can make logging and auditing easier. Logs can contain entries like “web-service”, “database-service”, or “auth-service”, to instantly identify service in a certain activity, instead of using random or fluctuating IP addresses.
For instance, consider two log entries that read “Failed connection from 192.168.1.45” and “Failed connection from web-service“. The latter is far more revealing as it gives context without requiring prior knowledge of infrastructure IP mappings or a separate search database.
In this article, we learned how the –hostname flag in Docker containers offers useful control over network settings. By understanding how to use hostnames, we can efficiently deploy applications and ensure seamless interactions across services.
Also, we learned how hostname management is essential to improve the scalability and robustness of microservices architectures in software development.