Sunil
Sunil

Reputation: 55

django + uwsgi in docker

What is the best way to deploy Django apps inside a docker. I have looked into couple of blogs and it seems in most of the examples everyone is trying to put nginx + django + uwsgi in one container.

But container should have one process only. so i am trying django and uswgi in one container and nginx is in another container or on host machine itself.

Could some please suggest me best approach.

P.S:- My django app is just providing me REST API results. and i am not using Django template for my static contents.

I am also looking for enabling all these with https. Please share a blog or github link if someone already have achieved similar way of django app hosting.

Upvotes: 2

Views: 4651

Answers (2)

user3284020
user3284020

Reputation: 21

Seems like old thread but maybe someone will find answer to resolve his/her question(s).

You don't really need nginx service if you are not using any static content serving or need it for different specific reason(s).

uwsgi can work as simple http server.

See docs: https://uwsgi-docs.readthedocs.io/en/latest/HTTP.html

Example of CMD command from my Dockerfile:

CMD ["uwsgi", "--http-socket", ":8000", "--py-autoreload", "1", "--module", "app.wsgi:application"]

Command for simpler view:

uwsgi --http-socket :8000 --py-autoreload 1 --module app.wsgi:application

Configuration above is designed and used for local development only.

Adopt it to suit your environment(s) requirements before applying it to your project.

Upvotes: 1

stealthybox
stealthybox

Reputation: 329

The bridging point between most NGINX+uWSGI setups is a uwsgi UNIX socket. This is exposed as a file on the file-system. (You can also use a WSGI over TCP socket)

If you want to run two containers, they need to both have a view of the file-system or network that contains this socket.

In plain docker, you can bind-mount the same volume or host directory into both containers to accomplish what you're talking about. uWSGI will create the socket in this path, and NGINX will communicate with the uWSGI server through the socket:

docker run -v /app/uwsgi/:/app/uwsgi/ my-uwsgi-server-image --socket /app/uwsgi/uwsgi.sock
docker run -v /app/uwsgi/:/app/uwsgi/ nginx
  # your nginx config should set the django upstream to use "/app/uwsgi/uwsgi.sock"

In kubernetes, you can accomplish the same thing with multiple containers in a single Pod using Volumes and VolumeMounts.

Upvotes: 1

Related Questions