Skip to content

Commit

Permalink
updated initial docker compose scripts for production env
Browse files Browse the repository at this point in the history
  • Loading branch information
MIKEINTOSHSYSTEMS committed Oct 20, 2024
1 parent c2c59eb commit d7838b8
Show file tree
Hide file tree
Showing 15 changed files with 3,132 additions and 39 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
*.txt
!mysql-init/init.sql
!/ai/chat/requirements.txt
/ai/venv
/ai/venv
/api/database/backups/
/ssl/
116 changes: 111 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ WORKDIR /var/www/html
# Copy application source code to the container
COPY analytics/ /var/www/html/analytics/
COPY app/ /var/www/html/app/
COPY api/ /var/www/html/api/
COPY assets/ /var/www/html/assets/
COPY backend/ /var/www/html/backend/
COPY dist/ /var/www/html/dist/
Expand Down Expand Up @@ -46,6 +47,8 @@ COPY test.php /var/www/html/
COPY pwabuilder-sw.js /var/www/html/
COPY manifest.json /var/www/html/
COPY 404.php /var/www/html/
COPY server.html /var/www/html/
COPY start.php /var/www/html/

# Copy AI chat files to the container
COPY ai/ /var/www/html/ai/
Expand All @@ -61,14 +64,85 @@ RUN apt-get update && \
apt-get install -y libpng-dev && \
docker-php-ext-install pdo pdo_mysql gd

# Expose the port Apache listens on
#EXPOSE 80
# Copy custom php.ini configuration
#COPY config/php.ini /usr/local/etc/php/php.ini

# Install system dependencies and PHP extensions
RUN apt-get update && \
apt-get install -y \
libpng-dev \
libmagickwand-dev \
libcurl4-openssl-dev \
libonig-dev \
supervisor \
libldap2-dev \
libicu-dev \
libzip-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libxml2-dev \
libpq-dev \
libsqlite3-dev \
libgmp-dev \
libssl-dev \
libsodium-dev \
zip \
unzip \
git \
curl \
nginx \
--no-install-recommends && \
# Install PHP extensions
docker-php-ext-configure gd --with-freetype --with-jpeg && \
docker-php-ext-install -j$(nproc) \
gd \
mysqli \
ldap \
intl \
pdo_mysql \
pdo_pgsql \
pgsql \
pdo_sqlite \
soap \
zip \
curl \
mbstring \
opcache \
bcmath \
gmp \
sodium \
# imap \
xml \
gettext \
&& \
# Enable additional PHP modules as needed
docker-php-ext-enable \
opcache \
bcmath \
gmp \
sodium \
# imap \
xml \
gettext \
# Set permissions after copying files
&& chmod 644 /etc/nginx/conf.d/default.conf \
&& \
# Clean up unnecessary packages and files
apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Set permissions for specific folders
RUN chown -R www-data:www-data /var/www/html/app/templates_c

# Expose the port PHP-FPM listens on
EXPOSE 9000
EXPOSE 9009

# Expose the port Apache listens on
EXPOSE 80

# Start PHP-FPM when the container runs
CMD ["php-fpm"]
#CMD ["php-fpm"]

################################################################################
# Base image for AI Chatbot Service
Expand Down Expand Up @@ -102,4 +176,36 @@ EXPOSE 8502
HEALTHCHECK CMD curl --fail http://localhost:8502/_stcore/health

# Start Streamlit when the container runs
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8502", "--server.address=0.0.0.0"]
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8502", "--server.address=192.168.160.10"]


################################################################################
# Setup for Supervisor
################################################################################

# Use the final PHP application stage as base
FROM php_app AS final_stage

# Copy supervisord configuration
COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Install Python and Streamlit in the final stage
#RUN apt-get update && apt-get install -y python3 python3-pip && pip3 install streamlit sshtunnel
RUN apt-get update && apt-get install -y python3 python3-pip && pip3 install -r requirements.txt

# Copy the chatbot application files
COPY ai/chat /ai/chat

# Set permissions for supervisord.conf (if exists)
RUN if [ -f /etc/supervisor/conf.d/supervisord.conf ]; then \
chown root:root /etc/supervisor/conf.d/supervisord.conf && \
chmod 644 /etc/supervisor/conf.d/supervisord.conf; \
else \
echo "supervisord.conf not found"; \
fi

# Set the working directory back to PHP application's directory
WORKDIR /var/www/html

# Start both PHP-FPM and Nginx using supervisord
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
53 changes: 53 additions & 0 deletions api/database/backups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

$hardcodedPassword = 'me@dereja';

if (!isset($_POST['password']) || $_POST['password'] !== $hardcodedPassword) {
http_response_code(403);
echo json_encode(['success' => false, 'message' => 'Invalid password']);
exit;
}

// Database connection details
$dbHost = '192.168.160.4';
$dbUser = 'merqderejadb';
$dbPassword = 'merqderejadb';
$dbName = 'merqderejadb';

// Docker container name
$containerName = 'merq_dereja_postgres';

// Directory to store backups
$backupDir = './backups';
if (!is_dir($backupDir)) {
mkdir($backupDir, 0777, true); // Create directory if it doesn't exist
}

// Backup file name with timestamp
$backupFile = 'derejame_mb_backup_' . date('Ymd_His') . '.sql';
$backupFilePath = "$backupDir/$backupFile";

// Command to execute pg_dump inside the Docker container
$command = "docker exec $containerName sh -c \"PGPASSWORD='$dbPassword' pg_dump -h $dbHost -U $dbUser -d $dbName -F c -b -v -f /tmp/$backupFile\" && docker cp $containerName:/tmp/$backupFile $backupFilePath";

// Execute the command
$output = [];
$return_var = 0;
exec($command . ' 2>&1', $output, $return_var);

if ($return_var === 0) {
// Prepare backup details for the response
$backupDetails = [
'name' => $backupFile,
'date' => date('Y-m-d H:i:s'),
'size' => filesize($backupFilePath),
'path' => $backupFilePath // Ensure this path is correct and accessible
];

// Send JSON response with success status and redirect URL
echo json_encode(['success' => true, 'newBackup' => $backupDetails, 'redirect' => 'mb.php']);
} else {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Error creating backup.']);
}
?>
30 changes: 30 additions & 0 deletions api/database/bulk_actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

$hardcodedPassword = 'me@dereja';

if (!isset($_POST['password']) || $_POST['password'] !== $hardcodedPassword) {
http_response_code(403);
echo json_encode(['success' => false, 'message' => 'Invalid password']);
exit;
}

$backupDir = './backups';

// Check if action and backups are set
if (isset($_POST['action']) && $_POST['action'] == 'delete' && isset($_POST['backups'])) {
$backupsToDelete = $_POST['backups'];

foreach ($backupsToDelete as $backupName) {
$filePath = "$backupDir/" . basename($backupName);
if (file_exists($filePath)) {
unlink($filePath);
}
}

header("Location: mb.php?msg=deleted");
exit();
}

header("Location: mb.php");
exit();
?>
Loading

0 comments on commit d7838b8

Please sign in to comment.