Anuj TBE
Anuj TBE

Reputation: 9826

docker with nginx and gunicorn not opening in browser

I am new to Docker and setting up docker to run Django application using gunicorn and nginx

my configuration files are

docker-compose.yml

version: '3'

services:
  nginx:
    image: nginx:latest
    container_name: "myapp-nginx"
    ports:
      - "10080:80"
      - "10443:43"
    volumes:
      - .:/app
      - ./config/nginx:/etc/nginx/conf.d
      - ./static_cdn:/static
    depends_on:
      - web
  web:
    build: .
    container_name: "myapp-dev"
    command: ./start.sh
    volumes:
      - .:/app
      - ./static_cdn:/static
    ports:
      - "9010"
    depends_on:
      - db
    expose:
      - "9010"
  db:
    image: postgres
    container_name: "myapp-db"

config/nginx/nginx.conf

upstream web {
    ip_hash;
    server web:9010;
}

server {
    location /static {
        autoindex on;
        alias /static/;
    }

    location / {
        proxy_pass http://web;
    }
    listen 9010;
    server_name localhost;
}

start.sh

#!/usr/bin/env bash

# Start Gunicorn processes
echo --: Starting application build
echo --: Creating migration
python3 manage.py makemigrations
echo ------: makemigrations complete
echo --: Running migration
python3 manage.py migrate
echo ------: migrate complete
echo --: Running collectstatic
python3 manage.py collectstatic <<<yes
echo ------: collectstatic complete
echo --: Starting Gunicorn.
gunicorn koober.wsgi:application \
    --bind 0.0.0.0:9010 \
    --workers 3

running docker using

docker-compose up --build

It runs successfully with output as

enter image description here

Here gunicorn is successfully started at 0.0.0.0:9010. But unable to visit the application using browser.

I tried following address in browser

  1. 127.0.0.1:9010
  2. 127.0.0.1:10080
  3. 127.0.0.1
  4. localhost:9010
  5. localhost:10080
  6. localhost
  7. 0.0.0.0:9010
  8. 0.0.0.0:10080
  9. 0.0.0.0

But none of them is working.

Edit 2: output of docker ps -a

enter image description here

Upvotes: 0

Views: 560

Answers (1)

Rohan J Mohite
Rohan J Mohite

Reputation: 2613

Try with this

upstream web {
  ip_hash;
  server web:9010;
}
server {

 listen       10080;

 location / {
    proxy_pass http://web;
 }     
}

Nginx should listen on 10080 port because in your compose file you have exposed port 80 to 10080 port.

and then try http://localhost:10080 or http://machine-ip-address:10080

here is the blog which I have written to explain how Docker + Nginx + Web application work together.

https://rohanjmohite.wordpress.com/2017/08/02/how-to-configure-docker-with-nginx-and-php-application/

Source code https://github.com/RohanMohite/Docker-Nginx-PHP/blob/master/server_nginx/conf/server.conf

Upvotes: 1

Related Questions