Nginx – Docker compose and nginx load balancer

dockerflasknginxpythonUbuntu

Currently I have 2 Docker containers:

  • Application (python flask)
  • Load Balancer (nginx)

I'm starting both containers separately with docker-composer up
Nginx is listening in port 8080 and should forward HTTP requests to application which is listening to port 8081. Currently this is not working as nginx doesnt know anything about the application container.

86615a5b0f02        parzee/loadbalancer   "/usr/local/bin/start"   3 minutes ago       Up 3 minutes        127.0.0.1:8080->8080/tcp   loadbalancer_loadbalancer_1
9adc8a04b356        parzee/application    "/usr/local/bin/start"   6 minutes ago       Up 5 minutes        127.0.0.1:8081->8081/tcp   application_application_1

This is my default configuration

upstream application {
    ip_hash;
        server application_application_1:8081 fail_timeout=0;
        server application_application_2:8081 fail_timeout=0;
    keepalive 64;
}

server {
    listen 8080;
    server_name loadbalancer;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;

    #Timeout after 8 hours
    proxy_read_timeout 43200000;
    proxy_connect_timeout 43200000;

        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://application;
            break;
        }
    }
}

I get:

2016/02/15 08:42:07 [emerg] 21#0: host not found in upstream "application_application_1:8081" in /etc/nginx/sites-enabled/default:3
2016/02/15 08:42:10 [emerg] 22#0: host not found in upstream "application_application_1:8081" in /etc/nginx/sites-enabled/default:3

How to define my nginx configuration by either using hosts or IP addresses?
I haven't used IP addresses as docker may assign different IPs from a known range.
Any suggestions? Should I start all of them in a single docker-compose.yml file?

Best Answer

did you define a link to application_application_1 in your docker-compose.yml file?