diff --git a/.dockerignore b/.dockerignore index a39d2b053..ec0fb9999 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,12 +1,28 @@ -node_modules -npm-debug.log -Dockerfile* -docker-compose* -.dockerignore -.git -.gitignore -README.md +**/__pycache__ +**/.venv +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +# TODO: should I put the images in another directory?? +!**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml LICENSE -.vscode -__pycache__ -env +README.md diff --git a/.vscode/launch.json b/.vscode/launch.json index 9a2737d78..338f327e2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,6 +20,36 @@ ], "jinja": true, "justMyCode": true + }, + { + "name": "Docker: Python Flask", + "type": "docker", + "request": "launch", + "preLaunchTask": "docker-run: debug flask", + "python": { + "pathMappings": [ + { + "localRoot": "${workspaceFolder}", + "remoteRoot": "/app" + } + ], + "projectType": "flask" + } + }, + { + "name": "Docker: Python Long Script", + "type": "docker", + "request": "launch", + "preLaunchTask": "docker-run: debug long-script", + "python": { + "pathMappings": [ + { + "localRoot": "${workspaceFolder}", + "remoteRoot": "/app" + } + ], + "projectType": "flask" + } } ] } diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 000000000..494ee5a01 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,61 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "docker-build", + "label": "docker-build: flask", + "platform": "python", + "dockerBuild": { + "tag": "pythonsamplevscodeflasktutorial:latest", + "dockerfile": "${workspaceFolder}/compose/flask_apps/Dockerfile", + "context": "${workspaceFolder}", + "pull": true + } + }, + { + "type": "docker-run", + "label": "docker-run: debug flask", + "dependsOn": [ + "docker-build: flask" + ], + "dockerRun": { + "env": { + "FLASK_APP": "hello_app/webapp.py" + } + }, + "python": { + "args": [ + "run", + "--no-debugger", + "--no-reload", + "--host", + "0.0.0.0", + "--port", + "5002" + ], + "module": "flask" + } + }, + { + "type": "docker-build", + "label": "docker-build: long-script", + "platform": "python", + "dockerBuild": { + "tag": "pythonsamplevscodeflasktutorial:latest", + "dockerfile": "${workspaceFolder}/compose/long_script/Dockerfile", + "context": "${workspaceFolder}", + "pull": true + } + }, + { + "type": "docker-run", + "label": "docker-run: debug long-script", + "dependsOn": [ + "docker-build: long-script" + ], + "python": { + "file": "long_script.py" + } + } + ] +} diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 4c775f6b9..000000000 --- a/Dockerfile +++ /dev/null @@ -1,34 +0,0 @@ -# Pull a pre-built alpine docker image with nginx and python3 installed -FROM tiangolo/uwsgi-nginx:python3.8-alpine-2020-12-19 - -# Set the port on which the app runs; make both values the same. -# -# IMPORTANT: When deploying to Azure App Service, go to the App Service on the Azure -# portal, navigate to the Applications Settings blade, and create a setting named -# WEBSITES_PORT with a value that matches the port here (the Azure default is 80). -# You can also create a setting through the App Service Extension in VS Code. -ENV LISTEN_PORT=5000 -EXPOSE 5000 - -# Indicate where uwsgi.ini lives -ENV UWSGI_INI uwsgi.ini - -# Tell nginx where static files live. Typically, developers place static files for -# multiple apps in a shared folder, but for the purposes here we can use the one -# app's folder. Note that when multiple apps share a folder, you should create subfolders -# with the same name as the app underneath "static" so there aren't any collisions -# when all those static files are collected together. -ENV STATIC_URL /hello_app/static - -# Set the folder where uwsgi looks for the app -WORKDIR /hello_app - -# Copy the app contents to the image -COPY . /hello_app - -# If you have additional requirements beyond Flask (which is included in the -# base image), generate a requirements.txt file with pip freeze and uncomment -# the next three lines. -#COPY requirements.txt / -#RUN pip install --no-cache-dir -U pip -#RUN pip install --no-cache-dir -r /requirements.txt diff --git a/compose.debug.yml b/compose.debug.yml new file mode 100644 index 000000000..903f36bd8 --- /dev/null +++ b/compose.debug.yml @@ -0,0 +1,21 @@ +version: '3.4' + +services: + pythonsamplevscodeflasktutorial: + image: pythonsamplevscodeflasktutorial + build: + context: . + dockerfile: ./compose/flask_apps/Dockerfile + command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m flask run --no-debugger --no-reload --host 0.0.0.0 --port 5002"] + ports: + - 5002:5002 + - 5678:5678 + environment: + - FLASK_APP=hello_app/webapp.py + + long-script: + image: long-script + build: + context: . + dockerfile: ./compose/long_script/Dockerfile + command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 long_script.py"] diff --git a/compose.yml b/compose.yml new file mode 100644 index 000000000..0d6cd363b --- /dev/null +++ b/compose.yml @@ -0,0 +1,16 @@ +version: '3.4' + +services: + pythonsamplevscodeflasktutorial: + image: pythonsamplevscodeflasktutorial + build: + context: . + dockerfile: ./compose/flask_apps/Dockerfile + ports: + - 5002:5002 + + long-script: + image: long-script + build: + context: . + dockerfile: ./compose/long_script/Dockerfile diff --git a/compose/flask_apps/Dockerfile b/compose/flask_apps/Dockerfile new file mode 100644 index 000000000..663144db6 --- /dev/null +++ b/compose/flask_apps/Dockerfile @@ -0,0 +1,25 @@ +# For more information, please refer to https://aka.ms/vscode-docker-python +FROM python:3-slim + +EXPOSE 5002 + +# Keeps Python from generating .pyc files in the container +ENV PYTHONDONTWRITEBYTECODE=1 + +# Turns off buffering for easier container logging +ENV PYTHONUNBUFFERED=1 + +# Install pip requirements +COPY requirements.txt . +RUN python -m pip install -r requirements.txt + +WORKDIR /app +COPY . /app + +# Creates a non-root user with an explicit UID and adds permission to access the /app folder +# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers +RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app +USER appuser + +# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug +CMD ["gunicorn", "--bind", "0.0.0.0:5002", "hello_app.webapp:app"] diff --git a/compose/long_script/Dockerfile b/compose/long_script/Dockerfile new file mode 100644 index 000000000..a29385815 --- /dev/null +++ b/compose/long_script/Dockerfile @@ -0,0 +1,25 @@ +# For more information, please refer to https://aka.ms/vscode-docker-python +FROM python:3-slim + +EXPOSE 5002 + +# Keeps Python from generating .pyc files in the container +ENV PYTHONDONTWRITEBYTECODE=1 + +# Turns off buffering for easier container logging +ENV PYTHONUNBUFFERED=1 + +# Install pip requirements +COPY requirements.txt . +RUN python -m pip install -r requirements.txt + +WORKDIR /app +COPY . /app + +# Creates a non-root user with an explicit UID and adds permission to access the /app folder +# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers +RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app +USER appuser + +# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug +CMD ["python", "long_script.py"] diff --git a/long_script.py b/long_script.py new file mode 100644 index 000000000..3b7b66da4 --- /dev/null +++ b/long_script.py @@ -0,0 +1,11 @@ +import time + + +def main(): + print("I'm here") + time.sleep(15) + print("I'm done") + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index e3e9a71d9..cef5a165b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ Flask +gunicorn