Building a Multi-Container Web App With Docker 1.12.x (Part 2)
Picking up where the introduction left off, now that your components are containerized and ready for scaling, let's finish putting your web app together.
Join the DZone community and get the full member experience.
Join For FreeIn the first part of this article, I showed how I split the frontend, backend, and database all into their own containers, and how each could be individually scaled using docker-compose.
If you’re already familiar with docker-compose and using HAproxy for load balancing against THE container, you might have noticed there’s a limitation in my approach, as the backend REST service was exposing its port 8080 externally, so I don’t think HTTP requests from the frontend browser in the app were ever passing through the HAproxy to be load-balanced — only the requests to load the front end on port 80 were being load-balanced.
I looked into how I could configure HAproxy with multiple backends, listening on different ports, but I eventually came to the conclusion that adding two different HAproxy containers, one load balancing for port 80 and one for port 8080, was easy to do.
I’m not sure if this is the best way to approach this, but it certainly works. Leave me a comment if you have any suggestions.
Here’s my final docker-compose.yml:
version: '2'
services:
mongodata:
image: mongo:3.2
volumes:
- /data/db
entrypoint: /bin/bash
mongo:
image: mongo:3.2
depends_on:
- mongodata
volumes_from:
- mongodata
ports:
- "27017"
addressbook:
image: addressbook
depends_on:
- mongo
environment:
- MONGODB_DB_NAME=addressbook
ports:
- "8080"
links:
- mongo
web:
image: docker-web-angularjs
ports:
- "80"
lb-web:
image: dockercloud/haproxy
depends_on:
- web
environment:
- STATS_PORT=1936
- STATS_AUTH="admin:your-password"
links:
- web
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 80:80
- 1936:1936
lb-addressbook:
image: dockercloud/haproxy
depends_on:
- addressbook
environment:
- STATS_PORT=1937
- STATS_AUTH="admin:your-password"
links:
- addressbook
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 8080:80
- 1937:1937
Related Refcard:
Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments