I am having trouble with my django, docker, nginx, and gunicorn setup. I cannot get the static files to show when running the server even after I collectstatic
them. And when I have nginx, I either get an error saying the port I am using is already allocated, or, if I change the port, I get "bad gateway" but am still able to use the port gunicorn is using -in my case, port 8000.
My docker-compose.yml file:
version: '3'
services:
db:
image: postgres:latest
container_name: rapid_goat_postgres_server_1
nginx:
image: nginx:latest
container_name: rapid_goat_nginx_1
ports:
- "8000:8000"
volumes:
- .:/code
- ./nginx:/etc/nginx/conf.d
- /static:/static
depends_on:
- web
web:
build: .
container_name: rapid_goat_django
command: bash -c "chmod -R 755 . && cd rapidgoat_scraper_product && python3 manage.py collectstatic --noinput && python3 manage.py makemigrations && python3 manage.py migrate && gunicorn rapidgoat_scraper.wsgi -b 0.0.0.0:8000 --workers 6"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
My nginx config file:
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
autoindex on;
root /code/static/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
With the above two files as they are, I get the following error when running docker-compose up --build
:
ERROR: for 693800faea42_693800faea42_693800faea42_my_nginx_docker_container Cannot start service nginx: driver failed programming external connectivity on endpoint my_nginx_docker_container (48eab4028fad0c54d8bbf5669fe746c24f293cfeb11dc57c8566d4010a995a83): Bind for 0.0.0.0:8000 failed: port is already allocated
ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on endpoint my_nginx_docker_container (48eab4028fad0c54d8bbf5669fe746c24f293cfeb11dc57c8566d4010a995a83): Bind for 0.0.0.0:8000 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
And then when I remove the nginx service from the docker-compose.yml file, I can get the gunicorn server up, but it does not serve the static files. I am also using in app templates so my django project structure loosk like this:
- project
-- __init__.py
-- settings.py
-- urls.py
-- wsgi.py
- appname
-- migrations
-- static
--- appname
... then rest of static files like js, css, images... etc
-- templates
--- appname
... then all of my html files. I am using django templating.
-- admin.py
-- apps.py
-- forms.py
-- models.py
-- tests.py
-- views.py
I am also using django 2.0.3
Edit:
Here is the same files when I remove nginx -same as when I change the port for nginx- and gunicorn does not serve the static:
My docker-compose.yml file:
version: '3'
services:
db:
image: postgres:latest
container_name: rapid_goat_postgres_server_1
web:
build: .
container_name: rapid_goat_django
command: bash -c "chmod -R 755 . && cd rapidgoat_scraper_product && python3 manage.py collectstatic --noinput && python3 manage.py makemigrations && python3 manage.py migrate && gunicorn rapidgoat_scraper.wsgi -b 0.0.0.0:8000 --workers 6"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Oh, and my Dockerfile is:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
RUN mkdir /static
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/