From 4e605a8a8d6e3369f053c7828cab37f0337f9181 Mon Sep 17 00:00:00 2001 From: Sosokker Date: Sat, 1 Feb 2025 03:13:26 +0700 Subject: [PATCH] chore: add Dockerfile and compose --- Dockerfile | 21 +++++++++++++++++++ Installation.md | 52 ++++++++++++++++++++++++++++++++++++++++++---- README.md | 50 ++++++++++++++++++++++++++++++++++---------- docker-compose.yml | 12 +++++++++++ entrypoint.sh | 20 ++++++++++++++++++ 5 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f10edff --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM ubuntu:latest + +RUN apt-get update \ + && apt-get install -y python3 python3-pip + +RUN apt-get install -y python3-venv + +WORKDIR /usr/share/ku-polls + +COPY . /usr/share/ku-polls + +# Create virtual environment, install requirement +RUN python3 -m venv venv \ + && ./venv/bin/pip install --upgrade pip \ + && ./venv/bin/pip install -r requirements.txt + +EXPOSE 8000 + +RUN chmod +x ./entrypoint.sh + +ENTRYPOINT ["./entrypoint.sh"] \ No newline at end of file diff --git a/Installation.md b/Installation.md index 3b63363..c5fb8a9 100644 --- a/Installation.md +++ b/Installation.md @@ -6,29 +6,35 @@ There are two ways to install and run this project. 2. Manually install the project with the Instruction in section 2. ### 1. Use `setup.py` or `setup.ps1`(for windows) to install the project. + 1. Install [Python 3.11 or later](https://www.python.org/downloads/) 2. Clone this repository and Run `setup.py` to install and run the project **Don't forget to answer the question from `setup.py` to setup the project** This method will autogenerate Environment Variable for you. + ```bash git clone https://github.com/Sosokker/ku-polls cd ku-polls python setup.py ``` + If you want to customize the environment variables, name of environment folder then run this command + ```bash python setup.py -custom ``` + or run `setup.ps1` (For Windows User) ----- +--- ### 2. Manually install the project with this instruction. 1. Install [Python 3.11 or later](https://www.python.org/downloads/) 2. Run these commands to clone this repository and enter the directory. + ```bash git clone https://github.com/Sosokker/ku-polls cd ku-polls @@ -39,30 +45,38 @@ cd ku-polls ```bash python -m pip install --user virtualenv ``` + 4. Create virtual environment with `venv` or `virtualenv`. + ```bash python -m virtualenv venv or python -m venv venv ``` + 5. Use `virtual environment` - Windows + ```bash .\venv\Scripts\activate ``` - Linux or MacOS + ```bash source venv/bin/activate ``` + 6. Install require module. + ``` pip install -r requirements.txt ``` 7. Create file call `.env` in `ku-polls` directory and add this line -**You can look at `sample.env` for more information and others environment variables to set.** + **You can look at `sample.env` for more information and others environment variables to set.** + ```bash SECRET_KEY=your_secret_key DEBUG = False @@ -72,16 +86,20 @@ EMAIL_HOST_PASSWORD = yourpassword ``` You can generate your own `your_secret_key` by this command + ```bash python manage.py shell -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())" ``` -or + +or + - [Django Secret Key Generator #1](https://djecrety.ir/) - [Django Secret Key Generator #2](https://miniwebtool.com/django-secret-key-generator/) **Don't forget to change `your_secret_key` to your secret key (without quote)** 8. Migrate database and load data into it then runserver. + ```bash python manage.py migrate python manage.py loaddata data/users.json @@ -90,13 +108,39 @@ python manage.py loaddata data/vote.json python manage.py runserver ``` -***NOTE*** +**_NOTE_** By Default `DEBUG=False` and Django will not load Static files for you so if you want to apply CSS run this. + ```bash python manage.py runserver --insecure ``` + or set `DEBUG=True` or do the [collectstatic](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/) Then connect to [http://127.0.0.1:8000/](http://127.0.0.1:8000/) or [localhost:8000/](localhost:8000/) + +### 3. Docker compose + +1. Clone the repository then create .env file in the following format + +```bash +git clone https://github.com/Sosokker/ku-polls +``` + +.env Format + +```file +SECRET_KEY=your_secret_key +DEBUG=True +ALLOWED_HOSTS=localhost +TIME_ZONE=Asia/Bangkok +EMAIL_HOST_PASSWORD=yourpassword +``` + +2. Run docker compose + +```bash +docker compose up +``` diff --git a/README.md b/README.md index 3f16273..dafaf1d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -## KU Polls: Online Survey Questions +## KU Polls: Online Survey Questions + [![Unit Tests](https://github.com/Sosokker/ku-polls/actions/workflows/django.yml/badge.svg)](https://github.com/Sosokker/ku-polls/actions/workflows/django.yml) An application to conduct online polls and surveys based @@ -10,6 +11,30 @@ Here is **[Install Instruction](Installation.md)**. ## How to Run +### Docker compose + +1. Clone the repository then create .env file in the following format + +```bash +git clone https://github.com/Sosokker/ku-polls +``` + +.env Format + +```file +SECRET_KEY=your_secret_key +DEBUG=True +ALLOWED_HOSTS=localhost +TIME_ZONE=Asia/Bangkok +EMAIL_HOST_PASSWORD=yourpassword +``` + +2. Run docker compose + +```bash +docker compose up +``` + ### Setup.py Method Using setup.py method, it will set `.env` for you and runserver automatically with [--insecure](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/#cmdoption-runserver-insecure). @@ -18,7 +43,9 @@ You can set `DEBUG=True` later and then runserver normally to load static files. Then, connect to [http://127.0.0.1:8000/](http://127.0.0.1:8000/) ### Manual Installation + After follow all of instruction. + - Set Virtual Environment and Use it. - Install Dependencies. - Migrate and then Load fixtures. @@ -44,25 +71,26 @@ Then, connect to [http://127.0.0.1:8000/](http://127.0.0.1:8000/) **NOTE** : If you have problems with port you can use runserver and specific your own port. For example, If you run this command then you need to connect to [http://127.0.0.1:7000/](http://127.0.0.1:7000/) + ```bash python manage.py runserver 7000 ``` ## Demo Superuser -|Username|Password| -|:--:|:--:| -|admin|ineedmorebullets| +| Username | Password | +| :------: | :--------------: | +| admin | ineedmorebullets | ## Demo User -|Username|Password| -|:--:|:--:| -|tester1|aa12345678aa| -|tester2|aa12345678aa| -|tester3|aa12345678aa| -|tester4|aa12345678aa| -|novote |aa12345678aa| +| Username | Password | +| :------: | :----------: | +| tester1 | aa12345678aa | +| tester2 | aa12345678aa | +| tester3 | aa12345678aa | +| tester4 | aa12345678aa | +| novote | aa12345678aa | ## Project Documents diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f5def73 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +services: + ku-polls: + build: . + env_file: + - .env + ports: + - "8000:8000" + volumes: + - ku-polls-data:/usr/share/ku-polls + +volumes: + ku-polls-data: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..a97caa8 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -e + + + +echo "Running migrations..." +./venv/bin/python3 manage.py migrate + +echo "Loading data for users..." +./venv/bin/python3 manage.py loaddata data/users.json + +echo "Loading data for polls..." +./venv/bin/python3 manage.py loaddata data/polls.json + +echo "Loading data for votes..." +./venv/bin/python3 manage.py loaddata data/vote.json + +echo "Starting Django development server..." +./venv/bin/python3 manage.py runserver 0.0.0.0:8000 --insecure \ No newline at end of file