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:
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:
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:
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:
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:
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:
Then use the [.inline-code]docker inspect[.inline-code] command to get the hostname of a specific container:
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]:
Then access the Docker API using the [.inline-code]docker[.inline-code] module: