Search code examples
perlnginxdockerdocker-composedancer

Failing to execute nginx proxy_pass directive for a Dancer2 app inside a Docker container


I have tried to orchestrate a Dancer2 app which runs on starman using Docker-compose. I'm failing to integrate nginx it crashes with 502 Bad Gateway error. Which inside my server looks like this :

 *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, 

My docker-compose file looks like this :

  version: '2'
    services:
  web:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    links:
      - pearlbee  
    volumes_from:
      - pearlbee  
  pearlbee:
    build: pearlbee
    command: carton exec  starman  bin/app.psgi
    ports:
      - "5000:5000"
    environment:
      - MYSQL_PASSWORD=secret

    depends_on:
      - mysql
  mysql:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_USER=root    

My nginx.conf file looks like this :

user root nogroup;
worker_processes auto;  
events { worker_connections 512; }

http {
 include /etc/nginx/sites-enabled/*;

    upstream pb{
        # this the localhost that starts starman
        #server 127.0.0.1:5000;
       #the name of the docker-compose service that creats the app
        server pearlbee;
       #both return the same error mesage
    }

    server {

    listen *:80;
    #root /usr/share/nginx/html/;
    #index   index.html 500.html favico.ico;


        location / {

        proxy_pass http://pb;

    }       

 }


}   

Solution

  • You're right to use the service name as the upstream server for Nginx, but you need to specify the port:

    upstream pb{
        server pearlbee:5000;
    }
    

    Within the Docker network - which Compose creates for you - services can access each other by name. Also, you don't need to publish ports for other containers to use, unless you also want to access them externally. The Nginx container will be able to access port 5000 on your app container, you don't need to publish it to the host.