-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from unb-mds/feat/docker
docker(api): configurar o docker para ambiente dev da API
- Loading branch information
Showing
22 changed files
with
179 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,23 @@ | ||
# Django Configuration | ||
# Generated with: | ||
# Configuração Django | ||
# Gerar secret key: | ||
# python3 -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())' | ||
DJANGO_SECRET_KEY="your_secret_key" | ||
DJANGO_SECRET_KEY="your_secret_key" | ||
SETTINGS_FILE_PATH="core.settings.base" | ||
|
||
# Banco de Dados | ||
DB_ENGINE="django.db.backends.postgresql" | ||
DB_NAME="postgres" | ||
DB_USERNAME="suagradeunb" | ||
DB_PASSWORD="suagradeunb" | ||
DB_HOSTNAME=db | ||
DB_PORT=5432 | ||
|
||
# PostgreSQL | ||
POSTGRES_DB="postgres" | ||
POSTGRES_USER="suagradeunb" | ||
POSTGRES_PASSWORD="suagradeunb" | ||
|
||
|
||
# Credenciais de acesso ao admin | ||
ADMIN_NAME="admin" | ||
ADMIN_PASS="admin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ApiConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'api' |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.contrib.auth.models import User | ||
from decouple import config | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def handle(self, *args, **options): | ||
username = config("ADMIN_NAME") | ||
if not len(User.objects.all().filter(username=username)): | ||
password = config("ADMIN_PASS") | ||
print(f'Conta do usuário {username} será criada!') | ||
admin = User.objects.create_superuser( | ||
username=username, password=password) | ||
admin.is_active = True | ||
admin.is_staff = True | ||
admin.save() | ||
else: | ||
print('Conta de administrador já existe!') |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.shortcuts import render | ||
|
||
# Create your views here. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from .base import * | ||
|
||
# Database | ||
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': BASE_DIR / 'db.sqlite3', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .base import * | ||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = False | ||
|
||
ALLOWED_HOSTS = ["0.0.0.0:8000", "localhost:8000", "127.0.0.1:8000"] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
services: | ||
db: | ||
image: postgres:16 | ||
volumes: | ||
- postgres_data:/var/lib/postgresql/data/ | ||
env_file: | ||
- ./api/.env | ||
|
||
api: | ||
restart: unless-stopped | ||
build: | ||
context: . | ||
dockerfile: ./docker/Dockerfile | ||
command: python3 manage.py runserver 0.0.0.0:8000 | ||
volumes: | ||
- .:/code/ | ||
env_file: | ||
- ./api/.env | ||
ports: | ||
- 8000:8000 | ||
depends_on: | ||
- db | ||
|
||
volumes: | ||
postgres_data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
FROM python:3.11.5-alpine3.18 | ||
|
||
# definir o diretório de trabalho | ||
WORKDIR /usr/src/app | ||
|
||
# definir variáveis de ambiente | ||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
|
||
# instalar dependências do poastgres | ||
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev | ||
|
||
# copiar os requisitos python | ||
COPY ./requirements.txt /usr/src/app/requirements.txt | ||
|
||
# instalação das dependências | ||
RUN pip install --upgrade pip && \ | ||
pip install -r /usr/src/app/requirements.txt | ||
|
||
# copiar entrypoint.sh e dar permissão de execução | ||
COPY ./docker/entrypoint.sh /usr/src/app/entrypoint.sh | ||
RUN chmod +x /usr/src/app/entrypoint.sh | ||
|
||
# copiar o projeto | ||
COPY ./api /usr/src/app/ | ||
|
||
# executar o arquivo de inicialização | ||
ENTRYPOINT ["/usr/src/app/entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/sh | ||
|
||
echo 'Esperando o PostgreSQL iniciar...' | ||
|
||
while ! nc -z $DB_HOSTNAME $DB_PORT; do | ||
sleep 0.1 | ||
done | ||
|
||
echo 'PostgreSQL iniciado' | ||
|
||
echo 'Migrando banco de dados...' | ||
python3 manage.py migrate | ||
|
||
echo 'Criando usuário admin...' | ||
python3 manage.py initadmin | ||
|
||
echo 'Coletando arquivos estáticos...' | ||
python3 manage.py collectstatic --no-input | ||
|
||
exec "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters