run nginx and gunicorn with supervisord in container

This commit is contained in:
Jan Koppe 2020-04-25 10:22:22 +02:00
parent 92bf7fbf75
commit 805e2911f8
Signed by: thunfisch
GPG Key ID: BE935B0735A2129B
5 changed files with 71 additions and 30 deletions

View File

@ -4,23 +4,24 @@ WORKDIR /app
# set env # set env
ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1
EXPOSE 8000
RUN apk update \ # install required packages
&& apk add postgresql-dev gcc python3-dev musl-dev gettext postgresql-client RUN apk add --no-cache postgresql-dev gcc python3-dev musl-dev gettext postgresql-client nginx supervisor
# install dependencies # install dependencies
ADD ./requirements.txt . ADD ./requirements.txt .
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
# add supervisor and nginx configs
ADD ./docker/nginx.conf /etc/nginx/nginx.conf
ADD ./docker/supervisord.conf /etc/supervisord.conf
# add user # add user
RUN addgroup -S portier && adduser -S portier -G portier RUN addgroup -S portier && adduser -S portier -G portier
# add code # add code
ADD --chown=portier:portier . /app ADD --chown=portier:portier . /app
RUN ./manage.py collectstatic --noinput --link RUN ./manage.py collectstatic --noinput --link
RUN ./manage.py compilemessages RUN ./manage.py compilemessages
USER portier
CMD ["/app/start.sh", "migrate_start"] CMD ["/app/start.sh", "migrate_start"]

View File

@ -1,10 +1,10 @@
version: '2.4' version: '2.4'
services: services:
web: app:
build: . build: .
ports: ports:
- 8000:8000 - 80:80
depends_on: depends_on:
- postgres - postgres
- redis - redis

21
docker/nginx.conf Normal file
View File

@ -0,0 +1,21 @@
user nginx;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nodelay on;
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
proxy_pass http://127.0.0.1:8000;
}
location /static {
alias /app/staticfiles;
}
}
}

33
docker/supervisord.conf Normal file
View File

@ -0,0 +1,33 @@
[supervisord]
logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
nodaemon=true ; (start in foreground if true;default false)
user=root
[program:gunicorn]
directory=/app
command=/usr/local/bin/gunicorn -w 4 --bind 127.0.0.1:8000 portier.wsgi
autostart=true
autorestart=true
priority=5
stdout_events_enabled=true
stderr_events_enabled=true
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
user=portier
[program:nginx]
command=/usr/sbin/nginx -g 'pid /tmp/nginx.pid; daemon off;'
autostart=true
autorestart=true
priority=10
stdout_events_enabled=true
stderr_events_enabled=true
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0

View File

@ -1,10 +1,5 @@
#!/bin/sh #!/bin/sh
migrate() {
python manage.py makemigrations
python manage.py migrate
}
wait_for_redis() { wait_for_redis() {
echo "Trying to connect to redis at ${REDIS_HOST:-redis}:${REDIS_PORT:-6379} ..." echo "Trying to connect to redis at ${REDIS_HOST:-redis}:${REDIS_PORT:-6379} ..."
while ! nc -z ${REDIS_HOST:-redis} ${REDIS_PORT:-6379}; while ! nc -z ${REDIS_HOST:-redis} ${REDIS_PORT:-6379};
@ -23,21 +18,12 @@ wait_for_database() {
echo "Successfully connected to database. Continuing."; echo "Successfully connected to database. Continuing.";
} }
case $1 in migrate() {
"create_superuser" ) python manage.py makemigrations
wait_for_redis python manage.py migrate
wait_for_database }
python manage.py createsuperuser --no-input --username "${ADMIN_USER:-admin}" --email "${ADMIN_EMAIL:-post@chaoswest.tv}"
;; wait_for_redis
"migrate_start" ) wait_for_database
wait_for_redis migrate
wait_for_database supervisord -n -c /etc/supervisord.conf
migrate
gunicorn -w 4 --bind 0.0.0.0:${EXPOSE_PORT:-8000} portier.wsgi
;;
"only_start" )
wait_for_redis
wait_for_database
gunicorn -w 4 --bind 0.0.0.0:${EXPOSE_PORT:-8000} portier.wsgi
;;
esac