Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Docker building process and config #104

Merged
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/backend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

- name: Install dependencies
working-directory: Backend
run: pip install -r requirements.txt && pip install -r requirements_test.txt
run: pip install -r requirements.txt && pip install -r requirements-test.txt

- name: Run tests
working-directory: Backend/src
Expand Down
45 changes: 45 additions & 0 deletions Backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Exclude version control system directories
.git
.svn

# Exclude Python bytecode files
__pycache__
**/__pycache__/

# Exclude Python pytest cache

**/.pytest_cache/



# Exclude environment-specific settings
.env.example
.env

# Exclude any local development settings
*.log
*.sqlite

# Exclude any cached or temporary files
*.pyc
*.pyo
*.pyd


# Exclude any build-related files
*.bak
.DS_Store

# Exclude the virtual environment directory (if applicable)
**/venv/

# Exclude docs
api-docs-spotify-electron.html
generate-docs.py


# Exclude test coverage
**/htmlcov/

# Exclude tests
**/tests/
2 changes: 1 addition & 1 deletion Backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
MONGO_URI=mongo-uri-connection
MONGO_URI=mongodb://root:root@localhost:27017/
SECRET_KEY_SIGN=f24e2f3ac557d487b6d879fb2d86f2b2
LAMBDA_URL=https://lambda-url.us-east-1.on.aws/path/
13 changes: 7 additions & 6 deletions Backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

FROM python:3-slim-buster
FROM python:3.9-slim

WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./src /app/src
COPY ./src /code/src

WORKDIR /app/src
WORKDIR /code/src
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]


#docker build -t spotify_electron_backend_image .
#docker run -d --name spotify_electron_backend -e MONGO_URI=mongo-uri SECRET_KEY_SIGN=secret-key-sign LAMBDA_URL=lambda-url -p 8000:8000 spotify_electron_backend_image
7 changes: 0 additions & 7 deletions Backend/docker/build_and_up.sh

This file was deleted.

7 changes: 7 additions & 0 deletions Backend/docker/build_and_up_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Build Docker images
docker-compose -f docker-compose-dev.yml build

# Bring up the Docker containers
docker-compose -f docker-compose-dev.yml up -d
7 changes: 7 additions & 0 deletions Backend/docker/build_and_up_prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Build Docker images
docker-compose -f docker-compose-prod.yml build

# Bring up the Docker containers
docker-compose -f docker-compose-prod.yml up -d
11 changes: 11 additions & 0 deletions Backend/docker/docker-compose-base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:

backend:
container_name: backend
build:
context: ../
dockerfile: Dockerfile
ports:
- 8000:8000
env_file:
- ../.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
services:

backend:
extends:
file: docker-compose-base.yml
service: backend

mongo:
image: mongo
container_name: mongodb
Expand All @@ -20,13 +25,3 @@ services:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_URL: mongodb://root:root@mongo:27017/

backend:
container_name: backend
build:
context: ../
dockerfile: Dockerfile
ports:
- 8000:8000
env_file:
- ../.env
6 changes: 6 additions & 0 deletions Backend/docker/docker-compose-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:

backend:
extends:
file: docker-compose-base.yml
service: backend
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions Backend/src/database/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ def __init__(self):
if Database.connection is None:
try:
uri = os.getenv("MONGO_URI")
if uri and '=' not in uri:
uri+='=true&w=majority'
Database.connection = MongoClient(uri, server_api=ServerApi('1'))[
"SpotifyElectron"]
Database.list_collection = Database.connection["playlist"]
Expand Down
24 changes: 20 additions & 4 deletions docs/backend/SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ cd Backend;
python -m venv venv;
venv/Scripts/activate
pip install -r requirements.txt;
pip install -r requirements_dev.txt;
pip install -r requirements_test.txt;
pip install -r requirements-dev.txt;
pip install -r requirements-test.txt;

```
4. Run the app in hot reload debug mode
Expand Down Expand Up @@ -77,16 +77,32 @@ python -m pytest --cov=. --cov-report=html // Test run and generate coverage in

### Dev Enviroment

* Uses local mongoDB database
* Access it via MongoExpress
* Connect http://localhost:8081/
* Use user : admin and password : pass

1. Go to docker folder
```
cd docker/
```

2. Run docker compose with the script build_and_up
2. Run docker compose with the script build_and_up_dev
```
./build_and_up.sh
./build_and_up_dev.sh
```

### Prod Enviroment

1. Go to docker folder
```
cd docker/
```

2. Run docker compose with the script build_and_up_prod
```
./build_and_up_prod.sh
```

### Production Enviroment

Expand Down