microsoft / vscode-docker

Docker Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker
Other
1.19k stars 508 forks source link

Could not find the task 'docker-run-test: debug' - Only one 'docker-run' task per task.json supported? #4138

Closed djswagerman closed 8 months ago

djswagerman commented 8 months ago

I am creating a django / python app and want to be able to start debugging in a container. I would like to have two docker files, one for a test and one for a production environment. The problem is I can only have one supported debug configuration, depending on which 'docker-run' tasks comes first in the tasks.json file. Whatever comes last in the file, works, whatever comes first, gives the error message: "Could not find the task 'docker-run-test: debug'" when starting the debug configuration.

This seems to be a bug. If this isn't a bug, I'd like to understand how I can have multiple docker based debug configurations, configured, or what I may miss in my configuration.

Configuration info:

I created a 'prod.Dockerfile':

# Use an official Python runtime as a parent image
FROM python:3.11-bookworm

# Allows docker to cache installed dependencies between builds
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Mounts the application code to the image
COPY . app
WORKDIR /app

EXPOSE 8000

# runs the production server
ENTRYPOINT ["python", "manage.py"]
CMD ["runserver", "0.0.0.0:8000"]

and a test.Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.11-bookworm

# Allows docker to cache installed dependencies between builds
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Mounts the application code to the image
COPY . app
WORKDIR /app

EXPOSE 8000

# runs the production server
ENTRYPOINT ["python", "manage.py"]
CMD ["runserver", "0.0.0.0:8000"]

tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "docker-build",
      "label": "docker-build",
      "dockerBuild": {
        "context": "${workspaceFolder}",
        "dockerfile": "${workspaceFolder}/prod.Dockerfile",
        "tag": "designguide_prod"
      },
      "problemMatcher": []
    },
    {
      "type": "docker-run",
      "label": "docker-run-test: debug",
      "dependsOn": ["docker-build-test"],
      "python": {
        "args": ["runserver", "0.0.0.0:8000", "--nothreading", "--noreload"],
        "file": "manage.py"
      }
    },
    {
      "type": "docker-run",
      "label": "docker-run: debug",
      "dependsOn": ["docker-build"],
      "python": {
        "args": ["runserver", "0.0.0.0:8000", "--nothreading", "--noreload"],
        "file": "manage.py"
      }
    },
    {
      "type": "docker-build",
      "label": "docker-build-test",
      "dockerBuild": {
        "context": "${workspaceFolder}",
        "dockerfile": "${workspaceFolder}/test.Dockerfile",
        "tag": "designguide_test"
      },
      "problemMatcher": []
    }
  ]
}

launch.json

{
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver"
            ],
            "django": true,
            "justMyCode": true
        },
        {
          "name": "Docker: Python - Django",
          "type": "docker",
          "request": "launch",
          "preLaunchTask": "docker-run: debug",
          "python": {
            "pathMappings": [
              {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/app"
              }
            ],
            "dockerRun": {
              "containerName": "designguide_prod_container",
              "image": "designguide_prod",
              "env": {
                  "DEBUG": "true"
              },
              "envFiles": [
                  "${workspaceFolder}/.env"
              ]
            }
          },
         "dockerServerReadyAction": {
            "action": "openExternally",
            "pattern": "Starting development server at (https?://\\S+|[0-9]+)",
            "uriFormat": "%s://localhost:%s/about.html"
          }
        },
        {
          "name": "Docker: Python - Django - Test",
          "type": "docker",
          "request": "launch",
          "preLaunchTask": "docker-run-test: debug",
          "python": {
            "pathMappings": [
              {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/app"
              }
            ],
            "dockerRun": {
              "containerName": "designguide_test_container",
              "image": "designguide_test",
              "env": {
                  "DEBUG": "true"
              },
              "envFiles": [
                  "${workspaceFolder}/.env"
              ]
            }          },
         "dockerServerReadyAction": {
            "action": "openExternally",
            "pattern": "Starting development server at (https?://\\S+|[0-9]+)",
            "uriFormat": "%s://localhost:%s/about.html"
          }
        }
    ]
}
bwateratmsft commented 8 months ago

This scenario should be working. There might be a bug in the VSCode task / launch system that's preventing the task from correctly being picked up.

djswagerman commented 8 months ago

Thanks for confirming. Is there any workaround I can try or am I stuck with manually switching the tasks in the tasks.json until the bug is resolved?

bwateratmsft commented 8 months ago

I just checked and I'm able to reproduce. I think this is a bug in VSCode. I'll try to find an existing one or otherwise open one.

bwateratmsft commented 8 months ago

Yeah, I think this is https://github.com/microsoft/vscode/issues/164485. The two docker-run tasks are nearly identical, differing only in dependsOn, and the task system can't distinguish between them.

@djswagerman I found a potential workaround that is very easy. You just need to add "dockerRun": {} to one of the docker-run tasks, like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build",
            "dockerBuild": {
                "context": "${workspaceFolder}",
                "dockerfile": "${workspaceFolder}/prod.Dockerfile",
                "tag": "designguide_prod"
            },
            "problemMatcher": []
        },
        {
            "type": "docker-run",
            "label": "docker-run-test: debug",
            "dependsOn": [
                "docker-build-test"
            ],
            "python": {
                "args": [
                    "runserver",
                    "0.0.0.0:8000",
                    "--nothreading",
                    "--noreload"
                ],
                "file": "manage.py"
            },
            "dockerRun": {} // Add this line to one of the `docker-run` tasks
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": [
                "docker-build"
            ],
            "python": {
                "args": [
                    "runserver",
                    "0.0.0.0:8000",
                    "--nothreading",
                    "--noreload"
                ],
                "file": "manage.py"
            }
        },
        {
            "type": "docker-build",
            "label": "docker-build-test",
            "dockerBuild": {
                "context": "${workspaceFolder}",
                "dockerfile": "${workspaceFolder}/test.Dockerfile",
                "tag": "designguide_test"
            },
            "problemMatcher": []
        }
    ]
}
djswagerman commented 8 months ago

Thanks! Your workaround worked for me! (for people landing here, if you add "dockerRun": {} to both tasks, I doesn't work. I added it to first "docker-run" task in my tasks.json)

bwateratmsft commented 8 months ago

Awesome, glad it worked! I'll close this issue since there's nothing more we can do with it but hopefully anyone running into it will find this.