Baeldung Pro – Ops – NPI EA (cat = Baeldung on Ops)
announcement - icon

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.

Partner – Orkes – NPI EA (cat=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

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.

2. Understanding Hostname in Docker

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.

2.1. Updating Hostname in Docker

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.

2.2. Accessing the Container using Hostname

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.

3. Scenarios for Using –hostname

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.

4. Conclusion

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.