From ebf4938e02d0298eec147b139b807709bda451a5 Mon Sep 17 00:00:00 2001 From: Navin Chandra <nc01009@surrey.ac.uk> Date: Sun, 5 May 2024 08:28:05 +0530 Subject: [PATCH] updated docker and docker compose file --- admins_service/main.py | 13 ++++++++++- database_mysql/Dockerfile | 15 ++++++++++++ docker-compose.yml | 47 ++++++++++++++++++++++++++++++-------- reviews_service/Dockerfile | 4 ++-- users_service/Dockerfile | 4 ++-- 5 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 database_mysql/Dockerfile diff --git a/admins_service/main.py b/admins_service/main.py index df9bf56..5bfd693 100644 --- a/admins_service/main.py +++ b/admins_service/main.py @@ -18,9 +18,20 @@ app.add_middleware( # SQLite connection # SQLite connection +# parent_directory = Path(__file__).resolve().parent.parent +# db_file_path = parent_directory / "my_ride.db" +# # print(db_file_path) + parent_directory = Path(__file__).resolve().parent.parent +# Specify the path to the SQLite database file in the data directory db_file_path = parent_directory / "my_ride.db" -# print(db_file_path) + +# Check if the database file exists +if not db_file_path.exists(): + # If the database file doesn't exist, use a local database file from the same location + local_db_file_path = Path(__file__).resolve().parent / "local_bike.db" + db_file_path = local_db_file_path + conn = sqlite3.connect(db_file_path) cursor = conn.cursor() diff --git a/database_mysql/Dockerfile b/database_mysql/Dockerfile new file mode 100644 index 0000000..137d098 --- /dev/null +++ b/database_mysql/Dockerfile @@ -0,0 +1,15 @@ +# Use the official MySQL 8.0 image as base +FROM mysql:8.0 + +# Set environment variables +ENV MYSQL_DATABASE=mydatabase \ + MYSQL_ROOT_PASSWORD=mysecretpassword + +# Optionally, if you have SQL initialization scripts, copy them to a directory in the image +# COPY ./sql-scripts/ /docker-entrypoint-initdb.d/ + +# Expose the MySQL port +EXPOSE 3306 + +# Define the entry point +CMD ["mysqld"] diff --git a/docker-compose.yml b/docker-compose.yml index 4b5c430..a443d4f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,28 +1,55 @@ version: '3' services: - bikes_service: - build: ./bikes_service + # MySQL service + database_mysql: + image: mysql:5.7 ports: - - "8001:8001" + - "3306:3306" + environment: + MYSQL_ROOT_PASSWORD: root_password + MYSQL_DATABASE: your_database + MYSQL_USER: your_username + MYSQL_PASSWORD: your_password + volumes: + - mysql_data:/var/lib/mysql + - ./sql_files:/docker-entrypoint-initdb.d # Mount SQL files for initialization + # Microservices definitions... admins_service: build: ./admins_service ports: - - "8002:8002" + - "${ADMINS_SERVICE_PORT}:8001" + environment: + - SERVICE_PORT=${ADMINS_SERVICE_PORT} + + bikes_service: + build: ./bikes_service + ports: + - "${BIKES_SERVICE_PORT}:8002" + environment: + - SERVICE_PORT=${BIKES_SERVICE_PORT} rentals_service: build: ./rentals_service ports: - - "8003:8003" - + - "${RENTALS_SERVICE_PORT}:8003" + environment: + - SERVICE_PORT=${RENTALS_SERVICE_PORT} + reviews_service: - build: ./bikes_service + build: ./reviews_service ports: - - "8001:8001" + - "${REVIEWS_SERVICE_PORT}:8004" + environment: + - SERVICE_PORT=${REVIEWS_SERVICE_PORT} users_service: - build: ./admins_service + build: ./users_service ports: - - "8002:8002" + - "${USERS_SERVICE_PORT}:8005" + environment: + - SERVICE_PORT=${USERS_SERVICE_PORT} +volumes: + mysql_data: diff --git a/reviews_service/Dockerfile b/reviews_service/Dockerfile index 67ca58e..9a90e34 100644 --- a/reviews_service/Dockerfile +++ b/reviews_service/Dockerfile @@ -14,7 +14,7 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . # Expose the port specified by the environment variable -EXPOSE $SERVICE_PORT +EXPOSE 8004 # Command to run the application -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "$SERVICE_PORT"] +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8004"] diff --git a/users_service/Dockerfile b/users_service/Dockerfile index 67ca58e..31ade53 100644 --- a/users_service/Dockerfile +++ b/users_service/Dockerfile @@ -14,7 +14,7 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . # Expose the port specified by the environment variable -EXPOSE $SERVICE_PORT +EXPOSE 8005 # Command to run the application -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "$SERVICE_PORT"] +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8005"] -- GitLab