Connect to docker-machine using 'localhost'

后端 未结 3 834
伪装坚强ぢ
伪装坚强ぢ 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条回答
  • 2020-12-24 15:50

    The easiest way is to make port forwarding from the VBox

    Settings->Network->Adapter 1->Port Forwarding

    then add Name, in host add 127.0.0.1(for local host) then proper port bindings and restart the VM.

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-12-24 16:08

    You can add a VirtualBox port forward to map a port on the docker host to your local machine.

    Assuming your docker machine is called "default" and you want to map port 80 in your container to localhost:8888 you can run:

    vboxmanage modifyvm default --natpf1 "nameformapping,tcp,,8888,,80"
    

    or if the VM is running

    vboxmanage controlvm default natpf1 "nameformapping,tcp,,8888,,80"
    

    This can also be done in the VirtualBox UI in the settings for the VM. Here is the doc from VirtualBox https://www.virtualbox.org/manual/ch06.html#network_nat

    You'll also need to map the port on your container to the port on the docker machine, you do that when you start the container (this also assumes that you have a "EXPOSE 80" command in your Dockerfile

    docker run -p 80:80 mycontainer
    

    https://docs.docker.com/engine/reference/run/

    Also see: https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

    0 讨论(0)
提交回复
热议问题