Skip to content

Commit

Permalink
Merge pull request #92 from bocabitlabs/develop
Browse files Browse the repository at this point in the history
Use Docker Compose for the deployment
  • Loading branch information
renefs authored Aug 1, 2023
2 parents 5980a1e + da42a5c commit 5d2146e
Show file tree
Hide file tree
Showing 36 changed files with 577 additions and 809 deletions.
8 changes: 7 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ pip-log.txt
.tox
.coverage
.coverage.*
.coveragerc
.cache
nosetests.xml
coverage.xml
*.cover
*.log
*mypy.ini
.mypy_cache
.pytest_cache
.pylintrc_old
.hypothesis
node_modules
client/node_modules
client/build
client/dist
client/dist
.venv
.git
.gitingore
80 changes: 80 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
PYTHONPATH=${PYTHONPATH}:${PWD}
# NGINX port where the application will listen for connections
# Default: 8080 (integer)
NGINX_PORT=8080
# List of allowed hosts to access the application
# Default: 0.0.0.0,localhost
ALLOWED_HOSTS=0.0.0.0,localhost,127.0.0.1
# Origins allowed by CORS to access the application API
# Default: http://localhost:3000,http://127.0.0.1:3000
CORS_ALLOWED_ORIGINS = http://localhost:3000,http://127.0.0.1:3000
# Debug mode enabled
# Default: true
DEBUG=true
# Log level
# Default: "DEBUG" (Other options: "INFO", "WARNING", "ERROR", "CRITICAL")
LOG_LEVEL=DEBUG
# Whether or not to log to file
# Default: false
LOG_TO_FILE=false
# Active handlers for the logger
# Default: console. Other options: console,file
LOGGER_HANDLERS=console
# Path to the log files
# Default: "". Other options: "/usr/src/logs/"
LOGS_ROOT=/your/path/to/buho-stocks/data/
# Type of database
# VALUES: sqlite | mysql | postgresql
DB_TYPE=sqlite
# MySQL root user password
# Default: ex4mpl3 (string)
DB_ROOT_PASSWORD=ex4mpl3
# Database name
# Default: buho_stocks (string)
DB_NAME=buho_stocks
# Database user
# Default: root (string)
DB_USER=root
# Database password
# Default: example (string)
DB_PASSWORD=example
# Database hostname
# Default: db (string)
DB_HOSTNAME=db
# Database port
# Default: 3306 (integer)
DB_PORT=3306
# Path of the database
# Default: /your/path/to/buho-stocks/data/db.sqlite3
DATABASE_SQLITE_PATH=/your/path/to/buho-stocks/data/db.sqlite3
# Secret key for the application. Can be generated by os.urandom(20).
# It is generated automatically when the container is created for the first time
SECRET_KEY=REPLACE_SECRET_KEY
# List of allowed hosts to access the application
# Default: 0.0.0.0,localhost
ALLOWED_HOSTS=0.0.0.0,localhost,127.0.0.1
# Path to the media files uploaded by the users
# Default: /your/path/to/buho-stocks/media/
MEDIA_ROOT=/your/path/to/buho-stocks/media/
# Timezone of the application
# Default: UTC
TIME_ZONE=UTC
# Enable or disable Sentry
# Default: False
ENABLE_SENTRY=false
# Sentry DSN (Data Source Name) in order to send your errors to Sentry
# Default: "" (string)
SENTRY_DSN=https://[email protected]/1727901
# DO NOT EDIT FROM HERE. THESE VARIABLES ARE USED INTERNALLY BY THE APPLICATION
# The following variables are populated from other variables
#
MARIADB_DATABASE=${DB_NAME}
#
#
MARIADB_USER=${DB_USER}
#
#
MARIADB_PASSWORD=${DB_PASSWORD}
#
#
MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
49 changes: 49 additions & 0 deletions .github/workflows/docker-build-publish.yml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Docker Build & Publish
on:
push:
branches:
- main
release:
types:
- created

jobs:
build-and-publish:
name: Build and Publish Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and Push backend Docker Image
uses: docker/build-push-action@v2
with:
context: . # Path to your Dockerfile and other build context files
push: true
tags: |
ghcr.io/${{ github.repository }}:latest
ghcr.io/${{ github.repository }}:${{ github.head_ref || github.ref_name }}
labels: |
org.opencontainers.image.source=https://github.com/bocabitlabs/${{ github.repository }}
- name: Build and Push client Docker Image
uses: docker/build-push-action@v2
with:
context: . # Path to your Dockerfile and other build context files
file: docker.client.Dockerfile
push: true
tags: |
ghcr.io/${{ github.repository }}-client:latest
ghcr.io/${{ github.repository }}-client:${{ github.head_ref || github.ref_name }}
labels: |
org.opencontainers.image.source=https://github.com/bocabitlabs/${{ github.repository }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,8 @@ mysql.conf
media
client/src/config.js
client/.env.production
volumes/mariadb_data
volumes/static
volumes/media
volumes/logs
.env.prod
12 changes: 9 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"request": "launch",
"program": "${workspaceFolder}/backend/manage.py",
"args": ["runserver", "0.0.0.0:8001"],
"django": true
"django": true,
"env": {
"DJANGO_ENV": "development"
}
},

{
Expand All @@ -29,14 +32,17 @@
"runtimeExecutable": "yarn",
"runtimeArgs": ["start"],
"cwd": "${workspaceRoot}/client",
"console": "integratedTerminal"
"console": "integratedTerminal",
},
{
"name": "Python: Test",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/backend/manage.py",
"args": ["test"]
"args": ["test"],
"env": {
"DJANGO_ENV": "test"
}
}
]
}
4 changes: 0 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
"python.analysis.completeFunctionParens": true,
"python.analysis.autoImportCompletions": true,
"python.analysis.useLibraryCodeForTypes": true,
//"python.analysis.inlayHints.variableTypes": true,
//"python.analysis.inlayHints.functionReturnTypes": true,
//"python.analysis.inlayHints.pytestParameters": true,
"python.analysis.ignore": ["**/site-packages/**/*.py", "**/migrations/*.py"],
"[python]": {
"editor.codeActionsOnSave": {
Expand All @@ -43,5 +40,4 @@
"[jinja-html]": {
"editor.defaultFormatter": "monosans.djlint"
},
"python.pythonPath": "python"
}
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ All notable changes to this project will be documented in this file.
### Removed
### Security

## [0.200.0] - 2023-07-30

# Improved

- Now using Docker Compose for the deployment as default.
- Using python-decouple to have different environments using .env files.

## [0.100.7] - 2023-06-04
### Fix
- Backend: Fix the company year stats update (PUT)
Expand Down
65 changes: 20 additions & 45 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
FROM tiangolo/uwsgi-nginx:python3.10
FROM python:3.10-slim

RUN apt-get update
RUN apt-get -y install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get -y install nodejs

VOLUME /usr/src/data
VOLUME /usr/src/media

RUN npm install --global yarn

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONPATH "${PYTHONPATH}:/usr/src"
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONPATH "${PYTHONPATH}:/usr/src"
ENV LISTEN_PORT 34800
ENV UWSGI_INI /usr/src/uwsgi.ini
# The following environment variables are used by Poetry to install dependencies

ENV POETRY_VERSION 1.5.0
ENV POETRY_HOME /opt/poetry
ENV POETRY_VIRTUALENVS_IN_PROJECT true
ENV POETRY_CACHE_DIR ${WORKING_DIR}/.cache
ENV VIRTUAL_ENVIRONMENT_PATH ${WORKING_DIR}/.venv
ENV POETRY_CACHE_DIR ${WORKDIR}/.cache
ENV VIRTUAL_ENVIRONMENT_PATH ${WORKDIR}/.venv

LABEL org.opencontainers.image.authors='[email protected]' \
org.opencontainers.image.url='https://github.com/bocabitlabs/buho-stocks/pkgs/container/buho-stocks' \
org.opencontainers.image.documentation='https://bocabitlabs.github.io/buho-stocks/' \
org.opencontainers.image.source="https://github.com/bocabitlabs/buho-stocks" \
org.opencontainers.image.vendor='Bocabitlabs (Rene Fernandez)' \
org.opencontainers.image.licenses='GPL-3.0-or-later'

COPY ./uwsgi.ini /usr/src/uwsgi.ini
COPY ./nginx-app.conf /etc/nginx/conf.d/custom.conf
COPY ./prestart.sh /app/prestart.sh
# Required to have netcat-openbsd
RUN apt-get update
RUN apt-get install default-libmysqlclient-dev netcat-openbsd gcc -y

# Install Poetry and dependencies
COPY pyproject.toml ./
Expand All @@ -38,29 +31,11 @@ COPY poetry.lock ./
# Using Poetry to install dependencies without requiring the project main files to be present
RUN pip install poetry==${POETRY_VERSION} && poetry install --only main --no-root --no-directory

WORKDIR /usr/src/client
COPY ./client/package.json /usr/src/client/package.json
COPY ./client/yarn.lock /usr/src/client/yarn.lock
COPY ./client/yarn.lock /usr/src/client/vite.config.ts
RUN yarn --production --pure-lockfile
COPY ./client /usr/src/client

RUN yarn build
RUN rm -rf /usr/src/client/node_modules/
RUN mkdir /app/frontend/
RUN mv ./dist/* /app/frontend/

WORKDIR /usr/src/app
COPY ./backend /usr/src/app

RUN mkdir /usr/src/media/
RUN mkdir /usr/src/logs/
# copy project
COPY ./backend/config/config.sample.py /usr/src/app/config/config.py

EXPOSE 34800
COPY ./backend $WORKDIR
COPY ./etc /usr/src/etc

RUN poetry run python manage.py collectstatic
RUN sed -i -e "s/REPLACE_SECRET_KEY/$(od -x /dev/urandom | head -1 | awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}')/g" /usr/src/app/config/config.py
RUN chmod +x /usr/src/etc/entrypoint.sh

EXPOSE 8000

ENTRYPOINT ["/usr/src/etc/entrypoint.sh"]
40 changes: 0 additions & 40 deletions Dockerfile.dev

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 5d2146e

Please sign in to comment.