Con-tainer2sul automatically registers and deregisters nodes for any Docker container.
This project is inspired by gliderlabs registrator.
The main difference is that Con-tainer2sul will by default register the containers as nodes. This allows to register a container without having to expose any network service on the host.
docker run -d \
-e c2c_consul__host=localhost \
-e c2c_consul__port=8500 \
-v /var/run/docker.sock:/var/run/docker.sock \
vidiben/container2sul
Con-tainer2sul uses rc to import its configuration.
The configuration can be overridden using any method supported by rc, for instance using a custom .c2crc
file or by defining some environmental variables prefixed by c2c_
.
Consul host
and port
can be configured:
.c2crc
{
"consul": {
"host": "localhost",
"port": 8500
},
"[..]": "[..]"
}
The docker
section of the configuration is given to dockerode constructor.
Please refer to dockerode documentation for supported options.
Default configuration listens to Docker daemon on /var/run/docker.sock
unix socket.
.c2crc
{
"[..]": "[..]",
"docker": {
"socketPath": "/var/run/docker.sock"
}
}
Con-tainer2sul uses bunyan. The logger
section of the configuraton object is passed to bunyan.createLogger
method.
Default configuration outputs logs at info
level to stdout
.
.c2crc
{
"[..]": "[..]",
"logger": {
"name": "con-tainer2sul"
}
}
Con-tainer2sul relies on Docker labels to define the values sent to Consul.
Internally, Con-tainer2sul calls Consul low-level catalog/register and catalog/deregister API endpoints.
By default, the registered ip address is the first one found in the NetworkSettings
group from docker inspect
result.
If the container is running in host
network mode, the ip address used is the docker0
one.
The registered ip address can be configured using a consul.ip
label.
docker run --rm --name c2c -ti -l consul.ip=10.0.0.1 alpine ash
will register a service c2c
on a node c2c
, which will be registered to the ip 10.0.0.1
.
By setting the consul.ip
label to host
, Con-tainer2sul will use docker0
ip address for the node.
By default, the registered service name is the container name.
The service name can be configured using a consul.service
label.
docker run --rm --name c2c -ti -l consul.service=myservice alpine ash
will register a service myservice
in Consul.
Tags can be set using a consul.tags
label, container the list of tags, comma separated.
docker run --rm --name c2c -ti -l consul.tags=www,api alpine ash
will register a service c2c
with www
and api
tags.
For the time being, Con-tainer2sul support registering only one port.
Port can be set using a consul.port
label.
docker run --rm --name c2c -ti -l consul.port=80 alpine ash
will register a service c2c
on port 80.
Each container can be attached some key/value pairs, using the consul.kv.<KEY>
template.
docker run --rm --name c2c -ti -l consul.kv.foo=bar -l consul.kv.bar=baz alpine ash
will register a service c2c
and create 2 key/value pairs under the /services/c2c/
tree.2 key/value pairs under the /services/c2c/
tree.
You can tell Con-tainer2sul to not register the container by using a container.skip
label, set to anything else than false
.
docker run --rm --name c2c -ti -l consul.skip=true ash
will not register any node or service in Consul.