Skip to content

Commit 00a1b8a

Browse files
authored
Merge pull request #8 from sonyasha/Dockerize-application
Dockerize application
2 parents 9998b37 + ba57b19 commit 00a1b8a

9 files changed

Lines changed: 110 additions & 6 deletions

File tree

.dockerignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
__pycache__/
2+
.git/
3+
.serverless/
4+
.gitignore
5+
serverless.yml
6+
7+
*.env
8+
9+
.DS_Store
10+
.vscode/

.github/workflows/deploy.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ on:
1414
- prod
1515

1616
env:
17-
AWS_REGION: "us-east-1"
1817
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
1918
ENV: ${{ inputs.environment }}
19+
FLASK_DEBUG: 0
2020

2121
jobs:
2222
deploy:
2323
runs-on: ubuntu-latest
24+
if: github.actor == github.repository_owner
2425

2526
steps:
2627
- uses: actions/checkout@v3
@@ -47,14 +48,15 @@ jobs:
4748
- name: Install Serverless plugins
4849
run: |
4950
serverless plugin install -n serverless-wsgi
51+
serverless plugin install -n serverless-ssm-fetch
5052
serverless plugin install -n serverless-python-requirements
5153
5254
- name: Configure AWS credentials
5355
uses: aws-actions/configure-aws-credentials@v4
5456
with:
5557
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
5658
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
57-
aws-region: ${{ env.AWS_REGION }}
59+
aws-region: ${{ secrets.AWS_REGION }}
5860

5961
- name: Deploy to environment
6062
run: |

.github/workflows/docker-test.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This workflow performs linting, builds Docker container, and runs test suite for all branches
2+
# And bandit security checks for master branch
3+
4+
name: Docker container test
5+
6+
on:
7+
push:
8+
9+
10+
env:
11+
API_KEY: ${{ secrets.API_KEY }}
12+
PORT: ${{ secrets.PORT }}
13+
14+
jobs:
15+
lint:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- uses: actions/checkout@v3
20+
- name: Set up Python 3.x
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: '3.13'
24+
- name: Install dependencies
25+
run: |
26+
python -m pip install --upgrade pip
27+
pip install -r requirements-lint.txt
28+
- name: Run ruff check
29+
run: ruff check .
30+
- name: Run black check
31+
run: black --check .
32+
- name: Run isort check
33+
run: isort --check .
34+
35+
test:
36+
runs-on: ubuntu-latest
37+
needs: lint
38+
39+
steps:
40+
- uses: actions/checkout@v3
41+
42+
- name: Build Docker image
43+
run: docker compose build
44+
- name: Run tests inside the container
45+
run: docker compose run --rm app pytest --cov=api tests/
46+
47+
bandit-scan:
48+
if: github.ref == 'refs/heads/main'
49+
runs-on: ubuntu-latest
50+
needs: test
51+
steps:
52+
- uses: actions/checkout@v3
53+
# Runs a pre configured Bandit scan
54+
- name: Run bandit
55+
uses: jpetrucciani/bandit-check@master
56+
with:
57+
# only scans under this path
58+
path: './api'

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.13-alpine
2+
3+
WORKDIR /app
4+
5+
COPY requirements.txt /app/requirements.txt
6+
RUN pip install --no-cache-dir -r requirements.txt
7+
8+
COPY . /app
9+
10+
EXPOSE 5000
11+
12+
CMD ["flask", "--app", "api/app.py", "run", "-h", "0.0.0.0", "-p", "5000"]

api/app.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
from flask import Flask, jsonify, request, send_from_directory
88

9+
from api.utils import DEV_PATHS, QUOTES, require_api_key
910
from api.views import bp as views_bp
1011

11-
from .utils import DEV_PATHS, QUOTES, require_api_key
12-
1312
app = Flask(__name__, template_folder="templates", static_folder="static")
1413
app.secret_key = os.environ.get("SECRET_KEY", "dev-secret-key")
1514
app.register_blueprint(views_bp)
@@ -188,3 +187,8 @@ def update_milestone(roadmap_id, milestone_index):
188187
@app.route("/favicon.ico")
189188
def favicon():
190189
return send_from_directory(app.static_folder, "favicon.ico", mimetype="image/vnd.microsoft.icon")
190+
191+
192+
if __name__ == "__main__":
193+
debug_mode = os.environ.get("FLASK_DEBUG", "0") == "1"
194+
app.run(debug=debug_mode, host="127.0.0.1", port=int(os.environ.get("PORT", 5000)))

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
app:
3+
platform: linux/amd64
4+
build: .
5+
volumes:
6+
- ./api:/app/api
7+
ports:
8+
- "${PORT}:${PORT}"
9+
environment:
10+
- API_KEY=${API_KEY}
11+
- FLASK_DEBUG=1

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"devDependencies": {
33
"serverless-python-requirements": "^6.1.2",
4+
"serverless-ssm-fetch": "^2.0.0",
45
"serverless-wsgi": "^3.0.5"
56
}
67
}

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ platformdirs==4.3.6
2121
pydantic==2.10.6
2222
pydantic_core==2.27.2
2323
pytest==8.3.5
24+
pytest-cov==6.0.0
2425
requests==2.32.3
2526
sniffio==1.3.1
2627
tqdm==4.67.1

serverless.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
service: flask-serverless-ai
1+
service: flask-serverless-lambda
22

33
provider:
44
name: aws
55
runtime: python3.13
6-
region: us-east-1
6+
region: us-west-1
77
memorySize: 128
88
plugins:
99
- serverless-python-requirements
1010
- serverless-wsgi
11+
- serverless-ssm-fetch
1112
package:
1213
exclude:
1314
- node_modules/**
@@ -23,9 +24,13 @@ custom:
2324
app: api.app.app
2425
pythonRequirements:
2526
dockerizePip: true
27+
serverlessSsmFetch:
28+
API_KEY: /flask-serverless-lambda/api-key
2629
functions:
2730
app:
2831
handler: wsgi_handler.handler
32+
environment:
33+
FLASK_DEBUG: 0
2934
events:
3035
- http: ANY /
3136
- http: 'ANY {proxy+}'

0 commit comments

Comments
 (0)