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: helloflask
build:
context: .
dockerfile: ./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 5000"]
ports:
- 5000:5000
- 5678:5678
environment:
- FLASK_APP=app.py
- FLASK_ENV=development
docker-compose.yml
version: '3.4'
services:
helloflask:
image: helloflask
build:
context: .
dockerfile: ./Dockerfile
ports:
- 5000:5000
networks:
- net-proxy
networks:
net-proxy:
# external: true,告知dockerd我们需要使用外部网络(卷、秘钥等也有类似操作),无需自动帮我创建net-proxy
external: 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-python
FROM python:3.8-slim-buster
EXPOSE 5000
# 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
ADD requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
ADD . /app
# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd 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:5000", "app:app"]