Ever had a hard time remembering the port numbers of different services in your local
Docker stack? This example demonstrates how you can access
the services via <subdomain>.localhost
instead of localhost:<port>
.
This is a simple node stack running two Express servers in two
different containers. You can access the services via foo.localhost
and bar.localhost
respectively. Here, Caddy is used as a reverse proxy to
direct the requests to the appropriate containers.
This example assumes that you're running a Unix system with Docker >= 20.10.15, Docker compose v2, cURL, and jq installed.
Clone the repo, go to the root directory, and run:
make up
This will spin up three containers—a single reverse proxy and two application containers.
You can make requests to the containers as follows:
curl -s -X GET http://foo.localhost | jq
This returns:
{
"status": "ok",
"statusCode": 200,
"containerIP": "219.20.128.1",
"message": "Hello world from container 1"
}
Similarly, you can access the second service as follows:
curl -s -X GET http://bar.localhost | jq
This returns:
{
"status": "ok",
"statusCode": 200,
"containerIP": "219.20.128.2",
"message": "Hello world from container 2"
}
Here, the containers have static IPs and you can see that the two requests are handled by two different containers.
- Add caching to the CI.