Skip to content

Commit 3947a78

Browse files
authored
chore: wip reorganize docker-compose files (#15)
* chore: wip reorganize docker-compose files
1 parent 078661d commit 3947a78

File tree

8 files changed

+110
-62
lines changed

8 files changed

+110
-62
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ jobs:
2020
- name: Build docker images
2121
run: docker compose build
2222

23+
- name: Run containers
24+
run: docker compose up -d
25+
2326
- name: Run PyTest
24-
run: docker compose run test
27+
run: docker exec mapdb-django-dev pytest

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Follow this guide to set up the api locally for development.
2424
> [!note]
2525
> By default, we are using Docker for the sake of simplicity. If you want to run the API natively, you can check out the section [Native Guide](#native-guide).
2626
27-
- Docker with compose
27+
- Docker
2828
- An IDE (PyCharm strongly recommended)
2929

3030
### Set up environment variables
@@ -41,11 +41,47 @@ You can copy `example.env` to `.env` and fill in the required values.
4141
Run the following command to start the API:
4242

4343
```bash
44-
docker compose up nginx-server -d
44+
docker compose up -d
4545
```
4646

4747
> Now you can access the API at `http://localhost`
4848
49+
### Running the tests
50+
51+
Once the api is started you can run the tests using the following command.
52+
53+
Run tests
54+
```
55+
docker exec -it mapdb-django-dev pytest
56+
```
57+
58+
## Production
59+
60+
> This section is still a WIP...
61+
62+
Follow this guide to set up the app for production.
63+
64+
### Prerequisites
65+
66+
- Docker
67+
68+
### Set up environment variables
69+
70+
You can copy `example.env` to `.env` and fill in the required values.
71+
72+
- Set `POSTGRES_PASSWORD` to a secure password.
73+
- Set `SECRET_KEY` to a secure secret key. You can use `from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())` in a python shell to generate it.
74+
- You MUST set `DEBUG` to `0`
75+
- You MUST set `RUN_ENVIRONMENT` to `production`
76+
77+
### Running the API
78+
79+
Run the following command to start the API:
80+
81+
```bash
82+
docker compose -f docker-compose.prod.yml up -d
83+
```
84+
4985
## Native Guide
5086

5187
You should use docker compose, but here are the native OS instructions in case you don't want to.

docker-compose.prod.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
services:
2+
db:
3+
container_name: mapdb-postgres
4+
image: postgres
5+
volumes:
6+
- ${POSTGRES_DATA_DIR}:/var/lib/postgresql/data
7+
environment:
8+
- POSTGRES_DB=${POSTGRES_DB}
9+
- POSTGRES_USER=${POSTGRES_USER}
10+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
11+
env_file:
12+
- .env
13+
ports:
14+
- "127.0.0.1:${POSTGRES_PORT}:${POSTGRES_PORT}"
15+
command: -p ${POSTGRES_PORT}
16+
17+
django:
18+
container_name: mapdb-django
19+
build:
20+
context: .
21+
dockerfile: docker/Dockerfile
22+
target: prod
23+
volumes:
24+
- ${HOST_MEDIA_ROOT}:/data/cncnet_silo # django will save user-uploaded files here. MEDIA_ROOT
25+
- ${HOST_STATIC_ROOT}:/data/cncnet_static # django will gather static files here. STATIC_ROOT
26+
ports:
27+
- "8000:8000"
28+
env_file:
29+
- .env
30+
depends_on:
31+
- db
32+
33+
nginx-server:
34+
# nginx proxies requests to django via gunicorn.
35+
container_name: mapdb-nginx
36+
image: nginx:latest
37+
volumes:
38+
- ./nginx.conf:/etc/nginx/nginx.conf:ro
39+
- ${HOST_STATIC_ROOT}:/usr/share/nginx/html/static # website static assets.
40+
- ${HOST_MEDIA_ROOT}:/usr/share/nginx/html/silo # website user-uploaded files.
41+
ports:
42+
- "80:80"
43+
depends_on:
44+
- django

docker-compose.yml

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
services:
22
db:
3+
container_name: mapdb-postgres-dev
34
image: postgres
45
volumes:
56
- ${POSTGRES_DATA_DIR}:/var/lib/postgresql/data
@@ -14,8 +15,11 @@ services:
1415
command: -p ${POSTGRES_PORT}
1516

1617
django:
17-
# Won't serve files on its own. Launch nginx-server to run the whoe app.
18-
build: .
18+
container_name: mapdb-django-dev
19+
build:
20+
context: .
21+
dockerfile: docker/Dockerfile
22+
target: dev
1923
volumes:
2024
- .:/cncnet-map-api
2125
- ${HOST_MEDIA_ROOT}:/data/cncnet_silo # django will save user-uploaded files here. MEDIA_ROOT
@@ -28,46 +32,14 @@ services:
2832
- db
2933

3034
nginx-server:
31-
# This is the prod server service.
32-
# `docker compose up nginx -d` will run the whole app.
3335
# nginx proxies requests to django via gunicorn.
36+
container_name: mapdb-nginx-dev
3437
image: nginx:latest
3538
volumes:
36-
- ./nginx.conf:/etc/nginx/nginx.conf:ro
39+
- ./docker/nginx.conf:/etc/nginx/nginx.conf:ro
3740
- ${HOST_STATIC_ROOT}:/usr/share/nginx/html/static # website static assets.
3841
- ${HOST_MEDIA_ROOT}:/usr/share/nginx/html/silo # website user-uploaded files.
3942
ports:
4043
- "80:80"
4144
depends_on:
4245
- django
43-
44-
test:
45-
build:
46-
context: ./
47-
dockerfile: test.DockerFile
48-
volumes:
49-
- .:/cncnet-map-api
50-
env_file:
51-
- .env
52-
environment:
53-
POSTGRES_TEST_HOST: db # Necessary to connect to docker db. Overrides the .env setting.
54-
depends_on:
55-
- db
56-
command:
57-
- pytest
58-
59-
windows-dev:
60-
# Use this for windows. The LZO libraries are annoying to deal with without using docker.
61-
# Chairman Bing of Massivesoft strikes again.
62-
build:
63-
context: ./
64-
dockerfile: test.DockerFile
65-
volumes:
66-
- .:/cncnet-map-api
67-
- ./data/tmp:/tmp/pytest-of-root # For inspecting files during pytests
68-
env_file:
69-
- .env
70-
environment:
71-
POSTGRES_TEST_HOST: db # Necessary to connect to docker db. Overrides the .env setting.
72-
depends_on:
73-
- db

Dockerfile renamed to docker/Dockerfile

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.12-bookworm as base
1+
FROM python:3.12-bookworm AS base
22

33
ENV PYTHONDONTWRITEBYTECODE=1
44
ENV PYTHONUNBUFFERED=1
@@ -12,11 +12,22 @@ COPY requirements.txt /cncnet-map-api
1212
COPY requirements-dev.txt /cncnet-map-api
1313
COPY web_entry_point.sh /cncnet-map-api
1414

15-
RUN apt-get update && apt-get install -y liblzo2-dev # Compression library used by westwood.
16-
RUN apt-get install libmagic1 # File type checking.
15+
# liblzo2 is a compression library used by westwood.
16+
# libmagic1 is used for detecting file mime types by analyzing the file contents.
17+
RUN apt-get update && apt-get install -y liblzo2-dev libmagic1
1718
RUN pip install --upgrade pip
19+
RUN chmod +x /cncnet-map-api/web_entry_point.sh
20+
21+
FROM base AS dev
1822
# The cflags are needed to build the lzo library on Apple silicon.
1923
RUN CFLAGS=-I$(brew --prefix)/include LDFLAGS=-L$(brew --prefix)/lib pip install -r ./requirements-dev.txt
24+
ENTRYPOINT "/cncnet-map-api/web_entry_point.sh"
25+
26+
FROM base AS prod
27+
# The cflags are needed to build the lzo library on Apple silicon.
28+
RUN CFLAGS=-I$(brew --prefix)/include LDFLAGS=-L$(brew --prefix)/lib pip install -r ./requirements.txt
29+
30+
COPY . /cncnet-map-api
2031

21-
RUN chmod +x /cncnet-map-api/web_entry_point.sh
2232
ENTRYPOINT "/cncnet-map-api/web_entry_point.sh"
33+
File renamed without changes.

example.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ POSTGRES_DATA_DIR=./data/db/
1414
# In docker the host will always be DB, but when run locally it will probably be local host.
1515
# The reason this is separate from production host is because putting PyCharm breakpoints in docker has issues
1616
# and pytests often need to be run locally to have breakpoints work.
17-
POSTGRES_TEST_HOST=localhost
17+
POSTGRES_TEST_HOST=db
1818

1919
# Port for postgres
2020
POSTGRES_PORT=5432

test.DockerFile

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)