使用 'localhost' 连接到 docker-machine [英] Connect to docker-machine using 'localhost'

查看:146
本文介绍了使用 'localhost' 连接到 docker-machine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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, which runs on top of docker-machine, I need to connect to it using the address I get from

docker-machine ip default

Is there a way to map localhost to that ip?

解决方案

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

这篇关于使用 'localhost' 连接到 docker-machine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆