diff --git a/Dockerfile b/Dockerfile index 56a7bab4a..0ccf63da1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ ENV APP_DIR /usr/local/app # Infrastructure tools # gettext is used for django to compile .po to .mo files. RUN apt-get update -RUN apt-get upgrade -y +RUN apt-get upgrade -y RUN apt-get install -y \ libpq-dev \ gcc \ @@ -30,7 +30,7 @@ RUN apt-get install -y \ libxml2-dev \ libxslt-dev -ENV PYTHONUNBUFFERED=1 \ +ENV PYTHONUNBUFFERED=1 \ PIP_DISABLE_PIP_VERSION_CHECK=on \ PIP_DEFAULT_TIMEOUT=100 \ POETRY_VIRTUALENVS_IN_PROJECT=true @@ -55,6 +55,7 @@ RUN poetry install --no-root --only dev COPY --from=node_deps /node_modules $APP_DIR/node_modules COPY --from=node_deps /usr/local/bin/node /usr/local/bin/node +RUN apt-get install -y postgresql-client FROM python_deps as build RUN mkdir -p "$APP_DIR" "$APP_DIR/src/assets" "$APP_DIR/src/media" diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..52c86e885 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: init + +init: + . ${NVM_DIR}/nvm.sh && nvm use + yarn install --dev + poetry env use 3.10 + poetry install diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index 807b62ca8..03325ec65 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -20,12 +20,18 @@ services: - ./src:/usr/local/app/src - ./logs:/usr/local/app/logs ports: - - "8000:8000" + - "${COMPOSE_APP_PORT:-8000}:8000" depends_on: - db environment: - - DJANGO_SUPERUSER_USERNAME=admin - - DJANGO_SUPERUSER_PASSWORD=1234 - - DJANGO_SUPERUSER_EMAIL=admin@pycon.tw + - DATABASE_URL=postgres://postgres:secretpostgres@db:5432/pycontw2016 + command: + - bash + - -c + - | + until pg_isready -h db; do + echo 'Waiting for PostgreSQL to be available...' + sleep 1 + done + python3 manage.py runserver 0.0.0.0:8000 working_dir: /usr/local/app/src - command: tail -f /dev/null diff --git a/document/deploy_docker_dev.md b/document/deploy_docker_dev.md index e1e041d4e..f87a03aef 100644 --- a/document/deploy_docker_dev.md +++ b/document/deploy_docker_dev.md @@ -4,19 +4,20 @@ # Containerized Development Environment -1. Edit the `DATABASE_URL` in `src/pycontw2016/settings/local.env`(Copy from [`local.sample.env`](../src/pycontw2016/settings/local.sample.env)). Use the Postgres username, password, database name, and port configured in [`./docker-compose-dev.yml`](../docker-compose-dev.yml). - +1. Simply run the following command to start containerized services, this will run both the database and django service for you: ``` - DATABASE_URL=postgres://postgres:secretpostgres@db:5432/pycontw2016 + docker compose -f docker-compose-dev.yml up -d ``` -2. Simply run the following command to install all dependencies, activate a containerized Postgres server, and enter into a poetry shell inside the application container (ctrl+c to quit). +2. If the services are up and running in the first time, you may need to run the following in `pycontw` service in docker shell. + +To get into the docker shell for `pycontw` ``` - ./enter_dev_env.sh + docker compose -f docker-compose-dev.yml exec -it pycontw /bin/sh ``` -3. In the shell, you can run any commands as if you are in a local development environment. Here are some common Django commands: +In the shell, you can run any commands as if you are in a local development environment. Here are some common Django commands: ```sh # make migrations @@ -33,7 +34,4 @@ # compile translations python manage.py compilemessages - - # run the dev server (you can access the site at http://localhost:8000/) - python manage.py runserver 0.0.0.0:8000 ``` diff --git a/document/deploy_local_env_dev.md b/document/deploy_local_env_dev.md index 0213dfe86..c1753d890 100644 --- a/document/deploy_local_env_dev.md +++ b/document/deploy_local_env_dev.md @@ -2,48 +2,27 @@ ### Set up a Virtual Environment #### Database - Docker (Optional) - -Write database password in .env: - - POSTGRES_PASSWORD=somepassword - -Define .env location in docker-compose-db.yml under the corresponding database service: - - services: - db: - image: postgres:11-alpine - env_file: - - .env - Create and start the database for development: - docker-compose -f docker-compose-db.yml up - -#### Python - Poetry - -Create your virtual environment: - - poetry env use 3.10 - -And enable it: - - poetry shell - -#### Node.js - [nvm](https://github.com/creationix/nvm) + docker-compose -f docker-compose-dev.yml db up -d -Switch to version specified in `.nvmrc`: - - nvm use - -### Install Dependencies +This will create a postgres database with the following existed: +``` +database_name=pycontw2016 +username=postgres +password=secretpostgres +port=5432 +``` +And the database connection url will be `postgres://postgres:secretpostgres@localhost:5432/pycontw2016` -Use pip to install Python depedencies: +#### Install environment using Makefile (Python and Node Modules) +Init environment, including installing dependencies - poetry install + make init -Use Yarn to install Node dependencies: +Activate the python environment in your terminal - yarn install --dev + poetry shell ### Set up Local Environment Variables for Database @@ -56,20 +35,6 @@ Then edit the `SECRET_KEY` line in `local.env`, replacing `{{ secret_key }}` int SECRET_KEY=twvg)o_=u&@6^*cbi9nfswwh=(&hd$bhxh9iq&h-kn-pff0&&3 -If you’re using a database for the first time, create a database named `pycontw2016` owned by the database user specified in the env file: - -> Enter pycontw_db_1 container -```cmd -docker exec -it pycontw_db_1 psql -U postgres -``` - -```sql -# Replace "postgres" with your specified role. -CREATE DATABASE pycontw2016 with owner = postgres; -``` - -After that, just run the migration. - ### Get Ready for Development `cd` into the `src` directory: diff --git a/enter_dev_env.sh b/enter_dev_env.sh deleted file mode 100755 index dd621f3ae..000000000 --- a/enter_dev_env.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash -xe -CONTAINER="${USER}_pycontw_vm" -COMPOSE_FILE="./docker-compose-dev.yml" - -# test if the container is running -HASH=`docker ps -q -f name=$CONTAINER` - -# test if the container is stopped -HASH_STOPPED=`docker ps -qa -f name=$CONTAINER` - -if [[ $(uname -m) == 'arm64' ]]; then - export DOCKER_DEFAULT_PLATFORM=linux/amd64 -fi - -if [ -n "$HASH" ];then - echo "found existing running container $CONTAINER, proceeding to exec another shell" - docker-compose -f $COMPOSE_FILE restart - docker exec -w /app/src -it $HASH bash -c "SHELL=bash poetry shell" -elif [ -n "$HASH_STOPPED" ];then - echo "found existing stopped container $CONTAINER, starting" - docker-compose -f $COMPOSE_FILE restart - docker start --attach -i $HASH_STOPPED -else - echo "existing container not found, creating a new one, named $CONTAINER" - docker-compose -f $COMPOSE_FILE pull - docker-compose -f $COMPOSE_FILE run -p 8000:8000 --name=$CONTAINER pycontw bash -c "SHELL=bash poetry shell" -fi -echo "see you, use 'docker rm $CONTAINER' to kill the dev container or 'docker-compose -f $COMPOSE_FILE down' to kill both the postgres and the dev container if you want a fresh env next time"