Terminus
Set Docker Container Hostname

Set Docker Container Hostname

The short answer

In Docker, a container hostname is a label assigned to a container to uniquely identify it within a network to facilitate the communication between containers.

To set the hostname of a Docker container during its creation, you can use the [.inline-code] docker run [.inline-code] command with the [.inline-code]--hostname[.inline-code] flag as follows:

$ docker run --hostname <hostname> <image>

Where: 

  • [.inline-code]docker run[.inline-code] is used to create and run Docker containers.
  • [.inline-code]hostname[.inline-code] is the hostname of the container.
  • [.inline-code]image[.inline-code] is the Docker image the container will be created from.

For example:

$ docker run --hostname my-docker-app ubuntu

This command launches a new container based on the latest version of the [.inline-code]ubuntu[.inline-code] image and sets its hostname to [.inline-code]my-docker-app[.inline-code].

Note that the container's hostname will default to its ID if not specified upon creation.

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]docker run new container with hostname[.inline-code] in the AI Command Suggestion will prompt a [.inline-code]docker[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#container-hostname-vs-name] Differences between a container hostname and name [#container-hostname-vs-name]

In Docker, container hostnames are used for internal network communication between applications running in different containers. They are, by default, visible to the other containers using the same network unless configured for external access. Container hostnames can be dynamically assigned but must be unique within a specific network.

On the other hand, container names serve as identifiers for users. They need to be unique among all running containers on the Docker host, serving as distinct labels for container management.

[#match-container-hostname-to-host] Matching the container hostname with the name of the host [#match-container-hostname-to-host]

To match the hostname of a Docker container with the one of the host machine, you can use the [.inline-code]docker run[.inline-code] command in following way:

$ docker run --hostname=$(hostname) [options] <image>

Where: 

  • The [.inline-code]hostname[.inline-code] command is used to output the name of the current host.
  • The [.inline-code]$()[.inline-code] command substitution syntax is used to execute the specified command and replace it at runtime with its output.

Note that this command substitution mechanism ensures that the container's hostname is synchronized with the hostname of the host system. 
For example: 

$ docker run --hostname=$(hostname) -it ubuntu

This command launches a new container based on the latest version of the [.inline-code]ubuntu[.inline-code] image and dynamically sets its hostname to the current name of the host machine.

[#get-container-hostname-with-cli] Getting a container's hostname using the command-line [#get-container-hostname-with-cli]

To get the hostname of a Docker container, you can use the [.inline-code]docker inspect[.inline-code] command with the [.inline-code]--format[.inline-code] flag as follows: 

$ docker inspect --format='{{.Config.Hostname}}' <container_id>

Where: 

  • [.inline-code]Docker inspect[.inline-code] provides information about Docker containers.
  • [.inline-code]--format[.inline-code] specifies the template to use to format the command's output.
  • [.inline-code]'{{.Config.Hostname}}'[.inline-code] is a format template used to extract the container's hostname.

For example, you can use the [.inline-code]docker ps -a[.inline-code] command to output the list of running and stopped containers:

$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS         PORTS                               NAMES
291e978c6b6a   mysql:8   "docker-entrypoint.s…"   6 months ago   Up 2 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   exciting_davinci

Then use the [.inline-code]docker inspect[.inline-code] command to get the hostname of a specific container:

$ docker inspect --format='{{.Config.Hostname}}' 291e978c6b6a
mysql-database

You can learn how the hostname within Docker containers, including Postgres containers, influences networking and communication setups from this article.

[#get-container-hostname-in-python] Getting a container's hostname using a Python script [#get-container-hostname-in-python]

To get the hostname of a specific Docker container in Python, you can first install the [.inline-code]docker[.inline-code] package with [.inline-code]pip[.inline-code]:

$ pip install docker

Then access the Docker API using the [.inline-code]docker[.inline-code] module:

import docker

def get_container_hostname(container_id):
    client = docker.from_env()
    container = client.containers.get(container_id)
    return container.attrs['Config']['Hostname']

container_id = "container_id" # Replace with the actual container ID
hostname = get_container_hostname(container_id)
print("Container Hostname:", hostname)