Tuesday, November 27, 2018

Python docker vscode and remote debugging

  • Docker file (Dockerfile)
    • install project requirements
    • base on python 3.6
FROM python:3.6-alpine COPY requirements.txt /tmp/requirements.txt WORKDIR /tmp RUN pip install -r requirements.txt WORKDIR /project CMD ["sh"]
  • requirements.txt
    • ptvsd: version 4.1.4 (4.2.X version has issue with run debugging multiple times)
ptvsd==4.1.4
  • Docker compose file base on image image: image name build: Docker file path volumes: shared project note that host folder point to current parent folder the remote path add project folder name automaticall
version: '2' services: dev-image: image: python_36 build: . ports: - 3000:3000 privileged: true container_name: pydev volumes: - ../:/project
  • vscode launch.json file
{ "name": "Attach (Remote Debug)", "type": "python", "request": "attach", "port": 3000, "host": "localhost", "pathMappings": [ { "localRoot": "${workspaceFolder}", "remoteRoot": "/project/" } ] }
  • python simple example
import ptvsd import time import os print("Waiting to attach") address = ('0.0.0.0', 3000) ptvsd.enable_attach(address) ptvsd.wait_for_attach() time.sleep(2) x = 1 print(x)

run docker (tasks.json)

  • start docker
    • --service-port:
    • dev-image: compose file service name
    • --name: container name
docker-compose -f docker-compose.yml run --service-ports --name devenv dev-image
  • run remote application
#docker exec -it devenv python "<relative path from working directory>/simple.py" docker exec -it devenv python "python_remote/src/simple.py"
  • stop docker
docker-compose -f docker-compose.yml down
{ "version": "2.0.0", "tasks": [ { "label": "start_docker", "type": "shell", "options": { "cwd": "${workspaceFolder}/python_remote" }, "command": "docker-compose -f docker-compose.yml run --service-ports --name devenv dev-image sh", "problemMatcher": [] }, { "label": "stop_docker", "type": "shell", "command": "docker-compose -f docker-compose.yml down", "options": { "cwd": "${workspaceFolder}/python_remote" }, "problemMatcher": [] }, { "label": "run remote", "type": "shell", "command":["docker exec devenv", "python", "${relativeFile}"], "problemMatcher": [] } ] }

No comments: