Skip to content

Instantly share code, notes, and snippets.

@MoriTanosuke
Last active January 8, 2022 17:08
Show Gist options
  • Save MoriTanosuke/bfe5e5cd28aa76b6c4194043975de61b to your computer and use it in GitHub Desktop.
Save MoriTanosuke/bfe5e5cd28aa76b6c4194043975de61b to your computer and use it in GitHub Desktop.
Example for haproxy+tomcat on docker

This gist is an example of a load balancing setup of tomcat, haproxy and docker.

To run the example, execute the following commands on the command line:

# start up the load balancer and one tomcat instance
docker-compose up -d
# open http://localhost and reload the page a couple of times

# now we start up another tomcat instance
docker-compose scale web=2
# reload http://localhost a couple of times again
# notice how the hostname is changing based on the tomcat instances
version: '2'
services:
web:
image: tomcat:alpine
volumes:
- ./index.jsp:/usr/local/tomcat/webapps/ROOT/index.jsp:ro
lb:
image: dockercloud/haproxy
links:
- web
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 80:80
<html>
<body>
This is host <strong><%= java.net.InetAddress.getLocalHost().getHostName() %></strong>.
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment