Connect to docker-machine using 'localhost'

后端 未结 3 851
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  萌比男神i
    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

提交回复
热议问题