2

I have configured uwsgi and nginx separately for a python production server following this link. I have configured them separately with working configuration. My uwsgi alone works fine, and nginx alone works fine. My problem is I am planning to use docker for this setup and am not able to run both uwsgi and nginx simultaneously, even though I am using a bash file. Below are the relevant parts in my configuration.

Dockerfile :

  #python setup
  RUN echo "daemon off;" >> /etc/nginx/nginx.conf
  RUN rm /etc/nginx/sites-enabled/default
  RUN ln -s mysite.conf /etc/nginx/sites-enabled/
  EXPOSE 80

  CMD ["/bin/bash", "start.sh"]

mysite.conf

upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
   server 127.0.0.1:8001; # for a web port socket
}


 server {
    listen      80;
    server_name aa.bb.cc.dd; # ip address of the server
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location / {
       uwsgi_pass  django;
       include     /etc/nginx/uwsgi_params; # the uwsgi_params file
    }
 }

start.sh :

service nginx status
uwsgi --socket :8001 --module server.wsgi
service nginx restart  
service nginx status  # ------- > doesn't get executed :( 

out put of the shell file

enter image description here

Can someone help me how to set this up using a bash script ?

Penkey Suresh
  • 5,816
  • 3
  • 36
  • 55

2 Answers2

2

Your start.sh script has the risk to end immediately after executing those two commands.
That would terminate the container right after starting it.

You would need at least to make sure nginx start command does not exit right away.
The official nginx image uses:

 nginx -g daemon off;

Another approach would be to keep your script as is, but use for CMD a supervisor, declaring your script in /etc/supervisor/conf.d/supervisord.conf.
That way, you don't expose yourself to the "PID 1 zombie reaping issue": stopping your container will wait for both processes to terminate, before exiting.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I was aware if this issue and did add nginx daemon off to the script. I've added some more code for clarity. Could you please go through it and let me know what I am getting wrong ? – Penkey Suresh Jun 13 '16 at 03:07
  • @PenkeySuresh when you say "am not able to run both uwsgi and nginx simultaneously,", what error do you see? – VonC Jun 13 '16 at 07:58
  • My build is successful and the docker process is live [when I do docker ps it says it's live]. I could see in the terminal that *** uWSGI is running in multiple interpreter mode *** . But the site is not up. When I go into the container (by doing docker exec bash) and do a check for nginx service it says nginx is not running. I've put both the commands to run uwsgi and nginx in the same bash script – Penkey Suresh Jun 13 '16 at 08:20
  • I changed my bash script to out put the status of nginx before running the uwsgi command. It outputs nginx is not running. After the running of wsgi I again output the status and it doesn't get executed at all. – Penkey Suresh Jun 13 '16 at 08:35
  • I've added a screen shot of the output for clarity – Penkey Suresh Jun 13 '16 at 08:45
  • @PenkeySuresh could you check the `docker logs ` when starting your container? (https://docs.docker.com/engine/reference/commandline/logs/) – VonC Jun 13 '16 at 08:55
  • the logs show same out put as the screenshot I've attached above – Penkey Suresh Jun 13 '16 at 08:58
  • Then docker exec to the container, and check the logs of nginx itself, to see if it was even started. – VonC Jun 13 '16 at 09:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114507/discussion-between-penkey-suresh-and-vonc). – Penkey Suresh Jun 13 '16 at 09:05
1

I think there is a very basic but important alternative worth pointing out.

Your initial scenario was:

  • Production environment.
  • Both uwsgi and nginx working fine alone.
  • TCP socket for uwsgi <=> nginx communication.

I don't think you should go with some complicated trick to run both processes in the same container.

You should simply run uwsgi and nginx in separate containers.

That way you achieve:

  1. Functional Isolation: If you want to replace Nginx by Apache, don't need to modify/rebuild/redeploy your uwsgi container.
  2. Resource Isolation: You can limit memory, CPU and IO separately for nginx and uwsgi.
  3. Loose Coupling: If you want you could even deploy the containers on separate machines (just need to make your upstream server uri configurable).
Nicomak
  • 2,319
  • 1
  • 21
  • 23