From c65f5fe3749d6bedcdfbbe2b3cfae5243d3c8d8e Mon Sep 17 00:00:00 2001 From: Irwan Cahyono Date: Fri, 2 May 2025 13:19:55 +0700 Subject: [PATCH] docker dan gitignore --- .gitignore | 93 +++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 38 ++++++++++++++++ dockers/nginx/Dockerfile | 4 ++ dockers/nginx/nginx.conf | 24 ++++++++++ dockers/python/Dockerfile | 60 +++++++++++++++++++++++++ requirements.txt | 5 +++ 6 files changed, 224 insertions(+) create mode 100644 .gitignore create mode 100644 docker-compose.yml create mode 100644 dockers/nginx/Dockerfile create mode 100644 dockers/nginx/nginx.conf create mode 100644 dockers/python/Dockerfile create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcbd9a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,93 @@ +# Created by https://www.gitignore.io + +### OSX ### +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo +*.pot + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + + +### Django ### +*.log +*.pot +*.pyc +__pycache__/ +local_settings.py +nin/__pycache__/ + +.env +db.sqlite3 + +# virtual env +env/ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f542635 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,38 @@ +version: '3.8' + +services: + web: + build: + context: ./dockers/python + dockerfile: Dockerfile + command: gunicorn hello_django.wsgi:application --bind 0.0.0.0:8000 + volumes: + - static_volume:/home/app/web/staticfiles + - media_volume:/home/app/web/mediafiles + expose: + - 8000 + # env_file: + # - ./.env.prod + # depends_on: + # - db + # db: + # image: postgres:15 + # volumes: + # - postgres_data:/var/lib/postgresql/data/ + # env_file: + # - ./.env.prod.db + nginx: + build: + context: ./dockers/nginx + dockerfile: Dockerfile + volumes: + - static_volume:/home/app/web/staticfiles + - media_volume:/home/app/web/mediafiles + ports: + - 1337:80 + depends_on: + - web + +volumes: + static_volume: + media_volume: \ No newline at end of file diff --git a/dockers/nginx/Dockerfile b/dockers/nginx/Dockerfile new file mode 100644 index 0000000..92b05cd --- /dev/null +++ b/dockers/nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:1.28.0 + +RUN rm /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/conf.d \ No newline at end of file diff --git a/dockers/nginx/nginx.conf b/dockers/nginx/nginx.conf new file mode 100644 index 0000000..b3ab021 --- /dev/null +++ b/dockers/nginx/nginx.conf @@ -0,0 +1,24 @@ +upstream web_django { + server web:8000; +} + +server { + + listen 80; + + location / { + proxy_pass http://web_django; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } + + location /static/ { + alias /home/app/web/staticfiles/; + } + + location /media/ { + alias /home/app/web/mediafiles/; + } + +} \ No newline at end of file diff --git a/dockers/python/Dockerfile b/dockers/python/Dockerfile new file mode 100644 index 0000000..db341f6 --- /dev/null +++ b/dockers/python/Dockerfile @@ -0,0 +1,60 @@ +# First stage: build the Python app +FROM python:3.13.3-bookworm + +# create directory for the app user +RUN mkdir -p /home/app + +# create the app user +RUN addgroup --system app && adduser --system --group app + +# create the appropriate directories +ENV HOME=/home/app +ENV APP_HOME=/home/app/web +RUN mkdir $APP_HOME +RUN mkdir $APP_HOME/staticfiles +RUN mkdir $APP_HOME/mediafiles +WORKDIR $APP_HOME + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# install system dependencies +RUN apt-get update && \ + apt-get install -y --no-install-recommends gcc + +# lint +RUN pip install --upgrade pip +# RUN pip install flake8==6.0.0 +# COPY . /usr/src/app/ +# RUN flake8 --ignore=E501,F401 . + +# install python dependencies +COPY ./requirements.txt . +# RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt +RUN pip install -r requirements.txt + +# install dependencies +# RUN apt-get update && apt-get install -y --no-install-recommends netcat +# COPY --from=builder /usr/src/app/wheels /wheels +# COPY --from=builder /usr/src/app/requirements.txt . +# RUN pip install --upgrade pip +# RUN pip install --no-cache /wheels/* + +# copy entrypoint.prod.sh +# COPY ./entrypoint.prod.sh . +# RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh +# RUN chmod +x $APP_HOME/entrypoint.prod.sh + +# copy project +COPY ../../. $APP_HOME + +# chown all the files to the app user +RUN chown -R app:app $APP_HOME + +# change to the app user +USER app + +# run entrypoint.prod.sh +# ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"] +ENTRYPOINT ["sh", "/entrypoint.sh"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c2709ca --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +asgiref==3.8.1 +Django==5.2 +gunicorn==23.0.0 +packaging==25.0 +sqlparse==0.5.3