Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building a Multi-Container Web App With Docker 1.12.x (Part 2)

DZone's Guide to

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.

· Cloud Zone ·
Free Resource

See why enterprise app developers love Cloud Foundry. Download the 2018 User Survey for a snapshot of Cloud Foundry users’ deployments and productivity.

In 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:

Cloud Foundry saves app developers $100K and 10 weeks on average per development cycle. Download the 2018 User Survey for a snapshot of Cloud Foundry users’ deployments and productivity. Find out what people love about the industry standard cloud application platform.

Topics:
spring boot ,docker ,haproxy ,cloud

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}