Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerize the app #401

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**/__pycache__
**/*.pyc
**/*.pyo
**/*.pyd
*.git
.dockerignore
Dockerfile*
*.md
73 changes: 73 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# For x86 architecture, use the following base image:
FROM python:3.9.13-slim

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV POSTGRES_USER=username
ENV POSTGRES_PASSWORD=password
ENV POSTGRES_DB=databasename

# Set the working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
postgresql \
wget \
unzip \
tar \
git \
g++ \
make \
&& rm -rf /var/lib/apt/lists/*

# Download the Certabo software
RUN wget https://www.certabo.com/wp-content/uploads/SOFTWARE/Release/Ubuntu/Certabo_Tabutronic_Ubuntu_4.5.zip

# Extract the Certabo software
RUN unzip /app/Certabo_Tabutronic_Ubuntu_4.5.zip -d /app

# Install the Certabo software
RUN dpkg -i /app/Certabo_Tabutronic_Ubuntu_4.5/Certabo_Tabutronic_Ubuntu_4.5.deb || true
RUN apt-get install -f

# Clone Stockfish and compile it
RUN git clone https://github.com/official-stockfish/Stockfish.git && \
cd Stockfish/src && \
make build ARCH=armv8 && \
cp stockfish /usr/local/bin/ && \
chmod +x /usr/local/bin/stockfish && \
cd ../.. && \
rm -rf Stockfish

# Start PostgreSQL service and setup database
USER postgres
RUN service postgresql start && \
psql -c "CREATE ROLE $POSTGRES_USER WITH LOGIN PASSWORD '$POSTGRES_PASSWORD'; ALTER ROLE $POSTGRES_USER CREATEDB;"

RUN service postgresql start && \
psql -c "CREATE DATABASE $POSTGRES_DB;" && \
psql -c "GRANT ALL PRIVILEGES ON DATABASE $POSTGRES_DB TO $POSTGRES_USER;"

USER root

# Copy only the requirements file
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the start script and the rest of the application code
COPY start.sh .
COPY . .

# Give execute permissions to the start script
RUN chmod +x start.sh

# Expose the port the app runs on
EXPOSE 5000

# Command to run the start script
CMD ["./start.sh"]
5 changes: 3 additions & 2 deletions backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pathlib
from flask import Flask
from flask_socketio import SocketIO
from config import STOCKFISH_PATH
from chessLogic.chessLogic import ChessLogic
from certaboHelper.certabo import Certabo
from certaboHelper.initCertabo import InitializeCertabo
Expand All @@ -25,7 +24,9 @@
conf = ''.join([str(loc),STOCKFISH_PATH])
pStockfish = conf
elif platform.system() == 'Darwin': #Darwin for MacOS
pStockfish = STOCKFISH_PATH
pStockfish = "/opt/homebrew/bin/stockfish"
else: #Default to Linux
pStockfish ="/usr/local/bin/stockfish"

chess_logic = ChessLogic(pStockfish)

Expand Down
14 changes: 14 additions & 0 deletions backend/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

DEVICE_PATH="/dev/ttyUSB0"
if [ -c "$DEVICE_PATH" ]
then
echo -e "\n\n\tDevice $DEVICE_PATH is accessible.\n\n"
else
echo -e "\nError: Device $DEVICE_PATH is not accessible.\n\nPlease check the device path or connection.\n\nMaybe you forgot to mount the device?\n\nExample docker run command: \n\tdocker run --device=/dev/ttyUSB0:/dev/ttyUSB0 -p 5000:5000 chess-backend\n\n"
exit 1
fi

# Start the Python application
echo -e "\n\n\tStarting Python application...\n\n"
python server.py
32 changes: 32 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3'
services:
stockfish:
image: tchorwat/stockfish
ports:
- "23249:23249"

postgres:
image: postgres:14
environment:
POSTGRES_PASSWORD: password
ports:
- "5432:5432"

backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
- "5000:5000"
depends_on:
- postgres
- stockfish

frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "3000:3000"
depends_on:
- backend