diff --git a/Makefile b/Makefile index d85d651..559eed4 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ setup-python: pip install -r requirements.txt setup-js: + cd app pnpm install update-dep: @@ -14,7 +15,7 @@ install-dep: pip install -r requirements.txt migration: - python3 manage.py makemigrations perpus + docker exec web python3 manage.py makemigrations perpus python3 manage.py migrate venv: @@ -22,11 +23,4 @@ venv: source .venv/bin/activate build: - pnpm build - python3 manage.py collectstatic - -setup-linux: - sudo apt update \ - sudo apt install \ - python3-dev default-libmysqlclient-dev python3-pip python3-venv \ - nodejs npm \ No newline at end of file + docker-compose -f docker-compose.prod.yml up -d --build \ No newline at end of file diff --git a/app/Dockerfile.prod b/app/Dockerfile.prod new file mode 100644 index 0000000..4520f53 --- /dev/null +++ b/app/Dockerfile.prod @@ -0,0 +1,68 @@ +########### +# BUILDER # +########### + +# pull official base image +FROM python:3.11.4-slim-buster as builder + +# set work directory +WORKDIR /usr/src/app + +# 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 + + +######### +# FINAL # +######### + +# pull official base image +FROM python:3.11.4-slim-buster + +# 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 +WORKDIR $APP_HOME + +# 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 \ No newline at end of file diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..f73d98b --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,61 @@ +version: "3.8" + +services: + web: + build: + context: ./app + dockerfile: Dockerfile.prod + command: gunicorn perpus.wsgi:application --bind 0.0.0.0:8000 + expose: + - 8000 + env_file: + - ./.env.prod + depends_on: + - db + + db: + image: mariadb + container_name: perpus-db + environment: + MARIADB_DATABASE: "${MARIADB_DATABASE}" + MARIADB_USER: "${MARIADB_USER}" + MARIADB_PASSWORD: "${MARIADB_PASSWORD}" + MARIADB_ROOT_PASSWORD: "${MARIADB_ROOT_PASSWORD}" + MARIADB_TZ: "Asia/Makassar" + command: "--default-time-zone=+08:00" + ports: + - "3306:3306" + volumes: + - ./_tmpdb/mariadb:/var/lib/mysql + networks: + - perpus-app + + phpmyadmin: + depends_on: + - db + image: phpmyadmin/phpmyadmin + container_name: phpmyadmin + restart: always + ports: + - 8080:80 + links: + - db:mysql + environment: + PMA_HOST: db + MYSQL_USERNAME: root + MYSQL_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD} + PMA_ARBITRARY: 1 + volumes: + - /_tmpdb/phpmyadmin + networks: + - perpus-app + + nginx: + build: ./nginx + ports: + - 1337:80 + depends_on: + - web + +networks: + perpus-app: \ No newline at end of file diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..072b014 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:1.25 + +RUN rm /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/conf.d \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 0000000..38fcd02 --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,14 @@ +upstream perpus { + server web:8000; +} + +server { + listen 80; + + location / { + proxy_pass http://perpus; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } +} \ No newline at end of file