4

Playing around with flask I would like to get a real setup up and running in docker. This means flask should be served via nginx and gunicorn. I set up a sample code repository https://github.com/geoHeil/pythonServing but so far can't get nginx to work properly.

Flask is served on application:5000, docker should resolve application to its respective name.

Nginx config is as follows:

server {

    listen 8080;
    server_name application;
    charset utf-8;

    location / {
        proxy_pass http://application:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

which looks good to me. So far I cant find the problem.

edit

compose file is here. Command to start was

docker-compose build
docker-compose up

version: '2'
services:
  application:
    restart: always
    build: ./application
    command: gunicorn -w 4 --bind :5000 wsgi:application
    links:
      - db
    expose:
     - "5000"
    ports:
      - "5000:5000"
  nginx:
    restart: always
    build: ./nginx
    links:
      - application
    expose:
      - 8080
    ports:
      - "8880:8080"
4
  • try to ping application from nginx container. Commented Jan 22, 2017 at 10:22
  • @FarhadFarahi indeed, that works just fine as PING application (192.168.0.3): 56 data bytes 64 bytes from 192.168.0.3: seq=0 ttl=64 time=0.091 ms 64 bytes from 192.168.0.3: seq=1 ttl=64 time=0.129 ms Commented Jan 22, 2017 at 10:39
  • Gimmie the commands you ran to setup the environment, if you use docker compose please provide compose file. Commented Jan 22, 2017 at 11:36
  • @FarhadFarahi please see the edit as well as github.com/geoHeil/pythonServing Commented Jan 22, 2017 at 11:50

2 Answers 2

4

Your nginx config file is in a wrong location.

Steps to fix:

sudo docker-compose down

Delete nginx image:

sudo docker images
sudo docker rmi 
REPOSITORY                       TAG                 IMAGE ID            CREATED              SIZE
pythonserving_nginx              latest              152698f13c7a        About a minute ago   54.3 MB

sudo docker rmi pythonserving_nginx

Now change the nginx Dockerfile:

FROM nginx:1.11.8-alpine
MAINTAINER geoheil

ADD sites-enabled.conf /etc/nginx/conf.d/sites-enabled.conf

Please note the location of nginx config.

Now try this docker-compose file (Using User-defined Networks):

version: '2'
services:
  application:
    restart: always
    build: ./application
    command: gunicorn -w 4 --bind :5000 wsgi:application
    networks:
      - testnetwork
    expose:
     - "5000"
    ports:
      - "5000:5000"
  db:
    restart: always
    image: postgres:9.6.1-alpine
    networks:
      - testnetwork
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=d
      - POSTGRES_PASSWORD=d
      - POSTGRES_DB=d
    volumes:
      - ./postgres:/var/lib/postgresql
  nginx:
    restart: always
    build: ./nginx
    networks:
      - testnetwork
    expose:
      - 8080
    ports:
      - "8880:8080"
networks:
  testnetwork:

And Bring up containers:

sudo docker-compose up

Browse to http://localhost:8880

Sign up to request clarification or add additional context in comments.

13 Comments

strange. that does not yet work. Nothing in the access or error logs.
Did u do all the steps? Because I tried it. Specially removing old images to make compose build new ones
Indeed, I deleted the image. It does not work for me. I am using docker for mac though.
I retried on a fresh VM ubuntu. Same problem there.
sudo docker exec -it <nginx container ID> /bin/sh then ls /etc/nginx/ and ls /etc/nginx/conf.d/, gimme the output for both ls commands
|
-1
Smaple docker file

FROM python:3.5

RUN apt-get update
RUN apt-get install -y --no-install-recommends \
        libatlas-base-dev gfortran nginx supervisor

RUN pip3 install uwsgi

COPY ./requirements.txt /project/requirements.txt

RUN pip3 install -r /project/requirements.txt

RUN useradd --no-create-home nginx

RUN rm /etc/nginx/sites-enabled/default
RUN rm -r /root/.cache

COPY nginx.conf /etc/nginx/
COPY flask-site-nginx.conf /etc/nginx/conf.d/
COPY uwsgi.ini /etc/uwsgi/
COPY supervisord.conf /etc/

COPY /app /project

WORKDIR /project

CMD ["/usr/bin/supervisord"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.