Consul
Register services running on Docker containers
This topic provides an overview for deploying a Consul server when running Consul on Docker containers.
Register a service
Start a service in a third container and register it with the Consul client. The basic service increments a number every time it is accessed and returns that number.
Pull the container.
$ docker pull hashicorp/counting-service:0.0.2
Run the container with port forwarding so that you can access it from your web browser by visiting http://localhost:9001.
$ docker run \
-p 9001:9001 \
-d \
--name=weasel \
hashicorp/counting-service:0.0.2
Next, you will register the counting service with the Consul client by adding a service definition file called counting.json
in the directory consul/config
.
$ docker exec fox /bin/sh -c "echo '{\"service\": {\"name\": \"counting\", \"tags\": [\"go\"], \"port\": 9001}}' >> /consul/config/counting.json"
Since the Consul client does not automatically detect changes in the configuration directory, you will need to issue a reload command for the same container.
$ docker exec fox consul reload
Configuration reload triggered
If you go back to the terminal window where you started the client, there should be log entries confirming that the Consul client received the hangup signal, reloaded its configuration, and synced the counting service.
2022-12-15T19:02:24.945Z [INFO] agent: Synced service: service=counting