From c1d4847da64036dee27a9840d1dbfe77f9e0aeca Mon Sep 17 00:00:00 2001 From: Subangkar Date: Wed, 2 Sep 2020 11:51:01 +0600 Subject: [PATCH 1/7] docker initial with py server --- djangoproject/Dockerfile | 24 ++++++++++++++++++++++++ djangoproject/mememaker/settings.py | 7 ++++++- djangoproject/requirements.txt | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 djangoproject/Dockerfile diff --git a/djangoproject/Dockerfile b/djangoproject/Dockerfile new file mode 100644 index 0000000..b1e331d --- /dev/null +++ b/djangoproject/Dockerfile @@ -0,0 +1,24 @@ +FROM python:3.8.5-buster + +LABEL key="MemesBD" + +ADD . /api +WORKDIR /api +# You will need this if you need PostgreSQL, otherwise just skip this +# RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev +# RUN pip install uwsgi +RUN pip install -r requirements.txt + +RUN sh initdb.sh +# RUN python manage.py collectstatic + +ENV PORT=8000 +EXPOSE 8000 +# Runner script here +# CMD ["/api/runner.sh"] +# CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "mememaker.wsgi:application"] +CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"] + + +# docker build -t mememaker -f Dockerfile . +# docker run -it -p 80:8000 mememaker \ No newline at end of file diff --git a/djangoproject/mememaker/settings.py b/djangoproject/mememaker/settings.py index 4df861b..52b102d 100644 --- a/djangoproject/mememaker/settings.py +++ b/djangoproject/mememaker/settings.py @@ -159,7 +159,6 @@ CORS_ORIGIN_REGEX_WHITELIST = [ 'http://localhost:8080', ] -STATIC_URL = '/static/' MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') SITE_ID = 4 @@ -168,3 +167,9 @@ # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(BASE_DIR, "static") +# STATIC_ROOT = '/home/django/django_project/django_project/static' + +# STATICFILES_DIRS = ( +# os.path.join(BASE_DIR, 'static'), +# ) \ No newline at end of file diff --git a/djangoproject/requirements.txt b/djangoproject/requirements.txt index 9dbe927..33b68c8 100644 --- a/djangoproject/requirements.txt +++ b/djangoproject/requirements.txt @@ -19,6 +19,7 @@ drf-nested-routers==0.91 drf-url-filters==0.5.1 drf-writable-nested==0.6.0 drf-yasg==1.17.1 +gunicorn==20.0.4 idna==2.9 inflection==0.5.0 itypes==1.2.0 From 31706b563b62cec712d61930d56e47a7985e0be1 Mon Sep 17 00:00:00 2001 From: Subangkar Date: Wed, 2 Sep 2020 20:47:46 +0600 Subject: [PATCH 2/7] dockerized with uwsgi server and postgres - static rendering - django settings for static/media/postgres --- djangoproject/Dockerfile | 82 ++++++++++++++++++++++------- djangoproject/mememaker/settings.py | 24 +++++---- djangoproject/mememaker/urls.py | 4 +- djangoproject/requirements.txt | 1 + djangoproject/uwsgi.ini | 22 ++++++++ docker-compose.yml | 61 +++++++++++++++++++++ scripts/entrypoint.sh | 11 ++++ 7 files changed, 174 insertions(+), 31 deletions(-) create mode 100644 djangoproject/uwsgi.ini create mode 100644 docker-compose.yml create mode 100644 scripts/entrypoint.sh diff --git a/djangoproject/Dockerfile b/djangoproject/Dockerfile index b1e331d..a4bdaca 100644 --- a/djangoproject/Dockerfile +++ b/djangoproject/Dockerfile @@ -1,24 +1,70 @@ -FROM python:3.8.5-buster +# FROM python:3.8.5-buster -LABEL key="MemesBD" +# LABEL key="MemesBD" -ADD . /api -WORKDIR /api -# You will need this if you need PostgreSQL, otherwise just skip this -# RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev -# RUN pip install uwsgi -RUN pip install -r requirements.txt +# ADD . /api +# WORKDIR /api +# # You will need this if you need PostgreSQL, otherwise just skip this +# # RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev +# # RUN pip install uwsgi +# RUN pip install -r requirements.txt -RUN sh initdb.sh -# RUN python manage.py collectstatic +# RUN sh initdb.sh +# # RUN python manage.py collectstatic -ENV PORT=8000 -EXPOSE 8000 -# Runner script here -# CMD ["/api/runner.sh"] -# CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "mememaker.wsgi:application"] -CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"] +# ENV PORT=8000 +# EXPOSE 8000 +# # Runner script here +# # CMD ["/api/runner.sh"] +# # CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "mememaker.wsgi:application"] +# CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"] -# docker build -t mememaker -f Dockerfile . -# docker run -it -p 80:8000 mememaker \ No newline at end of file +# # docker build -t mememaker -f Dockerfile . +# # docker run -it -p 80:8000 mememaker + + +FROM python:3.8.5 + +ENV PYTHONDONTWRITEBYTECODE 1 # Prevents Python from writing pyc files to disc +ENV PYTHONUNBUFFERED 1 # Prevents Python from buffering stdout and stderr + +ENV PATH="/scripts:${PATH}" + +# RUN mkdir /app +ADD djangoproject /app +WORKDIR /app + + +RUN apt-get update \ + && apt-get install gcc libc-dev g++ libffi-dev libxml2 libffi-dev unixodbc-dev -y \ + && rm -rf /var/lib/apt/lists/* + + +RUN pip install --upgrade pip + + +COPY djangoproject/requirements.txt /app/ +RUN pip install -r requirements.txt +RUN pip install uwsgi==2.0.19 +RUN apt-get purge -y --auto-remove gcc + + +COPY scripts /scripts +RUN chmod +x /scripts/* + + +COPY djangoproject /app/ + + +RUN mkdir -p /vol/web/media +RUN mkdir -p /vol/web/static + +RUN adduser --disabled-password user + +RUN chown -R user:user /vol +RUN chmod -R 755 /vol/web + +#USER user + +CMD ["entrypoint.sh"] \ No newline at end of file diff --git a/djangoproject/mememaker/settings.py b/djangoproject/mememaker/settings.py index 52b102d..1dc6384 100644 --- a/djangoproject/mememaker/settings.py +++ b/djangoproject/mememaker/settings.py @@ -19,10 +19,10 @@ # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '3jjb4s6f!6x@l+g%oga7&k9i^ipj45x@!5*t2_bnd(-7afckw(' +SECRET_KEY = os.environ.get('SECRET_KEY', '3jjb4s6f!6x@l+g%oga7&k9i^ipj45x@!5*t2_bnd(-7afckw(') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = bool(int(os.environ.get('DEBUG', 0))) ALLOWED_HOSTS = ['*'] @@ -99,8 +99,12 @@ DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': os.environ.get('POSTGRES_DB', 'postgres'), + 'USER': os.environ.get('POSTGRES_USER', 'postgres'), + 'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'postgres'), + 'HOST': os.environ.get('DB_HOST', 'db'), + 'PORT': os.environ.get('DB_PORT', '5432'), } } @@ -160,16 +164,14 @@ 'http://localhost:8080', ] MEDIA_URL = '/media/' -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') +MEDIA_ROOT = '/vol/web/media' if bool(int(os.environ.get('CONTAINER', 0))) else os.path.join(BASE_DIR, 'media') SITE_ID = 4 # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/' -STATIC_ROOT = os.path.join(BASE_DIR, "static") -# STATIC_ROOT = '/home/django/django_project/django_project/static' - -# STATICFILES_DIRS = ( -# os.path.join(BASE_DIR, 'static'), -# ) \ No newline at end of file +STATIC_ROOT = '/vol/web/static' if bool(int(os.environ.get('CONTAINER', 0))) else os.path.join(BASE_DIR, 'static') +STATICFILES_DIRS = ( + os.path.join(BASE_DIR, 'static'), +) diff --git a/djangoproject/mememaker/urls.py b/djangoproject/mememaker/urls.py index b33f505..e123116 100644 --- a/djangoproject/mememaker/urls.py +++ b/djangoproject/mememaker/urls.py @@ -57,5 +57,5 @@ url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), ] -if settings.DEBUG: - urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) +urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) +urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/djangoproject/requirements.txt b/djangoproject/requirements.txt index 33b68c8..6b1e3ae 100644 --- a/djangoproject/requirements.txt +++ b/djangoproject/requirements.txt @@ -29,6 +29,7 @@ MarkupSafe==1.1.1 oauthlib==3.1.0 packaging==20.4 Pillow==7.1.2 +psycopg2==2.8.5 pyparsing==2.4.7 python3-openid==3.1.0 pytz==2020.1 diff --git a/djangoproject/uwsgi.ini b/djangoproject/uwsgi.ini new file mode 100644 index 0000000..299fe9c --- /dev/null +++ b/djangoproject/uwsgi.ini @@ -0,0 +1,22 @@ +[uwsgi] + + +static-map = /static/=/vol/web/static +static-map = /media/=/vol/web/media + +# socket = :8000 # use socket if ngnix is on same machine as server , otherwise use http +http = :8000 # use socket if ngnix is on same machine as server , otherwise use http +module=mememaker.wsgi:application +env=DJANGO_SETTINGS_MODULE=mememaker.settings + +enable-threads +wsgi-file mememaker/wsgi.py +# process-related settings +# master +master = true +# maximum number of worker processes +processes = 10 +# the socket (use the full path to be safe +# chmod-socket = 664 +# clear environment on exit +vacuum = true \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3f2c16b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,61 @@ +version: '3.7' + +volumes: + postgres_data: + static_data: + # frontend_data: + +services: + db: + image: postgres + restart: always + volumes: + - postgres_data:/var/lib/postgresql/data/ + env_file: + - .env + ports: + - 5432:5432 + + web: + build: + context: . + dockerfile: djangoproject/Dockerfile + cache_from: + - mememaker_web:latest + volumes: + - static_data:/vol/web + ports: + - 8000:8000 # change also in uwsig ini and ngnix conf upstream + env_file: + - .env + depends_on: + - db + links: + - db:db + + + # nginx: + # build: + # context: . + # dockerfile: nginx/prod/Dockerfile + # ports: + # - 80:80 # change in ngnix conf + # volumes: + # - static_data:/vol/static + # # - frontend_data:/vol/frontend/static + # # env_file: + # # - nginx/prod/.env.dev + # depends_on: + # - web + + # vue: + # build: + # context: . + # dockerfile: frontend/Dockerfile + # env_file: + # - frontend/.env.dev + # volumes: + # - frontend_data:/app + # depends_on: + # - web + # - nginx diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh new file mode 100644 index 0000000..d399c8b --- /dev/null +++ b/scripts/entrypoint.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +python manage.py makemigrations accounts coreapp +python manage.py migrate + +python manage.py collectstatic --no-input + + +#uwsgi --http :8000 --module=mememaker.wsgi:application --env DJANGO_SETTINGS_MODULE=mememaker.settings --master --enable-threads --wsgi-file mememaker/wsgi.py + +uwsgi --ini uwsgi.ini \ No newline at end of file From ca00971ec3d3dcb1700d1a67914ce9b74bdce82c Mon Sep 17 00:00:00 2001 From: Subangkar Date: Wed, 2 Sep 2020 20:48:14 +0600 Subject: [PATCH 3/7] nginx/vue config for future --- .env | 10 ++++ nginx/prod/Dockerfile | 16 ++++++ nginx/prod/default.conf | 45 ++++++++++++++++ nginx/prod/nginx.conf | 113 ++++++++++++++++++++++++++++++++++++++++ nginx/prod/uwsgi_params | 13 +++++ 5 files changed, 197 insertions(+) create mode 100644 .env create mode 100644 nginx/prod/Dockerfile create mode 100644 nginx/prod/default.conf create mode 100644 nginx/prod/nginx.conf create mode 100644 nginx/prod/uwsgi_params diff --git a/.env b/.env new file mode 100644 index 0000000..3c3182e --- /dev/null +++ b/.env @@ -0,0 +1,10 @@ +POSTGRES_DB=mememaker +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres +DB_HOST=db +DB_PORT=5432 + +DEBUG=0 +SECRET_KEY=foo + +CONTAINER=1 \ No newline at end of file diff --git a/nginx/prod/Dockerfile b/nginx/prod/Dockerfile new file mode 100644 index 0000000..28d3fe9 --- /dev/null +++ b/nginx/prod/Dockerfile @@ -0,0 +1,16 @@ +FROM nginx:latest + +RUN rm /etc/nginx/conf.d/default.conf + +COPY ./nginx/prod/default.conf /etc/nginx/conf.d/default.conf + +COPY ./nginx/prod/uwsgi_params /etc/nginx/uwsgi_params + +COPY ./nginx/prod/nginx.conf /etc/nginx/nginx.conf + + +USER root + +RUN mkdir -p /vol/static +RUN chmod 755 /vol/static + diff --git a/nginx/prod/default.conf b/nginx/prod/default.conf new file mode 100644 index 0000000..7aded88 --- /dev/null +++ b/nginx/prod/default.conf @@ -0,0 +1,45 @@ +server { + listen 80; + listen [::]:80; + server_name dev.mememaker.org; + + #charset koi8-r; + #access_log /var/log/nginx/host.access.log main; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} +} diff --git a/nginx/prod/nginx.conf b/nginx/prod/nginx.conf new file mode 100644 index 0000000..490242f --- /dev/null +++ b/nginx/prod/nginx.conf @@ -0,0 +1,113 @@ +user nginx; +worker_processes 1; +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; +events { + worker_connections 1024; +} +http { + include /etc/nginx/mime.types; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + + + default_type application/octet-stream; + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + access_log /var/log/nginx/access.log main; + sendfile on; + keepalive_timeout 65; + + upstream django { + server web:8000; + } + + server { + listen 80; + listen [::]:80; + server_name dev.mememaker.org; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + + location / { + return 301 https://$host$request_uri; + } + } + + + server { + listen 80; + listen [::]:80; + server_name api.dev.mememaker.org; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + + location / { + return 301 https://$host$request_uri; + } + } + + server{ + listen 443 ssl; + listen [::]:443 ssl; + + server_name api.dev.mememaker.org; + + server_tokens off; + + charset utf-8; + + # max upload size + client_max_body_size 75M; # adjust to taste + + location /static { + alias /vol/static; + } + location /media { + alias /vol/media; + } + location / { + uwsgi_pass django; + include /etc/nginx/uwsgi_params; + } + + ssl_certificate /etc/letsencrypt/live/api.dev.mememaker.org/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/api.dev.mememaker.org/privkey.pem; + } + + server { + listen 443 ssl; + listen [::]:443 ssl; + + server_name dev.mememaker.org; + + + server_tokens off; + + charset utf-8; + + # max upload size + + client_max_body_size 75M; # adjust to taste + + + location / { + root /vol/frontend/static; + index index.html; + try_files $uri $uri/ /index.html; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + ssl_certificate /etc/letsencrypt/live/api.dev.mememaker.org/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/api.dev.mememaker.org/privkey.pem; + } +} diff --git a/nginx/prod/uwsgi_params b/nginx/prod/uwsgi_params new file mode 100644 index 0000000..c7727cd --- /dev/null +++ b/nginx/prod/uwsgi_params @@ -0,0 +1,13 @@ +uwsgi_param QUERY_STRING $query_string; +uwsgi_param REQUEST_METHOD $request_method; +uwsgi_param CONTENT_TYPE $content_type; +uwsgi_param CONTENT_LENGTH $content_length; +uwsgi_param REQUEST_URI $request_uri; +uwsgi_param PATH_INFO $document_uri; +uwsgi_param DOCUMENT_ROOT $document_root; +uwsgi_param SERVER_PROTOCOL $server_protocol; +uwsgi_param REMOTE_ADDR $remote_addr; +uwsgi_param REMOTE_PORT $remote_port; +uwsgi_param SERVER_ADDR $server_addr; +uwsgi_param SERVER_PORT $server_port; +uwsgi_param SERVER_NAME $server_name; From 2f28d73eeb8e676554a1d8205c64b1043d74c996 Mon Sep 17 00:00:00 2001 From: Subangkar Date: Tue, 15 Sep 2020 01:12:28 +0600 Subject: [PATCH 4/7] auth login added for dev --- djangoproject/coreapp/urls.py | 1 - djangoproject/mememaker/settings.py | 16 ++++++++++++---- djangoproject/mememaker/urls.py | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/djangoproject/coreapp/urls.py b/djangoproject/coreapp/urls.py index f2a8bd0..1d482da 100644 --- a/djangoproject/coreapp/urls.py +++ b/djangoproject/coreapp/urls.py @@ -18,5 +18,4 @@ urlpatterns = [ path('', include(router.urls)), path('', include(post_router.urls)), - # path('auth/', include('rest_framework.urls', namespace='rest_framework')), ] diff --git a/djangoproject/mememaker/settings.py b/djangoproject/mememaker/settings.py index 1dc6384..c21f241 100644 --- a/djangoproject/mememaker/settings.py +++ b/djangoproject/mememaker/settings.py @@ -22,7 +22,7 @@ SECRET_KEY = os.environ.get('SECRET_KEY', '3jjb4s6f!6x@l+g%oga7&k9i^ipj45x@!5*t2_bnd(-7afckw(') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = bool(int(os.environ.get('DEBUG', 0))) +DEBUG = bool(int(os.environ.get('DEBUG', 1))) ALLOWED_HOSTS = ['*'] @@ -97,8 +97,12 @@ # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases -DATABASES = { - 'default': { +DATABASE_CONFIGS = { + 'sqlite': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + }, + 'postgres': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': os.environ.get('POSTGRES_DB', 'postgres'), 'USER': os.environ.get('POSTGRES_USER', 'postgres'), @@ -108,6 +112,10 @@ } } +DATABASES = { + 'default': DATABASE_CONFIGS['postgres'] if bool(int(os.environ.get('CONTAINER', 0))) else DATABASE_CONFIGS['sqlite'] +} + # Password validation # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators @@ -174,4 +182,4 @@ STATIC_ROOT = '/vol/web/static' if bool(int(os.environ.get('CONTAINER', 0))) else os.path.join(BASE_DIR, 'static') STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), -) +) if bool(int(os.environ.get('CONTAINER', 0))) else () diff --git a/djangoproject/mememaker/urls.py b/djangoproject/mememaker/urls.py index e123116..9c25424 100644 --- a/djangoproject/mememaker/urls.py +++ b/djangoproject/mememaker/urls.py @@ -50,7 +50,7 @@ url(r'^rest-auth/facebook-connect/$', FacebookConnect.as_view(), name='rest-auth-facebook-connect'), url(r'^rest-auth/google/$', GoogleLogin.as_view(), name='rest-auth-google-login'), url(r'^rest-auth/google-connect/$', GoogleConnect.as_view(), name='rest-auth-google-connect'), - # path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), + path('auth/', include('rest_framework.urls', namespace='rest_framework')), url(r'^swagger(?P\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), From 527b3a0a7bee7916092b3a351384f43ac6c182d4 Mon Sep 17 00:00:00 2001 From: Subangkar Date: Wed, 16 Sep 2020 13:46:52 +0600 Subject: [PATCH 5/7] static/migration moved to build time --- .env | 4 ++-- djangoproject/Dockerfile | 11 ++++++++++- djangoproject/requirements.txt | 2 -- docker-compose.yml | 2 -- scripts/deploy.sh | 6 ++++++ scripts/entrypoint.sh | 7 +------ 6 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 scripts/deploy.sh diff --git a/.env b/.env index 3c3182e..c0cb7a9 100644 --- a/.env +++ b/.env @@ -4,7 +4,7 @@ POSTGRES_PASSWORD=postgres DB_HOST=db DB_PORT=5432 -DEBUG=0 +DEBUG=1 SECRET_KEY=foo -CONTAINER=1 \ No newline at end of file +CONTAINER=1 diff --git a/djangoproject/Dockerfile b/djangoproject/Dockerfile index a4bdaca..fc87cdb 100644 --- a/djangoproject/Dockerfile +++ b/djangoproject/Dockerfile @@ -67,4 +67,13 @@ RUN chmod -R 755 /vol/web #USER user -CMD ["entrypoint.sh"] \ No newline at end of file + +RUN find . -path "*/migrations/*.py" -not -name "__init__.py" -delete && \ + find . -path "*/migrations/*.pyc" -delete + +RUN python manage.py makemigrations && \ + python manage.py migrate + +RUN python manage.py collectstatic --no-input + +CMD ["entrypoint.sh"] diff --git a/djangoproject/requirements.txt b/djangoproject/requirements.txt index 6b1e3ae..0b05b38 100644 --- a/djangoproject/requirements.txt +++ b/djangoproject/requirements.txt @@ -13,13 +13,11 @@ django-extra-fields==2.0.5 django-rest-auth==0.9.5 django-sass-processor==0.8 django-sslserver==0.22 -django-tagconstants==0.1 djangorestframework==3.11.0 drf-nested-routers==0.91 drf-url-filters==0.5.1 drf-writable-nested==0.6.0 drf-yasg==1.17.1 -gunicorn==20.0.4 idna==2.9 inflection==0.5.0 itypes==1.2.0 diff --git a/docker-compose.yml b/docker-compose.yml index 3f2c16b..3993a4a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,8 +13,6 @@ services: - postgres_data:/var/lib/postgresql/data/ env_file: - .env - ports: - - 5432:5432 web: build: diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..7fa7b1b --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +python manage.py makemigrations accounts coreapp +python manage.py migrate + +python manage.py collectstatic --no-input diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index d399c8b..d52ae4d 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,11 +1,6 @@ #!/bin/sh -python manage.py makemigrations accounts coreapp -python manage.py migrate - -python manage.py collectstatic --no-input - #uwsgi --http :8000 --module=mememaker.wsgi:application --env DJANGO_SETTINGS_MODULE=mememaker.settings --master --enable-threads --wsgi-file mememaker/wsgi.py -uwsgi --ini uwsgi.ini \ No newline at end of file +uwsgi --ini uwsgi.ini From d02094653370a4018bdf7cbd7a08ed895157c1d9 Mon Sep 17 00:00:00 2001 From: Subangkar Date: Thu, 17 Sep 2020 15:49:13 +0600 Subject: [PATCH 6/7] static path fixed --- .env | 4 ++-- djangoproject/Dockerfile | 24 ++++++++++++++---------- djangoproject/mememaker/settings.py | 12 +++++++----- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/.env b/.env index c0cb7a9..8f1f5af 100644 --- a/.env +++ b/.env @@ -6,5 +6,5 @@ DB_PORT=5432 DEBUG=1 SECRET_KEY=foo - -CONTAINER=1 +STATIC_PATH=/vol/web/static +MEDIA_PATH=/vol/web/media diff --git a/djangoproject/Dockerfile b/djangoproject/Dockerfile index fc87cdb..f5d1bb9 100644 --- a/djangoproject/Dockerfile +++ b/djangoproject/Dockerfile @@ -30,6 +30,11 @@ ENV PYTHONDONTWRITEBYTECODE 1 # Prevents Python from writing pyc files to disc ENV PYTHONUNBUFFERED 1 # Prevents Python from buffering stdout and stderr ENV PATH="/scripts:${PATH}" +ENV CONTAINER=1 + +ENV STATIC_PATH=/vol/web/static +ENV MEDIA_PATH=/vol/web/media + # RUN mkdir /app ADD djangoproject /app @@ -37,16 +42,15 @@ WORKDIR /app RUN apt-get update \ - && apt-get install gcc libc-dev g++ libffi-dev libxml2 libffi-dev unixodbc-dev -y \ + && apt-get install gcc libc-dev g++ libffi-dev libxml2 libffi-dev unixodbc-dev -y \ && rm -rf /var/lib/apt/lists/* -RUN pip install --upgrade pip - - COPY djangoproject/requirements.txt /app/ -RUN pip install -r requirements.txt -RUN pip install uwsgi==2.0.19 + +RUN pip install --upgrade pip && \ + pip install -r requirements.txt && \ + pip install uwsgi==2.0.19 RUN apt-get purge -y --auto-remove gcc @@ -54,16 +58,16 @@ COPY scripts /scripts RUN chmod +x /scripts/* +COPY .env /app/ COPY djangoproject /app/ -RUN mkdir -p /vol/web/media -RUN mkdir -p /vol/web/static +RUN mkdir -p /vol/web/media /vol/web/static RUN adduser --disabled-password user -RUN chown -R user:user /vol -RUN chmod -R 755 /vol/web +RUN chown -R user:user /vol && \ + chmod -R 755 /vol/web #USER user diff --git a/djangoproject/mememaker/settings.py b/djangoproject/mememaker/settings.py index c21f241..9d0db3b 100644 --- a/djangoproject/mememaker/settings.py +++ b/djangoproject/mememaker/settings.py @@ -172,14 +172,16 @@ 'http://localhost:8080', ] MEDIA_URL = '/media/' -MEDIA_ROOT = '/vol/web/media' if bool(int(os.environ.get('CONTAINER', 0))) else os.path.join(BASE_DIR, 'media') +# MEDIA_ROOT = '/vol/web/media' if bool(int(os.environ.get('CONTAINER', 0))) else os.path.join(BASE_DIR, 'media') +MEDIA_ROOT = os.environ.get('MEDIA_PATH', os.path.join(BASE_DIR, 'media')) SITE_ID = 4 # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/' -STATIC_ROOT = '/vol/web/static' if bool(int(os.environ.get('CONTAINER', 0))) else os.path.join(BASE_DIR, 'static') -STATICFILES_DIRS = ( - os.path.join(BASE_DIR, 'static'), -) if bool(int(os.environ.get('CONTAINER', 0))) else () +# STATIC_ROOT = '/vol/web/static' if bool(int(os.environ.get('CONTAINER', 0))) else os.path.join(BASE_DIR, 'static') +STATIC_ROOT = os.environ.get('STATIC_PATH', os.path.join(BASE_DIR, 'static')) +# STATICFILES_DIRS = ( +# os.path.join(BASE_DIR, 'static'), +# ) if bool(int(os.environ.get('CONTAINER', 0))) else () From 3b68bb9423c7c429d3f9cd6ce703f83b7fb8095f Mon Sep 17 00:00:00 2001 From: Subangkar Date: Thu, 17 Sep 2020 17:02:43 +0600 Subject: [PATCH 7/7] static/ migration moved back to entry - up n running --- djangoproject/Dockerfile | 22 ++-------------------- djangoproject/mememaker/settings.py | 2 +- docker-compose.yml | 3 ++- scripts/entrypoint.sh | 4 ++++ 4 files changed, 9 insertions(+), 22 deletions(-) diff --git a/djangoproject/Dockerfile b/djangoproject/Dockerfile index f5d1bb9..ea0251d 100644 --- a/djangoproject/Dockerfile +++ b/djangoproject/Dockerfile @@ -24,7 +24,7 @@ # # docker run -it -p 80:8000 mememaker -FROM python:3.8.5 +FROM subangkar/drf-mememaker:latest ENV PYTHONDONTWRITEBYTECODE 1 # Prevents Python from writing pyc files to disc ENV PYTHONUNBUFFERED 1 # Prevents Python from buffering stdout and stderr @@ -41,19 +41,6 @@ ADD djangoproject /app WORKDIR /app -RUN apt-get update \ - && apt-get install gcc libc-dev g++ libffi-dev libxml2 libffi-dev unixodbc-dev -y \ - && rm -rf /var/lib/apt/lists/* - - -COPY djangoproject/requirements.txt /app/ - -RUN pip install --upgrade pip && \ - pip install -r requirements.txt && \ - pip install uwsgi==2.0.19 -RUN apt-get purge -y --auto-remove gcc - - COPY scripts /scripts RUN chmod +x /scripts/* @@ -62,7 +49,7 @@ COPY .env /app/ COPY djangoproject /app/ -RUN mkdir -p /vol/web/media /vol/web/static +RUN mkdir -p $MEDIA_PATH $STATIC_PATH RUN adduser --disabled-password user @@ -75,9 +62,4 @@ RUN chown -R user:user /vol && \ RUN find . -path "*/migrations/*.py" -not -name "__init__.py" -delete && \ find . -path "*/migrations/*.pyc" -delete -RUN python manage.py makemigrations && \ - python manage.py migrate - -RUN python manage.py collectstatic --no-input - CMD ["entrypoint.sh"] diff --git a/djangoproject/mememaker/settings.py b/djangoproject/mememaker/settings.py index 9d0db3b..b711dc5 100644 --- a/djangoproject/mememaker/settings.py +++ b/djangoproject/mememaker/settings.py @@ -183,5 +183,5 @@ # STATIC_ROOT = '/vol/web/static' if bool(int(os.environ.get('CONTAINER', 0))) else os.path.join(BASE_DIR, 'static') STATIC_ROOT = os.environ.get('STATIC_PATH', os.path.join(BASE_DIR, 'static')) # STATICFILES_DIRS = ( -# os.path.join(BASE_DIR, 'static'), +# STATIC_ROOT, # ) if bool(int(os.environ.get('CONTAINER', 0))) else () diff --git a/docker-compose.yml b/docker-compose.yml index 3993a4a..abace02 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,7 +19,8 @@ services: context: . dockerfile: djangoproject/Dockerfile cache_from: - - mememaker_web:latest + - subangkar/mememaker:initial + image: subangkar/mememaker:initial volumes: - static_data:/vol/web ports: diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index d52ae4d..10f09e5 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,6 +1,10 @@ #!/bin/sh +python manage.py collectstatic --no-input +python manage.py makemigrations +python manage.py migrate + #uwsgi --http :8000 --module=mememaker.wsgi:application --env DJANGO_SETTINGS_MODULE=mememaker.settings --master --enable-threads --wsgi-file mememaker/wsgi.py uwsgi --ini uwsgi.ini