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

后端 未结 8 1386
时光取名叫无心
时光取名叫无心 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 13:02

    I think the reason that I have the same issue is a bug in the latest Docker for Mac beta, but buried in the comments there I was able to find a solution that worked for me & my team. We're using this for local development, where we need our containerized services to talk to a monolith as we work to replace it. This is probably not a production-viable solution.

    On the host machine, alias a known available IP address to the loopback interface:

    $ sudo ifconfig lo0 alias 10.200.10.1/24
    

    Then add that IP with a hostname to your docker config. In my case, I'm using docker-compose, so I added this to my docker-compose.yml:

    extra_hosts:
    # configure your host to alias 10.200.10.1 to the loopback interface:
    #       sudo ifconfig lo0 alias 10.200.10.1/24
    - "relevant_hostname:10.200.10.1"
    

    I then verified that the desired host service (a web server) was available from inside the container by attaching to a bash session, and using wget to request a page from the host's web server:

    $ docker exec -it container_name /bin/bash
    $ wget relevant_hostname/index.html
    $ cat index.html
    

提交回复
热议问题