Adeel020 commited on
Commit
dbe4e53
·
0 Parent(s):

Initial Chatwoot deployment: all-in-one Docker with PG, Redis, Nginx

Browse files
Files changed (5) hide show
  1. Dockerfile +37 -0
  2. README.md +13 -0
  3. entrypoint.sh +149 -0
  4. nginx.conf +20 -0
  5. supervisord.conf +47 -0
Dockerfile ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ===== CA Chatwoot — All-in-One Container =====
2
+ # Runs Chatwoot, PostgreSQL, Redis, Sidekiq, and Nginx in a single container
3
+ # for deployment on Hugging Face Spaces (port 7860)
4
+
5
+ FROM chatwoot/chatwoot:community
6
+
7
+ USER root
8
+
9
+ # ===== Install PostgreSQL, Redis, Nginx, Supervisor =====
10
+ RUN apt-get update && apt-get install -y --no-install-recommends \
11
+ postgresql-16 \
12
+ postgresql-client-16 \
13
+ redis-server \
14
+ nginx \
15
+ supervisor \
16
+ && rm -rf /var/lib/apt/lists/* \
17
+ && rm -f /etc/nginx/sites-enabled/default
18
+
19
+ # ===== Nginx: reverse proxy port 7860 → Chatwoot on 3000 =====
20
+ COPY nginx.conf /etc/nginx/sites-available/chatwoot
21
+ RUN ln -s /etc/nginx/sites-available/chatwoot /etc/nginx/sites-enabled/ \
22
+ && echo "daemon off;" >> /etc/nginx/nginx.conf
23
+
24
+ # ===== Supervisor: manage all processes =====
25
+ COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
26
+
27
+ # ===== Entrypoint =====
28
+ COPY entrypoint.sh /entrypoint.sh
29
+ RUN chmod +x /entrypoint.sh
30
+
31
+ # ===== Data directory for persistence across restarts =====
32
+ RUN mkdir -p /data/postgres /data/redis && chmod 777 /data
33
+
34
+ # ===== Port for Hugging Face Spaces =====
35
+ EXPOSE 7860
36
+
37
+ ENTRYPOINT ["/entrypoint.sh"]
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: CA Chatwoot
3
+ emoji: 💬
4
+ colorFrom: gray
5
+ colorTo: black
6
+ sdk: docker
7
+ app_port: 7860
8
+ ---
9
+
10
+ # CA Chatwoot — AI-Powered Customer Support
11
+
12
+ Chatwoot instance for The Capital Acquisition website and client sites.
13
+ AI responses handled by OpenRouter via Netlify serverless function.
entrypoint.sh ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ # ===== Colors for output =====
5
+ RED='\033[0;31m'
6
+ GREEN='\033[0;32m'
7
+ YELLOW='\033[1;33m'
8
+ NC='\033[0m'
9
+
10
+ echo -e "${GREEN}=== CA Chatwoot Starting... ===${NC}"
11
+
12
+ # ===== Configuration from environment =====
13
+ CHATWOOT_DB_HOST="${CHATWOOT_DB_HOST:-localhost}"
14
+ CHATWOOT_DB_PORT="${CHATWOOT_DB_PORT:-5432}"
15
+ CHATWOOT_DB_NAME="${CHATWOOT_DB_NAME:-chatwoot}"
16
+ CHATWOOT_DB_USERNAME="${CHATWOOT_DB_USERNAME:-chatwoot}"
17
+ CHATWOOT_DB_PASSWORD="${CHATWOOT_DB_PASSWORD:-chatwoot_password}"
18
+ SECRET_KEY_BASE="${SECRET_KEY_BASE}"
19
+ REDIS_URL="${REDIS_URL:-redis://localhost:6379}"
20
+
21
+ export SECRET_KEY_BASE
22
+ export REDIS_URL
23
+
24
+ # ===== Setup PostgreSQL (if local) =====
25
+ setup_postgres() {
26
+ if [ "$CHATWOOT_DB_HOST" = "localhost" ] || [ "$CHATWOOT_DB_HOST" = "127.0.0.1" ]; then
27
+ echo -e "${YELLOW}Setting up local PostgreSQL...${NC}"
28
+
29
+ # Initialize PG data directory if needed
30
+ if [ ! -d "/data/postgres/16" ]; then
31
+ echo "Initializing PostgreSQL data directory..."
32
+ mkdir -p /data/postgres
33
+ chown postgres:postgres /data/postgres
34
+ su - postgres -c "/usr/lib/postgresql/16/bin/initdb -D /data/postgres/16"
35
+
36
+ # Configure PostgreSQL to listen on all interfaces
37
+ echo "listen_addresses = '*'" >> /data/postgres/16/postgresql.conf
38
+ echo "port = 5432" >> /data/postgres/16/postgresql.conf
39
+
40
+ # Allow password auth
41
+ echo "host all all 127.0.0.1/0 md5" >> /data/postgres/16/pg_hba.conf
42
+ echo "host all all ::1/0 md5" >> /data/postgres/16/pg_hba.conf
43
+ fi
44
+
45
+ # Start PostgreSQL
46
+ su - postgres -c "/usr/lib/postgresql/16/bin/pg_ctl -D /data/postgres/16 -l /data/postgres/logfile start"
47
+
48
+ # Wait for PostgreSQL to be ready
49
+ echo "Waiting for PostgreSQL..."
50
+ for i in $(seq 1 30); do
51
+ if su - postgres -c "psql -c 'SELECT 1'" &>/dev/null; then
52
+ echo -e "${GREEN}PostgreSQL is ready${NC}"
53
+ break
54
+ fi
55
+ sleep 1
56
+ done
57
+
58
+ # Create user and database if they don't exist
59
+ su - postgres -c "psql -tc \"SELECT 1 FROM pg_roles WHERE rolname='$CHATWOOT_DB_USERNAME'\" | grep -q 1 || psql -c \"CREATE USER $CHATWOOT_DB_USERNAME WITH PASSWORD '$CHATWOOT_DB_PASSWORD' CREATEDB;\""
60
+ su - postgres -c "psql -tc \"SELECT 1 FROM pg_database WHERE datname='$CHATWOOT_DB_NAME'\" | grep -q 1 || psql -c \"CREATE DATABASE $CHATWOOT_DB_NAME OWNER $CHATWOOT_DB_USERNAME;\""
61
+
62
+ # Set DATABASE_URL for Chatwoot
63
+ export DATABASE_URL="postgresql://$CHATWOOT_DB_USERNAME:$CHATWOOT_DB_PASSWORD@$CHATWOOT_DB_HOST:$CHATWOOT_DB_PORT/$CHATWOOT_DB_NAME"
64
+ else
65
+ echo -e "${YELLOW}Using external PostgreSQL at $CHATWOOT_DB_HOST${NC}"
66
+ export DATABASE_URL="postgresql://$CHATWOOT_DB_USERNAME:$CHATWOOT_DB_PASSWORD@$CHATWOOT_DB_HOST:$CHATWOOT_DB_PORT/$CHATWOOT_DB_NAME"
67
+ fi
68
+ }
69
+
70
+ # ===== Setup Redis (if local) =====
71
+ setup_redis() {
72
+ if echo "$REDIS_URL" | grep -q "localhost\|127.0.0.1"; then
73
+ echo -e "${YELLOW}Starting local Redis...${NC}"
74
+ mkdir -p /data/redis
75
+ redis-server --appendonly yes --dir /data/redis --daemonize yes
76
+
77
+ # Wait for Redis
78
+ for i in $(seq 1 10); do
79
+ if redis-cli ping &>/dev/null; then
80
+ echo -e "${GREEN}Redis is ready${NC}"
81
+ break
82
+ fi
83
+ sleep 1
84
+ done
85
+ else
86
+ echo -e "${YELLOW}Using external Redis${NC}"
87
+ fi
88
+ }
89
+
90
+ # ===== Run Chatwoot DB setup =====
91
+ setup_chatwoot_db() {
92
+ echo -e "${YELLOW}Running database setup...${NC}"
93
+ cd /app
94
+
95
+ # Create database if not exists
96
+ bundle exec rails db:create 2>/dev/null || true
97
+
98
+ # Run migrations
99
+ bundle exec rails db:migrate
100
+
101
+ # Seed data (admin account, etc.) if this is a fresh DB
102
+ bundle exec rails db:seed 2>/dev/null || true
103
+
104
+ echo -e "${GREEN}Database setup complete${NC}"
105
+ }
106
+
107
+ # ===== Create admin account =====
108
+ create_admin() {
109
+ echo -e "${YELLOW}Creating/verifying admin account...${NC}"
110
+ cd /app
111
+
112
+ # Use Rails runner to create admin if not exists
113
+ bundle exec rails runner "
114
+ email = ENV['ADMIN_EMAIL'] || 'admin@thecapitalacquisition.com'
115
+ password = ENV['ADMIN_PASSWORD'] || 'Capital123!'
116
+
117
+ unless User.exists?(email: email)
118
+ user = User.create!(
119
+ email: email,
120
+ password: password,
121
+ name: 'CA Admin'
122
+ )
123
+ account = Account.create!(
124
+ name: ENV['ACCOUNT_NAME'] || 'Capital Acquisition'
125
+ )
126
+ AccountUser.create!(
127
+ user: user,
128
+ account: account,
129
+ role: :administrator
130
+ )
131
+ puts \"Admin created: #{email}\"
132
+ else
133
+ puts \"Admin already exists: #{email}\"
134
+ end
135
+ "
136
+ }
137
+
138
+ # ===== Start everything via supervisor =====
139
+ start_services() {
140
+ echo -e "${GREEN}Starting all services...${NC}"
141
+ exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
142
+ }
143
+
144
+ # ===== Main =====
145
+ setup_postgres
146
+ setup_redis
147
+ setup_chatwoot_db
148
+ create_admin
149
+ start_services
nginx.conf ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ server {
2
+ listen 7860;
3
+ server_name _;
4
+
5
+ client_max_body_size 100m;
6
+
7
+ location / {
8
+ proxy_pass http://127.0.0.1:3000;
9
+ proxy_set_header Host $host;
10
+ proxy_set_header X-Real-IP $remote_addr;
11
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12
+ proxy_set_header X-Forwarded-Proto $scheme;
13
+
14
+ # WebSocket support (Action Cable for real-time)
15
+ proxy_http_version 1.1;
16
+ proxy_set_header Upgrade $http_upgrade;
17
+ proxy_set_header Connection "upgrade";
18
+ proxy_read_timeout 86400;
19
+ }
20
+ }
supervisord.conf ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [supervisord]
2
+ nodaemon=true
3
+ logfile=/dev/null
4
+ pidfile=/tmp/supervisord.pid
5
+
6
+ [program:postgresql]
7
+ command=/usr/bin/pg_ctlcluster 16 main start
8
+ user=postgres
9
+ autostart=true
10
+ autorestart=true
11
+ stderr_logfile=/dev/fd/1
12
+ stdout_logfile=/dev/fd/1
13
+ startsecs=5
14
+
15
+ [program:redis]
16
+ command=/usr/bin/redis-server --appendonly yes --dir /data/redis
17
+ autostart=true
18
+ autorestart=true
19
+ stderr_logfile=/dev/fd/1
20
+ stdout_logfile=/dev/fd/1
21
+
22
+ [program:chatwoot-web]
23
+ command=/bin/bash -lc "cd /app && bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:3000"
24
+ user=chatwoot
25
+ autostart=true
26
+ autorestart=true
27
+ stderr_logfile=/dev/fd/1
28
+ stdout_logfile=/dev/fd/1
29
+ environment=RAILS_ENV="production",NODE_ENV="production",INSTALLATION_ENV="docker"
30
+ startsecs=10
31
+
32
+ [program:chatwoot-sidekiq]
33
+ command=/bin/bash -lc "cd /app && bundle exec sidekiq -C config/sidekiq.yml"
34
+ user=chatwoot
35
+ autostart=true
36
+ autorestart=true
37
+ stderr_logfile=/dev/fd/1
38
+ stdout_logfile=/dev/fd/1
39
+ environment=RAILS_ENV="production",NODE_ENV="production",INSTALLATION_ENV="docker"
40
+ startsecs=10
41
+
42
+ [program:nginx]
43
+ command=/usr/sbin/nginx -g "daemon off;"
44
+ autostart=true
45
+ autorestart=true
46
+ stderr_logfile=/dev/fd/1
47
+ stdout_logfile=/dev/fd/1