Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed DockerAssignemnt.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions ErfanGeramizadehNaeini/Q1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## postgres
``` docker run --rm -d --name pg -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=ERFAN1234 -e POSTGRES_DB=postgres -p 15432:5432 -v pgdata:/var/lib/postgresql/data postgres:14```

POSTGRES_PASSWORD and POSTGRES_USER for the credentials neede for authenticating
-p needed to bind the first port from the host to the docker container.
And -v needed to persist data that resides in the path specified.
at last the image is postgres:14
I wrote a python client that I will explain in the recorded video
75 changes: 75 additions & 0 deletions ErfanGeramizadehNaeini/Q1/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import psycopg2
from psycopg2 import sql

DB_NAME = "newdb"
USER = "postgres"
PASSWORD = "ERFAN1234"
PORT = 15432
try:
conn = psycopg2.connect(
host="localhost",
port=PORT,
database="postgres",
user=USER,
password=PASSWORD

)
conn.autocommit = True
cur = conn.cursor()
cur.execute("SELECT 1 FROM pg_database WHERE datname = %s;", (DB_NAME,))
exists = cur.fetchone()

if not exists:
cur.execute(sql.SQL("CREATE DATABASE {}").format(
sql.Identifier(DB_NAME)
))
print(f"Database '{DB_NAME}' created.")
else:
print(f"Database '{DB_NAME}' already exists.")

cur.close()
conn.close()

conn = psycopg2.connect(
host="localhost",
port=PORT,
database=DB_NAME,
user=USER,
password=PASSWORD
)
cur = conn.cursor()

cur.execute("""
CREATE TABLE IF NOT EXISTS test_table (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
""")
while (1):
print("1. Insert data")
print("2. Select data")
print("3. Exit")
option = int(input())
if (option == 1):
try:
name = input("name: ")
cur.execute("INSERT INTO test_table (name) VALUES (%s);",
(name,))
conn.commit()
except Exception as e:
print("Error:", e)

elif (option == 2):
cur.execute("SELECT * FROM test_table;")
rows = cur.fetchall()
for row in rows:
print(row)
elif (option == 3):
print("Exiting...")
exit(0)
cur.close()
conn.close()


except Exception as e:
print("Error:", e)
1 change: 1 addition & 0 deletions ErfanGeramizadehNaeini/Q1/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker run --rm -d --name pg -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=ERFAN1234 -e POSTGRES_DB=postgres -p 15432:5432 -v pgdata:/var/lib/postgresql/data postgres:14
10 changes: 10 additions & 0 deletions ErfanGeramizadehNaeini/Q2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## redis

``` docker run -d --name rds -p 6390:6379 redis:7.0```

The explaination is the same as Q1

Int the first client program I set 5 keys to a value and then published hello followed by an integer to a channel then in the second program I retrieved all the keys and values and then listened to the channel for incoming messages.

Using redis insight will be explained in the recorded video.

15 changes: 15 additions & 0 deletions ErfanGeramizadehNaeini/Q2/first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import redis
import time
CHANNEL = 'erfangchannel'
r = redis.Redis(host='localhost', port=6390, decode_responses=True)


for i in range(5):
r.set(f"hello{i}", f"world{i}")
i = 1
while (1):
message = f"Hello {i}"
r.publish(CHANNEL, message)
print(f"Published: {message}")
time.sleep(1)
i += 1
2 changes: 2 additions & 0 deletions ErfanGeramizadehNaeini/Q2/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker run -d --name rds -p 6390:6379 redis:7.0
# redis-cli -h localhost -p 6390 ping for making sure it works
18 changes: 18 additions & 0 deletions ErfanGeramizadehNaeini/Q2/second.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import redis
CHANNEL = 'erfangchannel'

r = redis.Redis(host='localhost', port=6390, decode_responses=True)
keys = r.keys('*')
print(keys)
for key in keys:
value = r.get(key)
print(f"{key}: {value}")

pubsub = r.pubsub()

pubsub.subscribe(CHANNEL)
print("Subscribed to 'mychannel'...")

for message in pubsub.listen():
if message['type'] == 'message':
print(f"Received: {message['data']}")
9 changes: 9 additions & 0 deletions ErfanGeramizadehNaeini/Q3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## celery
The docker compose will bring uo the redis as well as celery
I useed this bind mount so I can run docker containers outside of the existing container
- /var/run/docker.sock:/var/run/docker.sock

Celery depends on redis. It means that runing celery container depends on running redis container.
I used CELERY_BROKER_URL and CELERY_RESULT_BACKEND for telling where to look for broker and backend(used in the tasks.py)
tasks.py defines some tasks that can be executed by calling send)task which is demonstrated in test.py

8 changes: 8 additions & 0 deletions ErfanGeramizadehNaeini/Q3/celery/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3.11-slim

WORKDIR /app
RUN pip install celery redis docker
COPY . /app


CMD ["celery", "-A", "tasks", "worker", "--loglevel=info"]
42 changes: 42 additions & 0 deletions ErfanGeramizadehNaeini/Q3/celery/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from celery import Celery
import os
import docker


app = Celery(
'tasks',
broker=os.environ.get('CELERY_BROKER_URL', 'redis://redis:6390/0'),
backend=os.environ.get('CELERY_RESULT_BACKEND', 'redis://redis:6390/0'),
)

client = docker.from_env()


@app.task(name='tasks.add') # just for test
def add(x, y):
return x + y


@app.task(name='tasks.start')
def start_container(image_name, container_name=None):
try:
container = client.containers.run(
image=image_name,
name=container_name,
detach=True
)
return f"{container.id}"
except docker.errors.APIError as e:
return f"Error starting container: {str(e)}"


@app.task(name='tasks.stop')
def stop_container(container_id):
try:
container = client.containers.get(container_id)
container.stop()
return f"Container {container_id} stopped"
except docker.errors.NotFound:
return f"Container {container_id} not found"
except docker.errors.APIError as e:
return f"Error stopping container: {str(e)}"
21 changes: 21 additions & 0 deletions ErfanGeramizadehNaeini/Q3/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3.8'

services:
redis:
image: redis:7.0
container_name: redis
expose:
- 6379
ports:
- "6391:6379"
celery:
image: celery:5.2
container_name: celery
build: ./celery
volumes:
- /var/run/docker.sock:/var/run/docker.sock # Mount Docker socket to allow Celery to communicate with Docker
depends_on:
- redis
environment:
- CELERY_BROKER_URL=redis://redis:6379/0
- CELERY_RESULT_BACKEND=redis://redis:6379/0
21 changes: 21 additions & 0 deletions ErfanGeramizadehNaeini/Q3/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from celery import Celery

app = Celery(
'client',
broker='redis://localhost:6391/0',
backend='redis://localhost:6391/0'
)
# initial test
print("Sending test task...")
result = app.send_task('tasks.add', args=[3, 4])
print("Result:", result.get(timeout=10))

print("Starting container...")
container_id = app.send_task('tasks.start', args=[
'pasapples/apjctf-todo-java-app:latest'])
id = container_id.get(timeout=1000)
print("Container ID:", id) # pulling may take time

# result = app.send_task('tasks.stop', args=[id])
# print("Sent stop task, waiting for result...")
# print(result.get(timeout=20))
10 changes: 10 additions & 0 deletions ErfanGeramizadehNaeini/Q45/.env.backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
POSTGRES_USER=postgres
SECRET_KEY=wedewdedewdwefeqqfqfewfweqfeqwfewfqwfewqfeferoopwenf
POSTGRES_PASSWORD=hello
POSTGRES_DB=defaultdb
POSTGRES_PORT=5432
DATABASE_ENGINE=postgres
CELERY_BROKER_URL=redis://redis:6379/0
CELERY_RESULT_BACKEND=redis://redis:6379/0
DATABASE_ENGINE=postgres
DEBUG=True
10 changes: 10 additions & 0 deletions ErfanGeramizadehNaeini/Q45/.env.celery
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CELERY_BROKER_URL=redis://redis:6379/0
CELERY_RESULT_BACKEND=redis://redis:6379/0
POSTGRES_DB=defaultdb
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=hello

POSTGRES_HOST=postgres


6 changes: 6 additions & 0 deletions ErfanGeramizadehNaeini/Q45/.env.database
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
POSTGRES_PASSWORD=hello

POSTGRES_DB=defaultdb


DEBUG=False
28 changes: 28 additions & 0 deletions ErfanGeramizadehNaeini/Q45/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# web app
I wrote django application with 5 apis 3 of them is for authenticating and registering.(ignore get-ip and token/refresh )
swagger is not implemented instead I explain my apis here

instead of explaining APIs I implemented a swagger.

run docker compose up --build and the visit localhost:3000/docs to see the swagger.

## Database schema:
cloud_team is an AbstractUser means that it has everything that a normal user would have.
Like username and password and email.

cloud_problem(name,number,description,image_name,port)
Name and number and description are obvious.
Image_name specifies the name of the image used for this problem. You can create two problems with the image name mentioned in the homework document.
Port specifies the port in the container that the image uses and should be bound to the outside port on the host.
cloud_teamproblem( team,problem,up,ip,port,container_id)
This table implements a many to many relationship betwean team and problem.
Team and problem are foreign keys.
Up is a boolean showing the status of the question container.
Ip is useless because I distinguish containers by their ports.
Port shows the port on host to access the container.
Container_id is what the name implies. It is used to stop that container if needed.

## setting up
Setting up the application is rather easy .just look at .env files and configure them as you desire then just write docker compose up –build and hit enter.


11 changes: 11 additions & 0 deletions ErfanGeramizadehNaeini/Q45/celery/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.11-slim

WORKDIR /app
RUN pip install celery redis docker psycopg2-binary
RUN apt-get update
RUN apt-get install -y postgresql-client


COPY . /app

#CMD ["celery", "-A", "tasks", "worker", "--loglevel=info"]
Loading