Docker端口在容器和主机之间转发 [英] Docker port forwarding between container and host
问题描述
所以我拉 mariadb
repo并运行这样的容器
$ docker run -p 127.0.0.1:3307:3306 --name mdb -e MYSQL_ROOT_PASSWORD = docker -d mariadb
这将将容器内的端口3306绑定到主机上的localhost或127.0.0.1接口上的端口3307。
它创建容器,我检查它与 $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7d30562194c mariadb/ docker-entrypoint。大约一个小时前Up 6分钟127.0.0.1:3307 - > 3306 / tcp mdb
但是当我telnet到港口,我没有什么
$ telnet 127.0.0.1 3307
尝试127.0.0.1 ...
telnet:连接到地址127.0.0.1:连接拒绝
telnet:无法连接到远程主机
我正在做这件事,因为我在3306端口中运行了一个 mysql
,我不想混淆。我尝试在 -p
选项切换订单,telnet上也没有。你可以帮我看看我在做什么错吗?
(另外我正在运行 boot2docker
OSX 10.9.5
)
基本问题是,如果你要使用 boot2docker
,您需要在boot2docker VM上找到localhost,而不是Mac。
问题清楚,这应该工作:
$ boot2docker ssh
...
$ docker run -p 127.0.0.1:3307:3306 --name mdb -e MYSQL_ROOT_PASSWORD = docker -d mariadb
...
$ telnet 127.0.0.1 3307
但显然你不想每次都进入虚拟机。所以我将用boot2docker VM的IP替换127.0.0.1,你完成了:
$ telnet $(boot2docker ip)3307
I'm new at docker and I'm trying to test some things. From the docs I saw that we can map ports between the container and the host.
So I pull the mariadb
repo and run a container like this
$ docker run -p 127.0.0.1:3307:3306 --name mdb -e MYSQL_ROOT_PASSWORD=docker -d mariadb
This would bind port 3306 inside the container to port 3307 on the localhost or 127.0.0.1 interface on the host machine.
And It creates the container, I check it with $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7d30562194c mariadb "/docker-entrypoint. About an hour ago Up 6 minutes 127.0.0.1:3307->3306/tcp mdb
But when I telnet to the port, I got nothing
$ telnet 127.0.0.1 3307
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
I'm doing this becouse I've got a mysql
running in the 3306 port, and I don't want to mess with that. I try switching the orders on the -p
option and also nothing on the telnet. Can you help me to see what I'm doing wrong?
(Also I'm running over boot2docker
on OSX 10.9.5
)
The basic problem is that if you're using boot2docker
, you need to address localhost on the boot2docker VM, not the Mac.
To make the problem clear, this should work:
$ boot2docker ssh
...
$ docker run -p 127.0.0.1:3307:3306 --name mdb -e MYSQL_ROOT_PASSWORD=docker -d mariadb
...
$ telnet 127.0.0.1 3307
But obviously you don't want to ssh into the VM each time. So I would just replace 127.0.0.1 with the IP of the boot2docker VM and you're done:
$ telnet $(boot2docker ip) 3307
这篇关于Docker端口在容器和主机之间转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!