Docker container - how to configure so it gets a viable IP address when running in vagrant?

妖精的绣舞 提交于 2019-12-02 17:43:52

The VM generated by vagrant is indeed isolated, in order to access it from your host, you can allocate a private network to it. Instead of doing config.vm.network :bridged, try config.vm.network :private_network, ip: "192.168.50.4", It should do the trick

However, this will only allow you to access the VM itself, not the containers.
In order to do so, when running the container, you can add the -p option

ex: docker run -d -p 8989 base nc -lkp 8989

This will run a netcat listening on 8989 within a container and expose the port publicly. As it is also run with -d, the container will be in detached mode and the only output will be the container's ID

In order to expose the port, Docker do a simple NAT. In order to know the real port, you can

do docker port <ID of the container> 8989

Netcat will be available from the mac at 192.168.50.4:<result>

I just wrote a tutorial of how to use a host-only network and TCP routing to make this pretty easy. This way you don't have to map every specific port.

http://ispyker.blogspot.com/2014/04/accessing-docker-container-private.html

Important points ...

1) Add host-only network to Virtual Box 2) Tell the boot2docker VM to have an adapter on the host-only network 3) Add an IP for the new boot2docker VM host-only networking adapter 4) Route all Mac OS X traffic for the docker container subnet to that boot2docker VM host-only networking IP

Actual steps are on the blog with output so you can compare to what you see as you follow them.

I have installed tomcat from my Dockerfile and forwarded that to 6060 using vagrant`s port forwarding. These are the steps worked for me:

vagrant provision
vagrant up
vagrant ssh
box_name$ docker run  -i -t -p 8080:8080 bsb_tomcat6 /bin/bash

Able to see tomcat up & running on localhost:6060, as I have done port forwarding to 6060 in my Vagrantfile

you also can define PRIVATE_NETWORK and FORWARD_DOCKER_PORTS environment variables to access your services that are running in docker containers:

$ vagrant halt
$ export PRIVATE_NETWORK=192.168.50.4
$ export FORWARD_DOCKER_PORTS=1
$ vagrant up

In my case i can access postgres from Mac using

$ telnet 192.168.50.4 49154

to find out actual application port you can use

$ sudo docker port 1854499c6547 5432
0.0.0.0:49154
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!