Skip to content

Commit

Permalink
feat(makefile): refactor docker and compose file and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
iknowright committed Jun 7, 2024
1 parent 7d7c606 commit 9f4e7fb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 93 deletions.
5 changes: 3 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 All @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: init

init:
. ${NVM_DIR}/nvm.sh && nvm use
yarn install --dev
poetry env use 3.10
poetry install
16 changes: 11 additions & 5 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- [email protected]
- 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
16 changes: 7 additions & 9 deletions document/deploy_docker_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<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
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
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
```
63 changes: 14 additions & 49 deletions document/deploy_local_env_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
28 changes: 0 additions & 28 deletions enter_dev_env.sh

This file was deleted.

0 comments on commit 9f4e7fb

Please sign in to comment.