When to use --hostname in docker?

Docker

Docker Problem Overview


Is --hostname like a domain name system in docker container environment that can replace --ip when referring to other container?

Docker Solutions


Solution 1 - Docker

The --hostname flag only changes the hostname inside your container. This may be needed if your application expects a specific value for the hostname. It does not change DNS outside of docker, nor does it change the networking isolation, so it will not allow others to connect to the container with that name.

You can use the container name or the container's (short, 12 character) id to connect from container to container with docker's embedded dns as long as you have both containers on the same network and that network is not the default bridge.

Solution 2 - Docker

--hostname is a parameter which can be given along with docker run command which will set the specified name as containers hostname whereas --ip is parameter to set specific ip address(ipv4) to that particular container.

docker run --hostname test --ip 10.1.2.3 ubuntu:14.04 

The following command will create a docker container with base image as ubuntu-14.04 with hostname as test and container ip address as 10.1.2.3

Solution 3 - Docker

If you need to change the hostname in a way that other containers from the same network will see it, just use --net-alias=${MY_NEW_DNS_NAME}

For example:

docker run -d --net-alias=${MY_NEW_DNS_NAME} --net=my-test-env --name=my-docker-name-test <dokcer-contanier>

Please see: https://stackoverflow.com/questions/36048897/difference-between-link-and-alias-in-overlay-docker-network

Solution 4 - Docker

This is not a direct answer, I just want to summarise something that is not immediately clear.

To get containers to talk to each other,

  1. Create a non default network:

docker network create MyNetwork

  1. Connect containers to this network at run time:

docker run --network MyNetwork --name Container1 Image1

docker run --network MyNetwork --name Container2 Image2

Now, if Container1 is for example a web server running on port 80, Processes inside Container2 will be able to resolve it using a host name of Container1 and port 80

Further if Container1 is set up like this:

docker run --network MyNetwork --name Container1 -p 8080:80 Image1

Then

  • Container2 can access Container1:80
  • the Host can access 127.0.0.1:8080

This is summarised from here https://jaaq.medium.com/making-docker-containers-talk-to-each-other-by-hostname-using-container-networking-94835a6f6a5b

You can also confirm containers are connected and check their internal IP addresses using this:

docker network inspect MyNetwork

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionletthefirefliesliveView Question on Stackoverflow
Solution 1 - DockerBMitchView Answer on Stackoverflow
Solution 2 - DockershahinView Answer on Stackoverflow
Solution 3 - DockerEhud LevView Answer on Stackoverflow
Solution 4 - DockerNick.McDermaidView Answer on Stackoverflow