Python in a container with Gunicorn
https://code.visualstudio.com/docs/containers/quickstart-python
Use Docker Compose with docker-compose.debug.yml
https://code.visualstudio.com/docs/containers/docker-compose#_debug
tasks.json
{"version": "2.0.0","tasks": [{"type": "docker-build","label": "docker-build","platform": "python","dockerBuild": {"tag": "helloflask:latest","dockerfile": "${workspaceFolder}/Dockerfile","context": "${workspaceFolder}","pull": true}},{"type": "docker-run","label": "docker-run: debug","dependsOn": ["docker-build"],"dockerRun": {"env": {"FLASK_APP": "app.py","FLASK_ENV": "development"},"volumes": [{"containerPath": "/app", "localPath": "${workspaceFolder}"}]},"python": {"args": ["run","--host", "0.0.0.0","--port", "5000"],"module": "flask"}}]}
launch.json
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "Python: Remote Attach","type": "python","request": "attach","port": 5678,"host": "localhost","pathMappings": [{"localRoot": "${workspaceFolder}","remoteRoot": "/app"}]},{"name": "Python: Flask","type": "python","request": "launch","module": "flask","env": {"FLASK_APP": "app.py","FLASK_ENV": "development","FLASK_DEBUG": "0"},"args": ["run","--no-debugger","--no-reload"],"jinja": true},{"name": "Docker: Python - Flask","type": "docker","request": "launch","preLaunchTask": "docker-run: debug","python": {"pathMappings": [{"localRoot": "${workspaceFolder}","remoteRoot": "/app"}],"projectType": "flask"}}]}
docker-compose.debug.yml
version: '3.4'services:helloflask:image: helloflaskbuild:context: .dockerfile: ./Dockerfilecommand: ["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 5000"]ports:- 5000:5000- 5678:5678environment:- FLASK_APP=app.py- FLASK_ENV=development
docker-compose.yml
version: '3.4'services:helloflask:image: helloflaskbuild:context: .dockerfile: ./Dockerfileports:- 5000:5000networks:- net-proxynetworks:net-proxy:# external: true,告知dockerd我们需要使用外部网络(卷、秘钥等也有类似操作),无需自动帮我创建net-proxyexternal: true# 告知dockerd我们使用my-proxy-net的网络,因此web服务可以与my-proxy-net容器互联name: my-proxy-net
Dockerfile
# For more information, please refer to https://aka.ms/vscode-docker-pythonFROM python:3.8-slim-busterEXPOSE 5000# Keeps Python from generating .pyc files in the containerENV PYTHONDONTWRITEBYTECODE=1# Turns off buffering for easier container loggingENV PYTHONUNBUFFERED=1# Install pip requirementsADD requirements.txt .RUN python -m pip install -r requirements.txtWORKDIR /appADD . /app# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rightsRUN useradd appuser && chown -R appuser /appUSER appuser# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debugCMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]
