Connect to a Service running inside a docker conta

2019-01-09 14:17发布

I have a service running in a docker container (local machine). I can see the service URL in the Ambari service config.

Now I want to connect to that service using my local development environment.

I found I can connect to that within the container but when I use that URL outside in my local I get connection refused.

Cause: org.apache.http.conn.HttpHostConnectException: Connect to
xx.xx.xx.com:12008 [xx.xx.xx.com/195.169.98.101] failed: Connection refused

How to connect to a service running inside a container from outside?
In my case code execute in my local machine.

2条回答
成全新的幸福
2楼-- · 2019-01-09 14:30

If your container has mapped its port on the VM 12008 port, you would need to make sure you have port forwarded 12008 in your VirtualBox connection settings, as I mention in "How to connect mysql workbench to running mysql inside docker?"

VBoxManage controlvm "boot2docker-vm" --natpf1 "tcp-port12008 ,tcp,,12008,,12008"
VBoxManage controlvm "boot2docker-vm" --natpf1 "udp-port12008 ,udp,,12008,,12008"
查看更多
等我变得足够好
3楼-- · 2019-01-09 14:51

The question needs more clarification, but I will answer with some assumptions.

I used an Ambari docker image (chose this randomly based on popularity). Then I started 3 clusters as mentioned and my amb-settings and docker ps looked like this:

anovil@anovil-Latitude-E6440:~/tmp/docker-ambari$ amb-settings 
  NODE_PREFIX=amb
  CLUSTER_SIZE=3
  AMBARI_SERVER_NAME=amb-server
  AMBARI_SERVER_IMAGE=hortonworks/ambari-server:latest
  AMBARI_AGENT_IMAGE=hortonworks/ambari-agent:latest
  DOCKER_OPTS=
  AMBARI_SERVER_IP=172.17.0.6
  CONSUL=amb-consul
  CONSUL_IMAGE=sequenceiq/consul:v0.5.0-v6
  EXPOSE_DNS=false
  DRY_RUN=false
anovil@anovil-Latitude-E6440:~/tmp/docker-ambari$ docker ps
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                                                              NAMES
d2483a74d919        hortonworks/ambari-agent:latest    "/usr/sbin/init syste"   20 minutes ago      Up 20 minutes                                                                          amb2
4acaec766eaa        hortonworks/ambari-agent:latest    "/usr/sbin/init syste"   21 minutes ago      Up 20 minutes                                                                          amb1
47e9419de59f        hortonworks/ambari-server:latest   "/usr/sbin/init syste"   21 minutes ago      Up 21 minutes       8080/tcp                                                           amb-server
548730bb1824        sequenceiq/consul:v0.5.0-v6        "/bin/start -server -"   22 minutes ago      Up 22 minutes       53/tcp, 53/udp, 8300-8302/tcp, 8400/tcp, 8301-8302/udp, 8500/tcp   amb-consul
27c725af6531        sequenceiq/ambari                  "/usr/sbin/init"         23 minutes ago      Up 23 minutes       8080/tcp                                                           awesome_tesla
anovil@anovil-Latitude-E6440:~/tmp/docker-ambari$ 

As of now, I can visit the Ambari server through: http://172.17.0.6:8080/

This works also from my host computer. However, if you want this to be connected from another computer from a similar network, then one option is to have a haproxy which does the redirection from: localhost:8080 -> 172.17.0.6:8080

So, I created a small haproxy.cfg and Dockerfile to achieve this:

anovil@anovil-Latitude-E6440:~/tmp/docker-ambari$ cat Dockerfile 
FROM haproxy:1.6

COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
anovil@anovil-Latitude-E6440:~/tmp/docker-ambari$ cat haproxy.cfg 
frontend localnodes
    bind *:8080
    mode http
    default_backend ambari

backend ambari
    mode http
    server ambari-server 172.17.0.6:8080 check


anovil@anovil-Latitude-E6440:~/tmp/docker-ambari$ docker build --rm -t ambariproxy .
Sending build context to Docker daemon 9.635 MB
Step 1 : FROM haproxy:1.6
 ---> af749d0291b2
Step 2 : COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
 ---> Using cache
 ---> 60cdd2c7bb05
Successfully built 60cdd2c7bb05
anovil@anovil-Latitude-E6440:~/tmp/docker-ambari$ docker run -d  -p 8080:8080 ambariproxy
63dd026349bbb6752dbd898e1ae70e48a8785e792b35040e0d0473acb00c2834

Now if I say localhost:8080 or MY_HOST_IP:8080 I can see the ambari-server and this should work also from computers in the same network.

Hope I managed to answer your question :)

Thanks,

查看更多
登录 后发表回答