Skip to content

Commit

Permalink
update dockerfiles and entrypoints for prod, adjust docker-compose, a…
Browse files Browse the repository at this point in the history
…djust settings.py
  • Loading branch information
alex-h-23 committed Nov 23, 2024
1 parent 088036a commit 37478e3
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 101 deletions.
23 changes: 15 additions & 8 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"format": "prettier -w src",
"format:check": "prettier -c src",
"typecheck": "tsc --noEmit",
"prepare": "cd .. && husky client/.husky"
"prepare": "if [ \"$NODE_ENV\" != \"production\" ]; then cd .. && husky client/.husky; fi"
},
"dependencies": {
"@hookform/resolvers": "^3.9.0",
Expand Down Expand Up @@ -55,7 +55,7 @@
"@csstools/postcss-oklab-function": "^3.0.16",
"@tanstack/eslint-plugin-query": "^5.43.1",
"@types/js-cookie": "^3.0.6",
"@types/node": "^20.14.10",
"@types/node": "20.17.6",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.13.1",
Expand All @@ -68,7 +68,8 @@
"postcss": "^8",
"prettier": "^3.3.2",
"prettier-plugin-tailwindcss": "^0.6.5",
"sonner": "^1.6.1",
"tailwindcss": "^3.4.1",
"typescript": "^5.5.2"
"typescript": "5.7.2"
}
}
2 changes: 1 addition & 1 deletion client/src/components/main/header/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function Links({ isHiddenWhenLg }: { isHiddenWhenLg: boolean }) {
Upcoming Events
</Link>

<Link className={`${onHoverStyle}`} href="/about">
<Link className={`${onHoverStyle}`} href="https://coexistaus.org/">
About Us
</Link>

Expand Down
32 changes: 20 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,47 @@ services:
container_name: coexist-db
image: postgres:16
restart: unless-stopped
env_file: ./server/.env
volumes:
- ${LOCAL_WORKSPACE_FOLDER:-.}/data/db:/var/lib/postgresql/data
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 3s
timeout: 3s
retries: 5
env_file: ./server/.env
ports:
- 5432:5432
# Remove port mapping in production for security
#ports:
# - "5432:5432"

server:
container_name: coexist-server
build:
context: .
dockerfile: ./docker/server/Dockerfile
restart: unless-stopped
env_file: ./server/.env
depends_on:
- db
ports:
- 8000:8000
volumes:
- ${LOCAL_WORKSPACE_FOLDER:-.}/server:/app
- "8000:8000"
# Remove code volume mounts in production
#volumes:
# - ./server:/app

client:
container_name: coexist-client
build:
context: .
dockerfile: ./docker/client/Dockerfile
restart: unless-stopped
env_file: ./client/.env
depends_on:
- server
ports:
- 3000:3000
volumes:
- ${LOCAL_WORKSPACE_FOLDER:-.}/client:/app
- ignore:/app/node_modules
- "3000:3000"
# Remove code volume mounts in production
#volumes:
# - ./client:/app

volumes:
ignore:
db-data:
5 changes: 0 additions & 5 deletions docker/client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ COPY /docker/client/entrypoint.sh /entrypoint.sh

COPY ./client/package.json ./client/package-lock.json ./

# Install ALL Dependencies
RUN npm install

# Copy Application code into a directory called `app`
COPY ./client /app

Expand All @@ -21,5 +18,3 @@ COPY ./client /app
# CMD commands get executed at container runtime!
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]

# TODO: Production
75 changes: 28 additions & 47 deletions docker/client/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,58 +1,39 @@
#!/bin/bash
#!/bin/sh

echo "${APP_NAME^^} - NextJS CONTAINER STARTING..."
echo $APP_NAME
set -e

# Display Docker Image / CI / Release details
echo "Image Build Date/Time: " "$(cat /app/build_timestamp.txt)" "UTC"
echo "$APP_NAME - NextJS CONTAINER STARTING..."
echo "APP_NAME: $APP_NAME"

# Display Build Date/Time if available
if [ -f /app/build_timestamp.txt ]; then
echo "Image Build Date/Time: $(cat /app/build_timestamp.txt) UTC"
fi

echo "-----------------------------------------------------------"
echo "APP_ENV: ${APP_ENV}"

# # ====================================================================================
# # Debug / Sanity check info
# # ====================================================================================
# echo " "
# echo "======= Current Dir / Files (Debug) ============================================================================="
# pwd
# ls -al

# echo " "
# echo "======= Env Vars (Debug) ========================================================================================"
# if [ "${APP_ENV^^}" != "PRODUCTION" ]; then
# # Only print environment vars in non-prod environments to prevent sensitive variables being sent to logging system
# printenv
# fi

# echo " "
# echo "======= Linux version (Debug) ==================================================================================="
# cat /etc/os-release

# echo " "
# echo "======= Node Path & Version (Debug) ==========================================================================="
# node -v

# Check for required env vars, exit as failure if missing these critical env vars.
if [[ -z "${APP_ENV}" ]]; then
# Check for required env vars
if [ -z "${APP_ENV}" ]; then
echo "█████████████████████████████████████████████████████████████████████████████████████████████████████████████"
echo "█ CRITICAL ERROR: Missing 'APP_ENV' environment variables."
echo "█ CRITICAL ERROR: Missing 'APP_ENV' environment variable."
echo "█████████████████████████████████████████████████████████████████████████████████████████████████████████████"
echo "APP_ENV=" $APP_ENV
echo "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"
exit
echo "APP_ENV=${APP_ENV}"
exit 1
fi

# CI TEST DOWN THE TRACK

# ====================================================================================
# Run inbuilt nextjs server if ENV is LOCAL
# ====================================================================================
if [ "${APP_ENV^^}" = "DEVELOPMENT" ]; then
# Install dependencies (idk why it's not installing the latest ones in the docker image)
# Start the application based on APP_ENV
if [ "${APP_ENV}" = "PRODUCTION" ]; then
echo "Starting Next.js application in production mode"
# Build and run for prod
npm install
# Run developments
echo " "
echo "======= Starting inbuilt nextjs webserver ==================================================================="
npm run dev
exit
fi
npm run build
npm start
elif [ "${APP_ENV}" = "DEVELOPMENT" ]; then
echo "Starting Next.js application in development mode"
npm install
exec npm run dev
else
echo "Unknown APP_ENV: ${APP_ENV}"
exit 1
fi
40 changes: 30 additions & 10 deletions docker/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
# Use the official Python image
FROM python:3.12-slim

RUN apt-get update && apt-get install --yes --no-install-recommends postgresql-client g++ libssl-dev && rm -rf /var/lib/apt/lists/*

RUN pip install --upgrade pip && pip install poetry

RUN poetry config virtualenvs.in-project false
RUN poetry config virtualenvs.create false
# Install system dependencies
RUN apt-get update && \
apt-get install --yes --no-install-recommends \
build-essential \
libssl-dev \
libpq-dev \
postgresql-client && \
rm -rf /var/lib/apt/lists/*

# Set work directory
WORKDIR /app

COPY ./docker/server/entrypoint.sh /entrypoint.sh
# Install Poetry
RUN pip install --upgrade pip && \
pip install poetry

# Configure Poetry
RUN poetry config virtualenvs.create false

# Copy the application files
COPY ./server/pyproject.toml ./server/poetry.lock ./
RUN poetry install --no-dev --no-interaction --no-ansi

COPY ./server/ ./

RUN poetry install
# Collect static files
RUN python manage.py collectstatic --noinput --verbosity 2

COPY ./server ./
# Create log directory
RUN mkdir -p /var/log/accesslogs && \
chmod -R 755 /var/log/accesslogs

# Copy entrypoint script
COPY ./docker/server/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]

# Start the application
ENTRYPOINT ["/entrypoint.sh"]
12 changes: 9 additions & 3 deletions docker/server/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

set -e

export DJANGO_SETTINGS_MODULE=api.settings

# Wait until Database is available before continuing
printf "\n" && echo "Checking Database is up"
# using psql
Expand All @@ -17,9 +21,11 @@ python manage.py migrate --noinput
echo "Collecting static files"
python manage.py collectstatic --noinput

# Create Django Superuser
echo "Creating Django Superuser"
python manage.py createsuperuser --noinput
# Check if superuser exists
if [ "$APP_ENV" = "DEVELOPMENT" ]; then
echo "Creating Django Superuser"
python manage.py createsuperuser --noinput || true
fi

# Run inbuilt Django server if ENV is development
if [ "${APP_ENV^^}" = "DEVELOPMENT" ]; then
Expand Down
3 changes: 3 additions & 0 deletions server/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ APP_NAME=DjangoAPI

APP_ENV=PRODUCTION

# =============
# TO BE CHANGED
# =============

API_SECRET_KEY="supersecretkey"
API_ALLOWED_HOSTS=".localhost 127.0.0.1 [::1]"

Expand Down
Loading

0 comments on commit 37478e3

Please sign in to comment.