Difficulties getting Ngnix + multiple Uwsgi/Flask containers to work together

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Sorry for a long post… that’s the way it goes with Docker, I guess. As an exercise to up-skill and get familiar with Docker and Compose, I’m migrating some non-containerised Flask web apps to containers. I’m using the official Nginx container as a reverse-proxy, fronting for uwsgi over unix domain sockets. Static assets are served directly by Nginx. I have the first Flask container working well, but when I try to add the second container (which is just a stub at this stage) accessing the domain it’s connected to displays the first domain instead. I have a separate nginx config file for each domain, with the server_name set to the relevant domain names, which are entirely different. The uwsgi_pass parameter specifies a different socket file for each app. When I make a request to the second domain, whilst monitoring the logs with docker logs -f nginx_1 I can see the requests coming in, and being processed correctly. If I stop the first domain’s container, subsequent requests to the second container results in nginx reporting that it’s unable to connect to the first container’s socket file. I’m probably doing something stupid, but I had this kind of setup working using native Nginx for years without problems. I know I could just run nginx natively, and only containerise the apps, but that seems “dirty”, and I want to be able to add additional sites as I go along – for instance, dev versions, so I’d like it to be as modular as possible. [edit] If I start a shell in the Nginx container, I can confirm that the sockets are present and permissions are correct, and that all the relevant directories are present. [/edit] The docker-compose.yml looks like this: “` version: ‘3.7’ volumes: socks: app1_static: app2_static: db: services: nginx: build: context: ./nginx ports: – “80:80” volumes: – socks:/tmp/sockets – app1_static:/usr/lib/www/app1/photos – app2_static:/usr/lib/www/app2/static depends_on: – app1 – app2 restart: unless-stopped app1: build: context: ./webapps/app1 volumes: – socks:/tmp/sockets – app1_static:/usr/lib/app/photos – db:/usr/lib/app/db:rw restart: unless-stopped app2: build: context: ./webapps/app2 volumes: – socks:/tmp/sockets – app2_static:/usr/lib/www/app2/static restart: unless-stopped “` The Dockerfiles are: Nginx: “` FROM nginx:latest Deleting the default.conf was an attempt to fix the problem and I know it’s a hack that I probably don’t need. Doesn’t work with or without this. RUN rm /etc/nginx/conf.d/default.conf COPY *.conf /etc/nginx/conf.d/ “` App1: “` FROM python:3-alpine ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 Enable auto-reload ENV FLASK_ENV=production Requirements are installed here to ensure they will be cached. COPY ./requirements.txt /requirements.txt RUN pip install –no-cache-dir -r /requirements.txt && pip install –no-cache-dir gunicorn COPY ./app/gunicorn.sh /gunicorn.sh RUN chmod +x /gunicorn.sh WORKDIR ./app ENTRYPOINT [“/gunicorn.sh”] “` App2: “` FROM python:alpine ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 Disable auto-reload ENV FLASK_ENV=production RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev linux-headers py-pip pcre-dev Install requirements COPY ./requirements.txt /requirements.txt RUN pip install –no-cache-dir -r /requirements.txt && pip install –no-cache-dir gunicorn Copy the app COPY ./app /usr/lib/app WORKDIR /usr/lib/app RUN chmod +x /usr/lib/app/uwsgi.sh RUN chown -R 101:101 /usr/lib/app/* ENTRYPOINT [“/usr/lib/app/uwsgi.sh”] “` And finally, the Nginx config files: app1: “` server { listen 80; server_name app1.co.uk www.app1.co.uk; location / { try_files $uri @app1; } location /photos { alias /usr/lib/www/app1/photos; } location @app1 { include uwsgi_params; uwsgi_pass unix:/tmp/sockets/app1.sock; } } “` app2: “` server { listen 80; server_name app2.xyz www.app2.xyz; location / { try_files $uri @app2; } location /static { alias /usr/lib/www/app2/static; } location @app2 { include uwsgi_params; uwsgi_pass unix:/tmp/sockets/app2.sock; } } “` submitted by /u/cybervegan [link] [comments]

X ITM Cloud News

Patricia

Leave a Reply

Next Post

How are you guys authenticating Azure Kubernetes in Jenkins pipelines?

Sun Aug 2 , 2020
Spread the love          I’m working on a project where Jenkins will run Terraform to stand up Azure resources(container registry, k8s etc), then Docker builds images locally with the final step having these images deployed as containers within the cluster. I initially was using the Terraform provider for provisioning the resources within […]
X- ITM

Cloud Computing – Consultancy – Development – Hosting – APIs – Legacy Systems

X-ITM Technology helps our customers across the entire enterprise technology stack with differentiated industry solutions. We modernize IT, optimize data architectures, and make everything secure, scalable and orchestrated across public, private and hybrid clouds.

This image has an empty alt attribute; its file name is x-itmdc.jpg

The enterprise technology stack includes ITO; Cloud and Security Services; Applications and Industry IP; Data, Analytics and Engineering Services; and Advisory.

Watch an animation of  X-ITM‘s Enterprise Technology Stack

We combine years of experience running mission-critical systems with the latest digital innovations to deliver better business outcomes and new levels of performance, competitiveness and experiences for our customers and their stakeholders.

X-ITM invests in three key drivers of growth: People, Customers and Operational Execution.

The company’s global scale, talent and innovation platforms serve 6,000 private and public-sector clients in 70 countries.

X-ITM’s extensive partner network helps drive collaboration and leverage technology independence. The company has established more than 200 industry-leading global Partner Network relationships, including 15 strategic partners: Amazon Web Services, AT&T, Dell Technologies, Google Cloud, HCL, HP, HPE, IBM, Micro Focus, Microsoft, Oracle, PwC, SAP, ServiceNow and VMware

.

X ITM