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

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

问题描述

有一些功能,如没有https的JavaScript服务工作者,只能在本地主机上工作,但是当我在Docker容器内运行我的应用程序时,使用docker-compose,它运行在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

有没有办法将 localhost 映射到该IP?

Is there a way to map localhost to that ip?

推荐答案

您可以向前添加一个VirtualBox端口,将Docker主机上的端口映射到本地计算机。

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

假设您的Docker机器被称为默认,并且要将容器中的端口80映射到本地主机:8888,您可以运行:

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"

或者VM正在运行

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

这也可以在虚拟机的设置中的VirtualBox UI中完成。以下是VirtualBox中的文档 https://www.virtualbox.org/manual/ch06.html#network_nat

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

您还需要将容器上的端口映射到Docker计算机上的端口,您在启动容器时执行此操作(这也假定您在您的Dockerfile中有一个EXPOSE 80命令

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/

另请参阅: https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

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

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