Q:

Unable to register Docker service with Registrator

I have a docker, docker-compose and Consul installed on my local machine. Now I want to register one of my services in Consul using Registrator. The Dockerfile of my service is as simple as:
FROM php:7.0-apache
COPY code/ /var/www/html/
ENV SERVICE_ID=php
ENV SERVICE_NAME=php-apache

Besides, I have Consul running. I can check it with simple commands like:
$ curl -X PUT 'Hello world ' 127.0.0.1:8500/v1/kv/msg1
$ curl 127.0.0.1:8500/v1/kv/msg1?raw
Hello world 

I then start registartor container, following one of the tutorials:
$ sudo docker run -d --name registrator-consul -v /var/run/docker.sock:/tmp/docker.sock \
-h jacobian-VirtualBox gliderlabs/registrator \
-ip 127.0.0.1 consul://127.0.0.1:8500

Wnen I run the above command, I get some big key in the console:
a25a48 ... 3b69

Everything looks good, since I get no error messages. If however I check
$ docker ps

I see no active images
But at the same time, if I run
$ docker ps -a

I see this row at the very top:
CONTAINER ID    ...    STATUS

##somecode##    ...    Exited(1) 52 seconds ago

This is exactly registrator-consul container and what I do not like is that it exited. I did not do it myself, so it is really strange that it is not in active state.
I finally build and run my php-apache container:
$ docker build -t php-apache.
$ docker run -d --name php-apache -p 8181:80 php-apache

At this moment, everything works great. I can even check that my service is working. So, when I go to localhost:8181, I see a nice page rendered with Apache web server. BUT. My task is to check whether it was registered in Consul or not. I check it like so:
$ curl jacobian-VirtualBox:8500/v1/catalog/services | jq '.'

But as a result of this command I see this in the console:
{
    "consul": []
}

So, no services were registered. My question is why? What I did wrong and how can I fix it? Thanks!

A:

You run your registrator with -h param. -h - just hostname for your container, not host where it will be deployed. Also you pass 127.0.0.1 as your Consul server address to registrator. 127.0.0.1 is loopback interface for registrator container - not for your host with running Consul server. If your Consul server running on your host (if you can access it by 127.0.0.1:8500) then you need to add --net=host param to your registrator run. 
P.S. I don't know real address of jacobian-VirtualBox host that you are using in curl request. In the case if it's not 127.0.0.1 it will not work and your need to connect your registrator container with Consul service other way (connect to other net or specify other Consul server address if it's available inside registrator container.

