From a61213ec37751480882041c07436181b69779fc0 Mon Sep 17 00:00:00 2001 From: Zaid AlTayan Date: Fri, 30 May 2025 19:29:00 +0300 Subject: [PATCH] Question 1 Completion --- .dockerignore | 30 ++++++++++++++++++++++++++++++ .vscode/launch.json | 24 ++++++++++++++++++++++++ Dockerfile | 35 +++++++++++++++++++++++++++++++++++ docker-compose.yml | 14 ++++++++++++++ requirements.txt | 4 ++-- 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 .vscode/launch.json create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..3aa51acb2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,30 @@ +*.pyc +__pycache__ +*.pyo +*.pyd +.Python +env/ +venv/ +.env +.venv +pip-log.txt +pip-delete-this-directory.txt +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.log +.pytest_cache/ +.git +.gitignore +.github +README.md +CHANGELOG.md +CONTRIBUTING.md +LICENSE.md +tests/ +docs/ +*.md \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..c0c105198 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Flask in Docker", + "type": "python", + "request": "attach", + "connect": { + "host": "localhost", + "port": 5678 + }, + "pathMappings": [ + { + "localRoot": "${workspaceFolder}", + "remoteRoot": "/app" + } + ], + "python": { + "pythonPath": "python", + "envFile": "${workspaceFolder}/.env" + } + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..95d352cf5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +# Use official Python runtime as a parent image +FROM python:3.11-slim + +# Set environment variables +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PORT=8000 + +# Set work directory +WORKDIR /app + +# Install system dependencies +RUN apt-get update \ + && apt-get install -y --no-install-recommends gcc \ + && rm -rf /var/lib/apt/lists/* + +# Copy requirements first to leverage Docker cache +COPY requirements.txt . + +# Install Python dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application +COPY . . + +# Create a non-root user and switch to it +RUN adduser --disabled-password --gecos '' appuser \ + && chown -R appuser:appuser /app +USER appuser + +# Expose the port the app runs on +EXPOSE 8000 + +# Command to run the application +CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:app"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..ec4092c3b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.8' + +services: + web: + build: . + ports: + - "8000:8000" + volumes: + - .:/app + environment: + - FLASK_APP=app.py + - FLASK_ENV=development + - PYTHONUNBUFFERED=1 + command: gunicorn --bind 0.0.0.0:8000 --reload app:app \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index d8b131866..d39142a1d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -Flask==3.1.0 -gunicorn \ No newline at end of file +Flask==3.0.2 +gunicorn==21.2.0 \ No newline at end of file