Skip to content
/ imhost Public

Example of Docker Compose's "--scale" with Nginx's round-robin.

License

Notifications You must be signed in to change notification settings

KEINOS/imhost

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ImHost

ImHost is a web server that simply responds the "host name" of the server at port 80.

Its main purpose is for testing and demonstration of load balancing and scaling of Docker containers.

We provide a Dockerfile for convenience. See the "Docker Compose with scale and round-robin" section for the full example using "--scale" option and Nginx as a load balancer as well.

Usage

Docker

$ # Build the image
$ docker build -t imhost https://github.com/KEINOS/imhost.git
**snip**

$ # Run the container (expose the port 80 to 8080)
$ docker run --rm -p 8080:80 imhost
**snip**

$ # Check the hostname
$ curl -sS http://localhost:8080/
Hello from host: f9536b7afae0

Docker Compose with scale and round-robin

Here is an example of docker-compose.yml that can:

  1. Scale-up imhost containers using --scale option.
  2. Load balancing using nginx.
$ # Run the containers and scale the imhost to 5 instances
docker compose up --scale imhost=5 --detach
**snip**

$ # Check the host names.
$ # Note that the host names are different each time because of
$ # the round-robin.
$ curl -sS http://localhost:8080
Hello from host: 2c80ea38d91a

$ curl -sS http://localhost:8080
Hello from host: 5fec68e38b9b

$ curl -sS http://localhost:8080
Hello from host: 353184aa84c0
version: '3'

services:
  imhost:
    build:
      context: https://github.com/KEINOS/imhost.git
    expose:
      - "80"
    restart: unless-stopped

  loadbalancer:
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    ports:
      - "8080:80"
    depends_on:
      - imhost
    restart: unless-stopped
user nginx;

worker_processes auto;

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        location / {
            proxy_pass http://imhost:80/;
        }
    }
}
  • For the full example, see the example directory.

Source code

GitHub go.mod Go version Go Reference GitHub License

Status

Unit Tests GolangCI-Lint Test Docker Tests CodeQL

codecov Go Report Card