visual-studio-codevscode-remotevscode-devcontainerdevcontainer

Prevent VSCode from pulling during devcontainer build


When using devcontainers in VSCode, I see that the command for building the image always includes the '--pull' directive.. But what if I want to use a fully local base image?.. What if I don't want it to be pulled and only use the locally cached one?

Is there a way to configure in devcontainer.json to tell it not to pull?


Solution

  • The way I solved it was using devcontainer.json's initializeCommand directive.

    Use it to tag the local image with a new tag and use that as base in the Dockerfile, so there is no chance devcontainers will attempt to pull. If you use a tag that can be pulled, devcontainer seems to always use the pulled/remote one:

    devcontainer.json
    "initializeCommand": "docker build -t me/my_image:local ."
    
    Dockerfile
    FROM me/my_image:local AS base
    

    Alternatively you could use env vars to set the tag in different contexts

    "initializeCommand": "TAG=local docker build -t my_image:local ."
    
    FROM me/my_image:${TAG:-latest} AS base
    

    And then, use other tags elsewhere or when you actually need the pulled image. Also, make sure to never push the local tag.