Connect to docker-machine using 'localhost'

后端 未结 3 852
伪装坚强ぢ
伪装坚强ぢ 2020-12-24 15:18

There are certain features, like JavaScript service workers without https, that only work on localhost, but when I run my app inside a docker container, using docker-compose

3条回答
  •  Happy的楠姐
    2020-12-24 15:52

    Editing your hosts file causes that your local machine only looks directly to the IP address specified for a domain. So, you could add the ip address of the docker-machine to the etc\hosts file in your local machine and map the port 80 on your container to the port 80 on the docker-machine.

    Example:

    1) Get docker host ip address

    $ docker-machine ip default
    192.168.99.100
    

    2) Add this line to etc/hosts file in your local machine

    192.168.99.100 domain.com
    

    3) Check that your machine is resolving the domain.

    $ ping domain.com
    PING domain.com (192.168.99.100): 56 data bytes
    64 bytes from 192.168.99.100: icmp_seq=0 ttl=64 time=0.294 ms
    64 bytes from 192.168.99.100: icmp_seq=1 ttl=64 time=0.437 ms
    64 bytes from 192.168.99.100: icmp_seq=2 ttl=64 time=0.556 ms
    64 bytes from 192.168.99.100: icmp_seq=3 ttl=64 time=0.270 ms
    

    Notes:

    • For Windows users the hosts file is localted at C:\Windows\System32\Drivers\etc\hosts
    • If you want to support multiple domains in just one single docker-machine, you can create a proxy-container with nginx inside on front of your other containers.

提交回复
热议问题