简体   繁体   中英

Docker port forwarding between container and host

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 )

If you are using boot2docker, that means you need to forward that port at the VM level itself:

VBoxManage controlvm boot2docker-vm natpf1 "name,tcp,127.0.0.1,3307,,3307"

See:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM