为易于使用的环境配置VS Code。我想有一种简单的方法来在Docker中启动Python脚本并附加调试器。
我有什么用处?
- 创建了Dockerfile和docker-compose.yaml以正确运行 docker-compose up | start 。
- 我能够附加到正在运行的 Docker容器并调试我的代码。
我想得到什么?
一个按钮可一次启动并附加。
我需要使用docker-compose启动应用程序。我不想在VS Code中配置docker-run任务。
我的代码和想法:
Dockerfile:
void updateBalanceOfSender() async {
var docID;
Firestore.instance
.collection('balance')
.where('user',isEqualTo: loggedInUser.email)
.snapshots()
.listen((data) =>
data.documents.forEach((doc) => print(docID = doc.documentID)));
_firestore
.collection('balance')
.document(docID)
.updateData({'balance': '1'});
}
docker-compose.yaml
FROM python:3.6-alpine
RUN mkdir -p /work/
WORKDIR /work/
COPY ./python/requirements.txt ./requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY ./python/src/ .
CMD python -m ptvsd --host 0.0.0.0 --port 5678 --wait run.py < tests/input.txt > tests/output.txt
launch.json配置:
version: "3.7"
services:
py-service:
container_name: py-container
image: py-image
build:
context: ./
volumes:
- ./python/src/:/work
ports:
- 5678:5678
tasks.json以运行docker-compose命令
{ // simple attach to running container - works good
"name": "Python Attach","type": "python","request": "attach","pathMappings": [
{
"localRoot": "${workspaceFolder}/python/src/","remoteRoot": "/work/"
}
],"port": 5678,"host": "127.0.0.1"
},{ // launch docker container and attach to debugger - NOT works
"name": "Run Py in Docker","type": "docker","request": "launch","platform": "python","preLaunchTask": "docker-compose-start","python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}/python/src/","remoteRoot": "/work/"
}
],"projectType": "general","host": "127.0.0.1",},
问题在于在Docker中执行 Run Py 通常会启动我的容器,但无法附加调试器,并且超时后失败。当容器仍在运行并等待附件时。可以通过某种方式解决此问题吗?
更新
最后,我能够启动和调试了!这是我的 task.json :
{
"label": "docker-compose-start","type": "shell","command": "docker-compose up"
},
launch.json :
{
"label": "docker-compose-start","command": "docker-compose up --build -d","isBackground": true,"problemMatcher": [
{
"pattern": [{ "regexp": ".","file": 1,"location": 2,"message": 3,}],"background": {
"activeonStart": true,"beginsPattern": "^(Building py-service)$","endsPattern": "^(Creating|Recreating|Starting) (py-container) ... (done)$",}
},],
毕竟,在将所有docker up | build输出显示为问题中给我带来了不便。 VS Code每次都要求继续,但是 Enter 帮助。