Monday, 5 June 2017

Proxying http requests to another docker container

I am writing a web application that will be run in an ec2 instance in aws. It consists of two docker containers, a back-end and front-end container. The back-end serves as an api and is written in nodejs, and the front-end is written in reactjs. I can not figure out how to get the containers to communicate in a way that is not only compatible with aws but will also work with an elastic loader balancer. Specifically, if the back-end is getting taxed heavily, I'd like more instances of the back-end to be spun up while still handling requests from the client.

The front-end will make a request to the back-end over http, and the back-end will respond with some data to be displayed in the client (via reactjs).

The front-end docker container exposes port 3000, the back-end container exposes port 3001 in their respective dockerfiles.

Locally I foster communication in node like so:

// package.json
...
"proxy": "http://localhost:3001/",
...

// front-end code
requestServerToProcessParameters(parameters) {
    return new Promise(function(resolve, reject) {
        request
            .post('/ProcessCase/DataPoints')
            .send(parameters)
            .end(function(err, res) {

                if (err) {
                    console.log(err);
                    reject(err)
                } else {
                    resolve(res);
                }
            });
    });
}

As you can see the front-end proxies the request to the back-end, however, this is hardcoded and in a true restful api neither the front or back-end should know about each other.

So I guess my question is, what would be the best way for these docker containers to communicate to each other? I don't really want them living on the same docker-network because again, the two containers shouldn't really know anything about each other. Can the elastic load balancer route my requests from the front-end to the back-end for me?



via Alex Cauthen

No comments:

Post a Comment