Skip to content

Commit

Permalink
Migrate to docker-compose. (#950)
Browse files Browse the repository at this point in the history
Migrate to docker-compose.
  • Loading branch information
TyVik committed Oct 9, 2023
1 parent a5eb3c0 commit 93b02e9
Show file tree
Hide file tree
Showing 14 changed files with 1,812 additions and 1,093 deletions.
5 changes: 1 addition & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
steps:
- checkout
- setup_remote_docker:
version: 19.03.13
version: 20.10.23
- run: echo "$DOCKER_HUB_PASSWORD" | docker login --username $DOCKER_HUB_LOGIN --password-stdin
- run:
name: Build builder
Expand Down Expand Up @@ -105,9 +105,6 @@ jobs:
resource_class: small
steps:
- checkout
- run:
name: Wait for DB
command: dockerize -wait tcp:https://localhost:5432 -timeout 30s
- run:
name: Prepare environment
command: mkdir ~/repo/logs
Expand Down
28 changes: 12 additions & 16 deletions Dockerfile.backend
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
FROM python:3.8-slim-buster as backend-builder
FROM python:3.11-slim-bookworm as backend-builder

RUN mkdir /app && mkdir /app/logs
WORKDIR /app

RUN apt update \
&& apt install -y --no-install-recommends build-essential make gcc gdal-bin curl wget git \
&& wget -q https://github.com/jwilder/dockerize/releases/download/v0.6.1/dockerize-linux-amd64-v0.6.1.tar.gz && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-v0.6.1.tar.gz && rm dockerize-linux-amd64-v0.6.1.tar.gz \
&& apt install -y --no-install-recommends gdal-bin \
&& apt-get autoremove -y \
&& apt-get clean

ENV POETRY_HOME="/opt/poetry"
RUN curl -sSL https://install.python-poetry.org | python3 - \
&& find / -name "*.pyc" -exec rm -f {} \;
RUN pip install pdm \
&& find / -name "*.pyc" -exec rm -f {} \; \
&& rm -rf /root/.cache/

ADD pdm.lock pyproject.toml ./

ENV PATH="${PATH}:${POETRY_HOME}/bin"
ADD poetry.lock pyproject.toml /app/
RUN poetry config virtualenvs.create false \
&& poetry install --no-dev --no-interaction --no-ansi \
RUN pdm sync -g -p /app --no-self \
&& find / -name "*.pyc" -exec rm -f {} \; \
&& rm -rf /root/.cache/

FROM python:3.8-slim-buster as backend
FROM python:3.11-slim-bookworm as backend

RUN apt update \
&& apt install -y --no-install-recommends gdal-bin \
&& apt-get autoremove -y \
&& apt-get clean

COPY --from=backend-builder /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/
COPY --from=backend-builder /usr/local/lib/python3.11/site-packages/ /usr/local/lib/python3.11/site-packages/
COPY --from=backend-builder /usr/local/bin/ /usr/local/bin/

ARG GIT_REVISION
ENV GIT_REVISION=${GIT_REVISION:-unknown}

WORKDIR /app

ADD . /app/
ADD . .
CMD ["python3"]


FROM backend-builder as backend-tester

WORKDIR /app

RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi \
RUN pdm sync -g -p /app --no-self --dev \
&& find / -name "*.pyc" -exec rm -f {} \; \
&& rm -rf /root/.cache/
4 changes: 2 additions & 2 deletions deploy/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
upstream django {
server unix:/var/tmp/geopuzzle.sock;
server 127.0.0.1:8000;
}

upstream daphne {
server unix:/var/tmp/daphne0.sock;
server 127.0.0.1:8001;
}

server {
Expand Down
64 changes: 64 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
version: '3.8'
services:
redis:
image: redis:5-alpine
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 30s
timeout: 10s
retries: 3
networks:
- webnet

postgres:
image: circleci/postgres:12-postgis
environment:
POSTGRES_USER: geopuzzle
POSTGRES_PASSWORD: geopuzzle
POSTGRES_DB: geopuzzle
healthcheck:
test: ["CMD", "pg_isready -U geopuzzle"]
interval: 30s
timeout: 10s
retries: 3
volumes:
- ./pgdata:/var/lib/postgresql/data
networks:
- webnet

app:
build:
context: .
dockerfile: Dockerfile.backend
target: backend
restart: on-failure
environment:
DJANGO_SETTINGS_MODULE: mercator.settings.development
SECRET_KEY: 123

POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_DB: geopuzzle
POSTGRES_USER: geopuzzle
POSTGRES_PASSWORD: geopuzzle

REDIS_HOST: redis
ports:
- 8000:8000
depends_on:
- postgres
- redis
links:
- postgres
- redis
networks:
- webnet
volumes:
- .:/app
command: ./manage.py runserver 0.0.0.0:8000

networks:
webnet:

volumes:
pgdata:
43 changes: 14 additions & 29 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.7'
version: '3.8'
services:
redis:
image: redis:5-alpine
Expand Down Expand Up @@ -26,36 +26,21 @@ services:
networks:
- webnet

app:
build:
context: .
dockerfile: Dockerfile.backend
target: backend
wsgi:
image: geopuzzle
restart: on-failure
environment:
DJANGO_SETTINGS_MODULE: mercator.settings.development
SECRET_KEY: 123

POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_DB: geopuzzle
POSTGRES_USER: geopuzzle
POSTGRES_PASSWORD: geopuzzle
env_file:
- .env
network_mode: host
command: gunicorn mercator.wsgi:application -b 0.0.0.0:8000

REDIS_HOST: redis
ports:
- 8000:8000
depends_on:
- postgres
- redis
links:
- postgres
- redis
networks:
- webnet
volumes:
- .:/app
command: ./manage.py runserver 0.0.0.0:8000
asgi:
image: geopuzzle
restart: on-failure
env_file:
- .env
network_mode: host
command: daphne mercator.asgi:application -b 0.0.0.0 -p 8001

networks:
webnet:
Expand Down
4 changes: 0 additions & 4 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
import os
import sys

import dotenv


if __name__ == "__main__":
dotenv.read_dotenv()

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mercator.settings.development")
try:
from django.core.management import execute_from_command_line
Expand Down
14 changes: 9 additions & 5 deletions maps/consumer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Iterable, Tuple

from channels.db import database_sync_to_async
from asgiref.sync import sync_to_async
from django import forms
from django.utils.translation.trans_real import get_supported_language_variant, parse_accept_lang_header

Expand All @@ -11,25 +11,29 @@ class GameConsumer(ReduxConsumer):
PREFIX: str
form: forms.Form

@database_sync_to_async
@sync_to_async
def get_object(self, pk: int):
raise NotImplementedError()

@database_sync_to_async
@sync_to_async
def check_form(self, form: forms.Form) -> bool:
return form.is_valid()

@sync_to_async
def get_info(self, region, lang):
return region.full_info(lang)

async def _give_up(self, pk: int):
region = await self.get_object(pk)
result = region.full_info(self.scope['lang'])
result = await self.get_info(region, self.scope['lang'])
result['type'] = f'{self.PREFIX}_GIVEUP_DONE'
await self.send_json(result)

async def _check(self, pk: int, *args, **kwargs):
region = await self.get_object(pk)
form = self.form(area=region, **kwargs)
if await self.check_form(form):
result = region.full_info(self.scope['lang'])
result = await self.get_info(region, self.scope['lang'])
result['type'] = f'{self.PREFIX}_CHECK_SUCCESS'
await self.send_json(result)

Expand Down
3 changes: 0 additions & 3 deletions mercator/asgi.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import os
from pathlib import Path

import dotenv
import django
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application

from mercator.routing import ws_routes

dotenv.read_dotenv(Path(__file__).resolve().parent.parent / '.env')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mercator.settings.do")
django.setup()
Expand Down
20 changes: 2 additions & 18 deletions mercator/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,6 @@
}
},
"handlers": {
"file": {
"class": "logging.handlers.RotatingFileHandler",
'filename': LOG_DIR.joinpath('django.errors'),
"formatter": "verbose",
"level": "INFO",
"maxBytes": 10485760,
'backupCount': 10,
},
"commands": {
"class": "logging.handlers.RotatingFileHandler",
'filename': LOG_DIR.joinpath('commands.log'),
"formatter": "verbose",
"level": "DEBUG",
"maxBytes": 10485760,
'backupCount': 30,
},
'console': {
'class': 'logging.StreamHandler',
},
Expand All @@ -184,11 +168,11 @@
'loggers': {
'commands': {
'level': 'DEBUG',
'handlers': ['commands'],
'handlers': ['console'],
},
'wambachers': {
'level': 'DEBUG',
'handlers': ['commands', 'console'],
'handlers': ['console'],
},
'django.db.backends': {
'handlers': [],
Expand Down
5 changes: 0 additions & 5 deletions mercator/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
from pathlib import Path

import dotenv
from django.core.wsgi import get_wsgi_application


dotenv.read_dotenv(Path(__file__).resolve().parent.parent / '.env')

application = get_wsgi_application()
Loading

0 comments on commit 93b02e9

Please sign in to comment.