Skip to content

Commit

Permalink
update Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadhabibi14 committed Feb 1, 2024
1 parent 4dc7b89 commit 079f600
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 9 deletions.
12 changes: 3 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ setup-python:
pip install -r requirements.txt

setup-js:
cd app
pnpm install

update-dep:
Expand All @@ -14,19 +15,12 @@ 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:
python -m venv .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
docker-compose -f docker-compose.prod.yml up -d --build
68 changes: 68 additions & 0 deletions app/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -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:
4 changes: 4 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:1.25

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
14 changes: 14 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 079f600

Please sign in to comment.