Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] improve containerized dev env - makefile and compose file #1198

Merged
merged 4 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.PHONY: init

init:
. ${NVM_DIR}/nvm.sh && nvm use
yarn install --dev
poetry env use 3.10
poetry install

run_db:
docker compose -f docker-compose-dev.yml up db -d

run_local: init run_db
export DATABASE_URL=postgresql://postgres:[email protected]:5432/pycontw2016
poetry run python src/manage.py runserver 0.0.0.0:8000

run_dev:
docker compose -f docker-compose-dev.yml up -d --build
docker compose -f docker-compose-dev.yml logs -f pycontw

stop_dev:
docker compose -f docker-compose-dev.yml stop

remove_dev:
docker compose -f docker-compose-dev.yml down

shell_dev:
docker compose -f docker-compose-dev.yml exec -it pycontw /bin/sh
7 changes: 0 additions & 7 deletions docker-compose-db.yml

This file was deleted.

16 changes: 10 additions & 6 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ services:
- POSTGRES_PASSWORD=secretpostgres
ports:
- ${COMPOSE_DB_PORT:-5432}:5432
healthcheck:
test: "pg_isready"
start_period: 5s
interval: 5s
retries: 10
pycontw:
container_name: pycontw_dev
build:
Expand All @@ -20,12 +25,11 @@ services:
- ./src:/usr/local/app/src
- ./logs:/usr/local/app/logs
ports:
- "8000:8000"
- "${COMPOSE_APP_PORT:-8000}:8000"
depends_on:
- db
db:
condition: service_healthy
environment:
- DJANGO_SUPERUSER_USERNAME=admin
- DJANGO_SUPERUSER_PASSWORD=1234
- [email protected]
- DATABASE_URL=postgres://postgres:secretpostgres@db:5432/pycontw2016
command: "python3 manage.py runserver 0.0.0.0:8000"
working_dir: /usr/local/app/src
command: tail -f /dev/null
18 changes: 8 additions & 10 deletions document/deploy_docker_dev.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
### Requirements
- Docker Engine 1.13.1+
- Docker Compose 1.10.0+
- Docker Compose v2

# 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).
mattwang44 marked this conversation as resolved.
Show resolved Hide resolved

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
make run_dev
```

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 (<code>ctrl+c</code> 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
mattwang44 marked this conversation as resolved.
Show resolved Hide resolved
make shell_dev
```

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
Expand All @@ -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
```
97 changes: 40 additions & 57 deletions document/deploy_local_env_dev.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,78 @@
## Getting Started
### Set up a Virtual Environment
## Local Environment Setup

#### 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
### Requirements
- Docker Engine 1.13.1+
- Docker Compose v2
- nvm
- poetry

### 0 - Database - Docker (Optional)
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)

Switch to version specified in `.nvmrc`:

nvm use

### Install Dependencies
make run_db

Use pip to install Python depedencies:

poetry install

Use Yarn to install Node 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`

yarn install --dev
If you plan to serve your own database server, you will need to modify `DATABASE_URL` in the next section.

### Set up Local Environment Variables for Database
### 1- Set up Local Environment Variables

Settings are stored in environment variables via [django-environ](http://django-environ.readthedocs.org/en/latest/). The quickest way to start is to copy `local.sample.env` into `local.env`:

cp src/pycontw2016/settings/local.sample.env src/pycontw2016/settings/local.env

Default is sqlite3, you can change to connect `postgres`. Copy `local.sample.env` and change its parameters to your personal setting.
Then edit the `SECRET_KEY` line in `local.env`, replacing `{{ secret_key }}` into any [Django Secret Key](http://www.miniwebtool.com/django-secret-key-generator/) value. An example:

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
```
#### 2 - Install environment (Python and Node Modules)
Init environment, including installing dependencies

```sql
# Replace "postgres" with your specified role.
CREATE DATABASE pycontw2016 with owner = postgres;
```
make init

After that, just run the migration.
Activate the python environment in your terminal

### Get Ready for Development
poetry shell

### 3 - Get Ready for Development

`cd` into the `src` directory:

cd src

#### Migrate the database:
#### 4 - Migrate the database:

python manage.py migrate

#### Create Super User
#### 5 - Create Super User

python manage.py createsuperuser

#### Compile localized translation
#### 6 - Compile localized translation

python manage.py compilemessages

Now you’re all set!

## Run the Development Server
#### 7 - Run the Development Server

python manage.py runserver

## Subsequent Development

Step 1 ~ 6 are (mostly) one-time thing, in subsequent development, you may want to
speed up the process of running your application. Use

make run_local

to quick launch your local server. Then develop your logic as it may.

If you missed out the steps in above section, just run `poetry shell` and `cd src` then execute those commands when needed.
28 changes: 0 additions & 28 deletions enter_dev_env.sh

This file was deleted.

5 changes: 4 additions & 1 deletion src/pycontw2016/settings/local.sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
# Note: No spaces around '=' sign and no quotes for righthand values.

DEBUG=True
# You have to set the DATABASE_URL if you are dealing with your own database server other than one we provide in `docker-compose-dev.yml`,
# otherwise you may leave the DATABASE_URL variable unset.
# DATABASE_URL=

# syntax: DATABASE_URL=postgres://username:[email protected]:5432/database
DATABASE_URL=sqlite:///db.sqlite3
# Command to create a new secret key:
# $ python -c 'import random; import string; print("".join([random.SystemRandom().choice(string.digits + string.ascii_letters + string.punctuation) for i in range(100)]))'
SECRET_KEY={{ secret_key }}
Loading