How to get the hostname of the docker host from inside a docker container on that host without env vars

后端 未结 8 1391
时光取名叫无心
时光取名叫无心 2020-12-13 12:13

What are the ways get the docker host\'s hostname from inside a container running on that host besides using environment variables? I know I can pass the hostname as

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 12:38

    I'm adding this because it's not mentioned in any of the other answers. You can give a container a specific hostname at runtime with the -h directive.

    docker run -h=my.docker.container.example.com ubuntu:latest
    

    You can use backticks (or whatever equivalent your shell uses) to get the output of hosthame into the -h argument.

    docker run -h=`hostname` ubuntu:latest
    

    There is a caveat, the value of hostname will be taken from the host you run the command from, so if you want the hostname of a virtual machine that's running your docker container then using hostname as an argument may not be correct if you are using the host machine to execute docker commands on the virtual machine.

提交回复
热议问题