Search code examples
dockerdocker-composehostname

Get host's hostname in docker compose


what is the equivalent of docker swarm's {{.Node.Hostname}} in docker-compose. I am trying to get host's hostname in my docker-compose.yml file. Thanks.


Solution

  • you can use the extra_hosts option of docker compose.

    first you need to define an alias for your host machine loopback in your host machine. for example :

    sudo ifconfig lo0 alias 10.100.100.1/24
    

    then in your docker-compose file:

    extra_hosts:
        - "my_hostname:10.200.10.1"
    

    to check the result run this :

    $ docker exec -it your_container_name /bin/bash
    // run a command to check connectivity to your host machine.like this:
    // supposed you have a website running on host machine 
    $ wget my_hostname/index.html
    $ cat index.html