docker run --name vs --hostname difference as command line option (ping: bad address) docker run --name vs --hostname difference as command line option (ping: bad address) docker docker

docker run --name vs --hostname difference as command line option (ping: bad address)


1.What is the difference between --name and --hostname in docker run command?

Answer: When we use docker run command docker creates a container and assigns a Container Id of type UUID to it. Now this Container Id can be used to refer to the container created. But remembering this Container Id can be difficult.

So we can use --name in docker run command. Now you can either use the Container Id to refer to the container created or can use the container name for the same.

Similarly when docker container is created the hostname defaults to be the container’s ID in Docker. You can override the hostname using --hostname. I have taken this from Docker docs.

Now consider a scenario where you are making use of docker containers through code and you want to refer to docker. Since docke rid is generated at the time of creation you can't know it in advance so you can use --name. To know when to use --hostname in docker run read from this stackoverflow post

2.Why foo can't reach bar by its hostname = barhost ?

Answer: As specified in the above mentioned stackoverflow post the --hostname doesn't literally change the hostname for docker container such that same can be used to access it from outside. It's use case is similar to why would you want to use --name flag that is you are expecting a certain value which otherwise is generated at the time of container creation.

3.If you can't reach bar by its hostname from foo why it is possible to do that from within bar?

Answer: The answer to this must be clear by now. Inside the container the hostname mentioned using --hostname exists but it is not true outside of the container.