From 678fa3dab225998b45d32619176c03c1f72c31c7 Mon Sep 17 00:00:00 2001 From: mantsevat Date: Tue, 24 Sep 2024 17:11:35 +0300 Subject: [PATCH] first task hello_world --- hello_world/Dockerfile | 11 ++++++++++ hello_world/docker-compose.yml | 18 ++++++++++++++++ hello_world/hello_world.py | 38 ++++++++++++++++++++++++++++++++++ hello_world/requirements.txt | 1 + 4 files changed, 68 insertions(+) create mode 100644 hello_world/Dockerfile create mode 100644 hello_world/docker-compose.yml create mode 100644 hello_world/hello_world.py create mode 100644 hello_world/requirements.txt diff --git a/hello_world/Dockerfile b/hello_world/Dockerfile new file mode 100644 index 0000000..aeeb7c8 --- /dev/null +++ b/hello_world/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.11-alpine + +WORKDIR /hello_world + +COPY requirements.txt . + +RUN pip3 install --no-cache-dir -r requirements.txt + +COPY . /hello_world + +CMD ["python3", "hello_world.py"] diff --git a/hello_world/docker-compose.yml b/hello_world/docker-compose.yml new file mode 100644 index 0000000..3f9f9b6 --- /dev/null +++ b/hello_world/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.1' + +services: + + mongo: + image: mongo + restart: always + environment: + MONGO_INITDB_ROOT_USERNAME: root + MONGO_INITDB_ROOT_PASSWORD: example + + app: + build: . + depends_on: + - mongo + environment: + - MONGO_URL=mongodb://root:example@mongo:27017/ + diff --git a/hello_world/hello_world.py b/hello_world/hello_world.py new file mode 100644 index 0000000..c02434e --- /dev/null +++ b/hello_world/hello_world.py @@ -0,0 +1,38 @@ +from pymongo import MongoClient + +def create_users(db): + users = [] + for i in range (1, 11): + users.append({"name": "test_user" + str(i), "email": "test_email" + str(i), "password": "testpass" + str(i), "games": []}) + col = db["users"] + col.insert_many(users) + +def create_games(db): + games = [] + for i in range(2): + games.append({"name": "test_game", "players": [], "owner_id": 5*i +1}) + col = db["games"] + col.insert_many(games) + +def get_db(): + connection_string = "mongodb://root:example@mongo:27017/" + client = MongoClient(connection_string) + db = client["secret_santa_db"] + return db + +def read_data(db, collection_name): + col = db[collection_name] + for record in col.find(): + print(record) + +def main(): + secret_santa_db = get_db() + + create_users(secret_santa_db) + create_games(secret_santa_db) + + read_data(secret_santa_db, "users") + read_data(secret_santa_db, "games") + +if __name__ == "__main__": + main() diff --git a/hello_world/requirements.txt b/hello_world/requirements.txt new file mode 100644 index 0000000..ecf3c6f --- /dev/null +++ b/hello_world/requirements.txt @@ -0,0 +1 @@ +pymongo==4.9.1