114

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

letthefireflieslive
  • 11,493
  • 11
  • 37
  • 61

4 Answers4

134

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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 7
    "to use a container's hostname or container name [...] to connect" I think this sentence is a bit misleading, because as you wrote before the container's *hostname* (`--hostname`) is not resolvable, but the *container name* (`--name`) and the *container id* (which will be used as the hostname if you omit `--hostname`) – Murmel Oct 25 '17 at 11:23
  • 4
    @user1885518 completely agree that it was confusing. The implication was that the users outside of the container would not be able to resolve the hostname just by setting that value on the container. Inside of docker networking, there's a different DNS. But the statement was also wrong at least with current versions of docker, the hostname is not added as a network alias, only the container id and the container name resolve with the internal docker DNS. – BMitch Oct 25 '17 at 11:32
  • I had the same problem installing an Arcgis server in a container. It would work the first time after build, but commiting and running a second time I couldn't reach it. After setting a fixed host the server is reachable. It applies to most containerized tools that requires installation after build -> commit -> new run with the commited image(a Tableau server in a container would be another example where you build the image, run a container, install tableau and license, docker commit the image, and then run the commited image...it just needs the same host it had when the product was installed. – John Nov 24 '20 at 22:32
  • But if we run the docker container without --hostname then what is the hostname for it ? – Prashant Singh Aug 18 '22 at 07:32
  • 1
    @PrashantSingh it defaults to a short version of the container ID. That's unique and randomly generated for each container. – BMitch Aug 18 '22 at 13:45
29

--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

shahin
  • 3,515
  • 1
  • 17
  • 17
20

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: Difference between --link and --alias in overlay docker network?

Pang
  • 9,564
  • 146
  • 81
  • 122
Ehud Lev
  • 2,461
  • 26
  • 38
10

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

Justin C
  • 640
  • 7
  • 11
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
  • 2
    I think your example might be even more instructive if you added the `--hostname` parameter (which this question is about). :) – balu Aug 15 '22 at 15:51
  • That's why I started my answer with _This is not a direct answer_ It's a painful lesson I learnt and this was probably the closest google I found – Nick.Mc Aug 16 '22 at 00:56
  • 1
    But your answer _does_ work if you replace `--name` with `--hostname` – Ari Jul 11 '23 at 06:37