Skip to content

Commit

Permalink
Dockerize Tracksight (#1255)
Browse files Browse the repository at this point in the history
### Changelist 
moved the front and backend to be brought up via a docker container

### Testing Done
launched on different devices/OS systems 

### Resolved Tickets
n/a

---------

Co-authored-by: Ashli <[email protected]>
  • Loading branch information
Lucien950 and forbesashli authored May 17, 2024
1 parent 73e0609 commit d824000
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ black = "*"
flask = "*"
Definitions = "*"
flask_cors = "*"
pandas = "*"
flask_socketio = "*"
pytest = "*"
asammdf = "*"
requests = "*"

[dev-packages]
setuptools = "*"
Expand Down
23 changes: 23 additions & 0 deletions software/tracksight/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ENV FLASK_APP=app.py
# ENV FLASK_RUN_HOST=0.0.0.0
FROM python:3.11-slim

# for healthcheck
RUN apt update && apt install -y curl && apt clean

USER root
WORKDIR /backend
RUN pip install pipenv

WORKDIR /backend
COPY Pipfile .
COPY Pipfile.lock .
RUN pipenv install --system --deploy

WORKDIR /backend
COPY ./software/tracksight/backend/ .

USER root
WORKDIR /backend
EXPOSE 5000
CMD ["python", "app/telemetry.py"]
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def hello_world():
return "Telemetry Backend is running!"


@app.route("/health")
def health():
"""
:return:
"""
return jsonify(status="healthy"), 200


@app.route("/signal/measurements", methods=["GET"])
def return_all_measurements():
"""
Expand Down
1 change: 1 addition & 0 deletions software/tracksight/backend/app/process/influx_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dateutil.parser import parse

INFLUX_DB_URL = "https://us-east-1-1.aws.cloud2.influxdata.com"
# INFLUX_DB_URL = "http://localhost:8086"
BUCKET = "testing"
TEMP_TOKEN = "pyh_P66tpmkqnfB6IL73p1GVSyiSK_o5_fmt-1KhZ8eYu_WVoyUMddNsHDlozlstS8gZ0WVyuycQtQOCKIIWJQ=="

Expand Down
49 changes: 49 additions & 0 deletions software/tracksight/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: tracksight
services:
influx:
image: influxdb:2.7.6-alpine
ports:
- "8086:8086"
volumes:
- ./influxdb:/var/lib/influxdb
environment:
- INFLUXDB_DB=tracksight
- INFLUXDB_ADMIN_ENABLED=true
- INFLUXDB_ADMIN_USER=admin
- INFLUXDB_ADMIN_PASSWORD=admin
- INFLUXDB_USER=tracksight
- INFLUXDB_USER_PASSWORD=tracksight
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:8086/health" ]
interval: 1m1s
timeout: 5s
retries: 3
start_period: 1s
start_interval: 1s
backend:
user: root
build:
context: ../../
dockerfile: ./software/tracksight/backend/Dockerfile
ports:
- "5000:5000"
volumes:
- ./backend/logs:/backend/logs
- ../../can_bus:/can_bus
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:5000/health" ]
interval: 1m1s
timeout: 5s
retries: 3
start_period: 1s
start_interval: 1s
depends_on:
influx:
condition: service_healthy
frontend:
build: ./frontend
ports:
- "80:3000"
depends_on:
backend:
condition: service_healthy
67 changes: 67 additions & 0 deletions software/tracksight/frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi


# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1

RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD HOSTNAME="0.0.0.0" node server.js
4 changes: 3 additions & 1 deletion software/tracksight/frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}
const nextConfig = {
output: 'standalone'
}

module.exports = nextConfig

0 comments on commit d824000

Please sign in to comment.