Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share .sock file between nginx docker and uwsgi docker?

I'm using docker-compose and now have two docker containers - one is a nginx webserver, whereas the other one is ubuntu with Python uwsgi and Flask.

As I know, the best way to connect nginx and uWSGI is done by sharing a *.sock file between them and pass the requests into the file (And that what I do in older projects where I did not use dockers).

I'm wondering how can I share the sock file between the dockers in order to enable the communication between them?

And at all.. I'm wondering if this scenario of two containers - one for nginx and one for the Flask framework and uWSGI - is best practice and right to do.

Thanks

like image 708
GMe Avatar asked Feb 04 '23 07:02

GMe


1 Answers

You don't need to share the sock file with different docker container.

The simple solution is share the socket port between dockers.

In the uwsgi file need to add socket=:3000.

[uwsgi]

master=true
chdir=.
module=flaskapp

harakiri=60

callable=app
thunder-lock=true
socket=:3000
workers=12
threads=4
chmod-socket=666
vacuum=true
die-on-term=true
pidfile=uwsgi.pid

max-requests=5000

post-buffering=65536
post-buffering-bufsize=524288

the define the nginx conf. the uwsgi_pass section flask:3000 means (docker server name):(docker expose port).

server {
    listen 80;

    charset     utf-8;
    client_max_body_size 20M;

    location / {
        try_files $uri @slack;
    }

    location @slack {
        include uwsgi_params;
        uwsgi_pass flask:3000;
        uwsgi_read_timeout 60s;
        uwsgi_send_timeout 60s;
        uwsgi_connect_timeout 60s;
    }
}

In docker-compose file:

nginx:
    container_name: nginx
    build:
      context: .
      dockerfile: ./Dockerfile-Nginx
    ports:
      - "80:80"
    depends_on:
      - flask

flask:
    tty: true
    container_name: flask
    build:
      context: .
      dockerfile: ./Dockerfile
    expose:
      - "3000"

    command: uwsgi --ini ./uwsgi.ini

So just let the docker run one process for each docker container.

like image 140
nooper Avatar answered Feb 16 '23 04:02

nooper