Skip to content

Commit 3ab75d3

Browse files
authored
Merge pull request #28 from vemonitor/app
Dockerize full stack app
2 parents 1fede67 + 9326067 commit 3ab75d3

28 files changed

+637
-139
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sh text eol=lf

.gitignore

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ celerybeat.pid
123123
*.sage.py
124124

125125
# Environments
126-
.env
126+
.env*
127127
.venv
128128
env/
129129
venv/
@@ -162,8 +162,10 @@ cython_debug/
162162
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
163163
#.idea/
164164

165-
dev/
165+
/dev/*
166166
.vscode/settings.json
167167

168168
*/node_modules/*
169-
records
169+
records
170+
171+
/backend/alembic/versions/*.py

backend/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Dockerfile
2+
/alembic/versions/*
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
# project envs
1+
# This file is used to set up the environment variables for the backend
2+
# This file should be copied to .env and edited with the correct values
3+
24
# Domain
35
# This would be set to the production domain with an env var on deployment
46
# used by Traefik to transmit traffic and aqcuire TLS certificates
57
DOMAIN="localhost"
68
# To test the local Traefik config
79
# DOMAIN=localhost.tiangolo.com
810

9-
# Used by the backend to generate links in emails to the frontend
10-
FRONTEND_HOST="http://localhost:5173"
11-
# In staging and production, set this env var to the frontend host, e.g.
12-
# FRONTEND_HOST=https://dashboard.example.com
13-
1411
# Environment: local, staging, production
1512
ENVIRONMENT="local"
1613

14+
API_V1_STR="/api/v1"
1715
PROJECT_NAME="EmonTools"
1816
STACK_NAME="full-stack-emontools-project"
1917

@@ -22,24 +20,19 @@ DATA_BASE_PATH="/opt/emon_tools/data"
2220
STATIC_BASE_PATH="/opt/emon_tools/static"
2321

2422
# Backend
23+
# Used by the backend to generate links in emails to the frontend
24+
FRONTEND_HOST="http://localhost:5173"
25+
# In staging and production, set this env var to the frontend host, e.g.
26+
# FRONTEND_HOST=https://dashboard.example.com
2527
BACKEND_CORS_ORIGINS="http://localhost,http://localhost:5173,https://localhost,https://localhost:5173"
26-
SECRET_KEY=
27-
FIRST_SUPERUSER=admin@example.com
28+
29+
SECRET_KEY=changethis
30+
FIRST_SUPERUSER=changethis
2831
FIRST_SUPERUSER_PASSWORD=changethis
2932

3033
# Mysql
31-
MYSQL_SERVER="127.0.0.1"
34+
MYSQL_HOST="127.0.0.1"
3235
MYSQL_PORT=3306
33-
MYSQL_DB="app"
36+
MYSQL_DB="emontools"
3437
MYSQL_USER="emontools"
3538
MYSQL_PASSWORD=changethis
36-
37-
# EmonCms envs
38-
# local emoncms instance
39-
EMONCMS_URL="http://127.0.0.1:8080"
40-
# emoncms apikey
41-
API_KEY=changethis
42-
# local phpfina paths
43-
EMON_FINA_PATH="/var/lib/phpfina"
44-
# phpfina archives directory
45-
ARCHIVE_FINA_PATH="./datas"

backend/.example_env_docker

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This file is used to set up the environment variables for the backend
2+
# This file should be copied to .env.docker and edited with the correct values
3+
4+
# Domain
5+
# This would be set to the production domain with an env var on deployment
6+
# used by Traefik to transmit traffic and aqcuire TLS certificates
7+
DOMAIN="localhost"
8+
# To test the local Traefik config
9+
# DOMAIN=localhost.tiangolo.com
10+
11+
# Environment: local, staging, production
12+
ENVIRONMENT="local"
13+
14+
API_V1_STR="/api/v1"
15+
PROJECT_NAME="EmonTools"
16+
STACK_NAME="full-stack-emontools-project"
17+
18+
# Data Path
19+
DATA_BASE_PATH="/opt/emon_tools/data"
20+
STATIC_BASE_PATH="/opt/emon_tools/static"
21+
22+
# Backend
23+
# Used by the backend to generate links in emails to the frontend
24+
FRONTEND_HOST="http://localhost:5173"
25+
# In staging and production, set this env var to the frontend host, e.g.
26+
# FRONTEND_HOST=https://dashboard.example.com
27+
BACKEND_CORS_ORIGINS="http://localhost,http://localhost:5173,https://localhost,https://localhost:5173"
28+
29+
SECRET_KEY=changethis
30+
FIRST_SUPERUSER=changethis
31+
FIRST_SUPERUSER_PASSWORD=changethis
32+
33+
# Mysql
34+
MYSQL_HOST="127.0.0.1"
35+
MYSQL_PORT=3306
36+
MYSQL_DB="emontools"
37+
MYSQL_USER="emontools"
38+
MYSQL_PASSWORD=changethis

backend/Dockerfile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Use Python 3.11 slim base image
2+
FROM python:3.11-slim
3+
4+
# Set environment variables
5+
ENV PYTHONUNBUFFERED=1 \
6+
# Add the local user's bin directory to PATH
7+
PATH="/home/appuser/.local/bin:$PATH"
8+
9+
# Create a non-root user and group
10+
RUN groupadd --gid 1000 appuser && \
11+
useradd --uid 1000 --gid appuser --create-home appuser
12+
13+
# Set working directory
14+
WORKDIR /opt/emon_tools
15+
16+
# Install build dependencies if needed (e.g., for any compiled packages)
17+
RUN apt-get update && apt-get install -y --no-install-recommends \
18+
gcc \
19+
dos2unix \
20+
default-mysql-client \
21+
python3-dev \
22+
default-libmysqlclient-dev \
23+
build-essential \
24+
pkg-config \
25+
&& rm -rf /var/lib/apt/lists/*
26+
27+
# Copy requirements and install them as the non-root user
28+
COPY --chown=appuser:appuser requirements-docker-dev.txt .
29+
30+
# Create the datas and static directories for storing data
31+
RUN mkdir -p /opt/emon_tools/datas /opt/emon_tools/static /opt/emon_tools/backend \
32+
&& chown -R appuser:appuser /opt/emon_tools/datas /opt/emon_tools/static /opt/emon_tools/backend
33+
34+
# Switch to the non-root user
35+
USER appuser
36+
37+
# Install Python dependencies
38+
# Use --no-cache-dir to avoid caching the packages in the image
39+
# Use --user to install packages in the user's home directory
40+
RUN pip install --no-cache-dir --user --upgrade pip && \
41+
pip install --no-cache-dir --user -r requirements-docker-dev.txt
42+
43+
# Copy the backend source code
44+
COPY --chown=appuser:appuser . ./backend
45+
46+
# Replace env file with the one for Docker
47+
COPY --chown=appuser:appuser .env.docker ./backend/.env
48+
49+
RUN dos2unix /opt/emon_tools/backend/scripts/docker_start.sh
50+
RUN dos2unix /opt/emon_tools/backend/scripts/pre_start.sh
51+
RUN dos2unix /opt/emon_tools/backend/scripts/wait_for_db.sh
52+
53+
# Ensure scripts have execution permission
54+
RUN chmod +x /opt/emon_tools/backend/scripts/docker_start.sh \
55+
&& chmod +x /opt/emon_tools/backend/scripts/pre_start.sh \
56+
&& chmod +x /opt/emon_tools/backend/scripts/wait_for_db.sh
57+
58+
# Set the PYTHONPATH to ensure the backend module is found
59+
ENV PYTHONPATH=/opt/emon_tools
60+
61+
# Expose FastAPI port (use 8000)
62+
EXPOSE 8000
63+
64+
# Use the entrypoint script as CMD, which will run migrations and then launch uvicorn
65+
CMD ["./backend/scripts/docker_start.sh"]

backend/alembic.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[alembic]
44
# path to migration scripts
55
# Use forward slashes (/) also on windows to provide an os agnostic path
6-
script_location = ./emon_tools/fastapi/alembic
6+
script_location = ./backend/alembic
77

88
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
99
# Uncomment the line below if you want the files to be prepended with date and time

0 commit comments

Comments
 (0)