Search code examples
dockervisual-studio-codedockerfilevscode-remote

How do I specify the dockerfile stage in Visual Studio Code Remote?


I have a multi-stage Dockerfile. I want to configure VS Code Remote to build the dev stage of the Dockerfile rather than the last stage in the Dockerfile.

I'm pretty sure this configuration would go in the .devcontainer.json file. I've read through the .devcontainer.json reference: https://code.visualstudio.com/docs/remote/containers#_devcontainerjson-reference and tried runArgs, but these are the run-time args, not the build args.

I tried this:

{
        "dockerFile": "Dockerfile",
        "extensions": ["ms-python.python"],
        "runArgs": [
                "--target",
                "dev"
        ]
}

and:

{
        "dockerFile": "Dockerfile",
        "extensions": ["ms-python.python"],
        "buildArgs": [
                "--target",
                "dev"
        ]
}

When I used runArgs, I got Failed: Starting the development container because target is not a docker run option. When I tried buildArgs, the argument was apparently ignored, which makes sense since it isn't listed in the documentation.


Solution

  • runArgs specifies the arguments to use with the "docker run" command. In this case, we need to pass arguments to the "docker build" command.

    For specifying build arguments, you need to use the "build" property. In your example, you would need your devcontainer.json file to contain:

    "build": { 
        "target": "dev" 
    },