Skip to content

Commit f7d0e0a

Browse files
authored
Added Docker Compose for self hosting Akinator (#12)
* Added Docker Compose for self hosting MongoDB and Dockerfile changes * Added self host section in README
1 parent 161f8f4 commit f7d0e0a

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

.dockerignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ coverage.xml
1818
.mypy_cache
1919
.pytest_cache
2020
.hypothesis
21-
venv
21+
venv
22+
compose.yml
23+
Dockerfile
24+
Procfile
25+
app.json
26+
.git
27+
.gitignore
28+
data

Dockerfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ FROM python:3.9-slim
33

44
WORKDIR /app
55

6-
COPY requirements.txt requirements.txt
6+
COPY . .
77

8-
RUN pip install -r requirements.txt
9-
10-
COPY . .
8+
RUN pip install --no-cache-dir -r requirements.txt
119

1210
CMD [ "python", "__main__.py"]
1311

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,34 @@ A Telegram bot where you can play the Akinator Guessing game
33
check out [Akinator](https://t.me/aki_akinator_bot) on Telegram.
44
If the bot is offline, contact me on telegram, so that I can look into it.
55

6-
# Deployments
6+
# Self Host (Containers)
7+
Create a `.env` file and fill up the following in it.
8+
```
9+
BOT_TOKEN=<your-bot-token>
10+
11+
# Mongo DB Username and Password
12+
MONGO_ROOT_USER=admin
13+
MONGO_ROOT_PASSWORD=admin
14+
15+
# Mongo Express Username and Password
16+
MONGOEXPRESS_LOGIN=admin
17+
MONGOEXPRESS_PASSWORD=admin
18+
```
19+
Change the username & password for MongoDB and MongoExpress as you like above.
20+
21+
Put the `compose.yml` in the same directory where `.env` file is present.
22+
After that, run the following:
23+
```
24+
docker compose up -d
25+
```
26+
# Cloud Deployments (I'm not sure if this still works)
27+
728
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://dashboard.heroku.com/new?button-url=https%3A%2F%2Fgithub.com%2Fadenosinetp10%2FAkinator-bot&template=https%3A%2F%2Fgithub.com%2Fadenosinetp10%2FAkinator-bot
829
)
930

1031
[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new/template?template=https%3A%2F%2Fgithub.com%2Fadenosinetp10%2FAkinator-Bot&envs=aki_mongo_host%2Cbot_token&aki_mongo_hostDesc=mongoDB+URI+for+accessing+database.&bot_tokenDesc=Telegram+Bot+token+obtained+from+BotFather&referralCode=9C9po6)
1132

12-
## Setting up the database
33+
## Setting up the database (In MondoDB Atlas if you don't wanna self host the Database)
1334

1435
Akinator uses **MongoDB** by default to store the user stats such as number of games won, abandoned, played and their telegram provided info and also to facilitate for the leaderboard function. MongoDB is easy to set up and work with, noSQL is easy compared to SQL. MongoDB even offers free cloud database for small use. You can create a local mongoDB cluster or use the online mongoDB Atlas to start.. Here, the scope of the following steps is to follow on the footpath of the online setup using mongoDB Atlas.
1536

compose.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
services:
2+
3+
# MongoDB
4+
mongo:
5+
container_name: mongo-db
6+
image: mongo
7+
restart: unless-stopped
8+
volumes:
9+
- ./data:/data/db
10+
environment:
11+
MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USER}
12+
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD}
13+
14+
# This is the UI dashboard for MongoDB if you want to manage the DB graphically through a browser UI.
15+
mongo-express:
16+
container_name: "mongo-express"
17+
image: mongo-express
18+
restart: unless-stopped
19+
ports:
20+
- "8081:8081"
21+
environment:
22+
ME_CONFIG_MONGODB_SERVER: mongo
23+
ME_CONFIG_MONGODB_PORT: 27017
24+
ME_CONFIG_MONGODB_ENABLE_ADMIN: false
25+
ME_CONFIG_MONGODB_AUTH_DATABASE: admin
26+
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_ROOT_USER}
27+
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_ROOT_PASSWORD}
28+
ME_CONFIG_BASICAUTH_USERNAME: ${MONGOEXPRESS_LOGIN}
29+
ME_CONFIG_BASICAUTH_PASSWORD: ${MONGOEXPRESS_PASSWORD}
30+
31+
# Akinator Telegram Bot
32+
akinator-bot:
33+
container_name: akinator-tg-bot
34+
image: advnpzn/akinator:latest
35+
restart: unless-stopped
36+
environment:
37+
bot_token: ${BOT_TOKEN}
38+
aki_mongo_host: "mongodb://${MONGO_ROOT_USER}:${MONGO_ROOT_PASSWORD}@mongo:27017"
39+

database.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from typing import Any
2-
from pymongo import MongoClient
2+
from pymongo import MongoClient, database
33
from config import AKI_MONGO_HOST
44

5-
my_client = MongoClient(host=AKI_MONGO_HOST)
6-
my_db = my_client["aki-db"]
5+
try:
6+
my_client = MongoClient(host=AKI_MONGO_HOST)
7+
my_db = my_client["aki-db"]
8+
except Exception as e:
9+
print("Error: ", e)
710

811

912
def addUser(user_id: int, first_name: str, last_name: str, user_name: str) -> None:

0 commit comments

Comments
 (0)