rednafi / docker-local-subdomain

Locally access Docker services as 'subdomain.localhost' instead of 'localhost:port'

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

Prerequisites

This example assumes that you're running a Unix system with Docker >= 20.10.15, Docker compose v2, cURL, and jq installed.

Installation

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.

Usage

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.

Todo

  • Add caching to the CI.

About

Locally access Docker services as 'subdomain.localhost' instead of 'localhost:port'


Languages

Language:Python 28.0%Language:JavaScript 19.3%Language:TypeScript 17.3%Language:Makefile 13.3%Language:Shell 11.5%Language:Dockerfile 10.5%