From 71d9f88ef3a93604ddeb8a68b222ec77aa2c3245 Mon Sep 17 00:00:00 2001 From: ariwahyunahar Date: Mon, 11 Aug 2025 15:02:35 +0700 Subject: [PATCH] docker file updated --- Docker/Dockerfile | 10 ++- Docker/README.md | 131 +++++++++++++++++--------------------- Docker/docker-compose.yml | 27 -------- install.sh | 46 +++++++++++++ 4 files changed, 113 insertions(+), 101 deletions(-) delete mode 100644 Docker/docker-compose.yml create mode 100644 install.sh diff --git a/Docker/Dockerfile b/Docker/Dockerfile index 26fd820..809bbf2 100644 --- a/Docker/Dockerfile +++ b/Docker/Dockerfile @@ -2,13 +2,17 @@ FROM python:3.10-slim WORKDIR /app +# Install system dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + gcc \ + postgresql-client \ + && rm -rf /var/lib/apt/lists/* + # Copy requirements first for better caching COPY requirements.txt . - -# Install dependencies RUN pip install --no-cache-dir -r requirements.txt -# Copy the rest of the application +# Copy application code COPY . . # Expose the port the app runs on diff --git a/Docker/README.md b/Docker/README.md index 1cb7092..305889c 100644 --- a/Docker/README.md +++ b/Docker/README.md @@ -1,117 +1,106 @@ -# Docker Setup for Digital Twin Monitoring Application +# Docker Setup for Digital Twin Monitoring -This folder contains Docker configuration files for deploying the Digital Twin Monitoring application in a production environment. - -## Files - -- `Dockerfile`: Defines how to build the application container -- `docker-compose.yml`: Defines the services and their configurations +This document provides instructions for setting up and running the Digital Twin Monitoring application using Docker. ## Prerequisites -- Docker and Docker Compose installed on your production server -- Access to PostgreSQL databases (configured in environment variables) - -## Deployment Instructions +- Ubuntu server +- Internet connection +- Basic knowledge of terminal commands -### Option 1: Using Docker Compose (Recommended) +## Quick Start -1. Copy the entire project to your production server -2. Navigate to the project root directory -3. Make sure your `.env` file is properly configured with: - - `TELEGRAM_BOT_TOKEN` - - `TELEGRAM_CHAT_ID` - - Database connection details (optional, defaults are provided) -4. Run the following command: +The easiest way to get started is to run the provided installation script: ```bash -cd Docker -docker-compose up -d +chmod +x install.sh +./install.sh ``` -This will: -- Build the Docker image using the Dockerfile -- Start the container in detached mode -- Map port 5003 to the host -- Configure environment variables -- Set up restart policies +This script will: +1. Check if Docker is installed and install it if needed +2. Build the Docker image +3. Stop any existing containers with the same name +4. Start a new container with the application -### Option 2: Using Docker Directly +After running the script, the application will be available at `http://[server-ip]:5003`. -If you prefer to use Docker without Docker Compose: +## Manual Setup -1. Copy the entire project to your production server -2. Navigate to the project root directory -3. Build the Docker image: +If you prefer to set up manually, follow these steps: + +### 1. Build the Docker image ```bash docker build -t digital-twin-monitoring -f Docker/Dockerfile . ``` -4. Run the container: +### 2. Run the Docker container ```bash docker run -d \ - --name digital-twin-app \ - -p 5003:5003 \ - -e DB_HOST_1=192.168.1.85 \ - -e DB_HOST_2=192.168.1.86 \ - -e DB_PORT=5432 \ - -e DB_USER=postgres \ - -e DB_PASS=postgres \ - -e DB_NAME=digital_twin \ - -e TELEGRAM_BOT_TOKEN=your_token \ - -e TELEGRAM_CHAT_ID=your_chat_id \ + --name digital-twin-monitoring \ --restart unless-stopped \ + -p 5003:5003 \ + --env-file .env \ digital-twin-monitoring ``` -Replace `your_token` and `your_chat_id` with your actual Telegram credentials. +## Environment Variables -## Accessing the Application +The application uses the following environment variables from the `.env` file: -Once deployed, the application will be available at: +- `TELEGRAM_BOT_TOKEN`: Token for Telegram bot notifications +- `TELEGRAM_CHAT_ID`: Chat ID for Telegram notifications +- `DB_HOST_1`: PostgreSQL host for tables: pf_parts, ms_equipment_master +- `DB_HOST_2`: PostgreSQL host for table: dl_pi_fetch_last +- `DB_PORT`: PostgreSQL port +- `DB_USER`: PostgreSQL username +- `DB_PASS`: PostgreSQL password +- `DB_NAME`: PostgreSQL database name -``` -http://your-server-ip:5003 -``` +Make sure your `.env` file is properly configured before running the container. ## Maintenance -### Viewing Logs +### Viewing logs ```bash -# Using Docker Compose -docker-compose logs -f +docker logs digital-twin-monitoring +``` + +### Stopping the container -# Using Docker directly -docker logs -f digital-twin-app +```bash +docker stop digital-twin-monitoring ``` -### Stopping the Application +### Restarting the container ```bash -# Using Docker Compose -docker-compose down +docker restart digital-twin-monitoring +``` + +### Removing the container -# Using Docker directly -docker stop digital-twin-app -docker rm digital-twin-app +```bash +docker stop digital-twin-monitoring +docker rm digital-twin-monitoring ``` -### Updating the Application +### Updating the application -1. Pull the latest code changes -2. Rebuild and restart: +To update the application, pull the latest code, rebuild the image, and restart the container: ```bash -# Using Docker Compose -docker-compose down -docker-compose up -d --build - -# Using Docker directly -docker stop digital-twin-app -docker rm digital-twin-app +git pull docker build -t digital-twin-monitoring -f Docker/Dockerfile . -# Then run the container again with the same parameters as above +docker stop digital-twin-monitoring +docker rm digital-twin-monitoring +docker run -d \ + --name digital-twin-monitoring \ + --restart unless-stopped \ + -p 5003:5003 \ + --env-file .env \ + digital-twin-monitoring ``` \ No newline at end of file diff --git a/Docker/docker-compose.yml b/Docker/docker-compose.yml deleted file mode 100644 index 125c623..0000000 --- a/Docker/docker-compose.yml +++ /dev/null @@ -1,27 +0,0 @@ -version: '3' - -services: - app: - build: - context: .. - dockerfile: Docker/Dockerfile - ports: - - "5003:5003" - environment: - - DB_HOST_1=${DB_HOST_1:-192.168.1.85} - - DB_HOST_2=${DB_HOST_2:-192.168.1.86} - - DB_PORT=${DB_PORT:-5432} - - DB_USER=${DB_USER:-postgres} - - DB_PASS=${DB_PASS:-postgres} - - DB_NAME=${DB_NAME:-digital_twin} - - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN} - - TELEGRAM_CHAT_ID=${TELEGRAM_CHAT_ID} - volumes: - - ../.env:/app/.env - restart: unless-stopped - networks: - - app-network - -networks: - app-network: - driver: bridge \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..05d0ee9 --- /dev/null +++ b/install.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# Exit on error +set -e + +echo "Starting Digital Twin Monitoring installation..." + +# Check if Docker is installed +if ! command -v docker &> /dev/null; then + echo "Docker is not installed. Installing Docker..." + sudo apt-get update + sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - + sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" + sudo apt-get update + sudo apt-get install -y docker-ce + sudo systemctl enable docker + sudo systemctl start docker + sudo usermod -aG docker $USER + echo "Docker installed successfully. You may need to log out and log back in for group changes to take effect." +else + echo "Docker is already installed." +fi + +# Build the Docker image +echo "Building Docker image..." +sudo docker build -t digital-twin-monitoring -f Docker/Dockerfile . + +# Check if container is already running and stop it +if sudo docker ps -a | grep -q digital-twin-monitoring; then + echo "Stopping existing container..." + sudo docker stop digital-twin-monitoring || true + sudo docker rm digital-twin-monitoring || true +fi + +# Run the Docker container +echo "Starting Docker container..." +sudo docker run -d \ + --name digital-twin-monitoring \ + --restart unless-stopped \ + -p 5003:5003 \ + --env-file .env \ + digital-twin-monitoring + +echo "Installation completed successfully!" +echo "Digital Twin Monitoring is now running at http://localhost:5003" \ No newline at end of file