4

I'm using the vscode command Remote-contains: Open Folder in container...

I'm trying to mount bind a file into the docker container.

~/.config/dart/pub-tokens.json

The host file is under my HOME directory and I need it mounted in the same location within the container's HOME directory.

Here is my mount command from the vscode devcontainer.json

"mounts": [
        "source=${localEnv:HOME}/.config/dart/pub-tokens.json,target=${containerEnv:HOME}/.config/dart/pub-tokens.json,type=bind,consistency=cached",
        
    ]

Note the 'containerEnv' in the target clause.

Launching the container via the vscode Remote-contains: Open Folder in container... produces the following error: (for readability I've added some newlines)

Start: Run: docker run --sig-proxy=false -a STDOUT -a STDERR 
--mount type=bind,source=/home/bsutton/git/onepub/onepub,target=/workspaces/onepub 
--mount source=/home/bsutton/.config/dart/pub-tokens.json,target=${containerEnv:HOME}/.config/dart/pub-tokens.json,type=bind,consistency=cached 
--mount source=/home/bsutton/.onepub/onepub.yaml,target=${containerEnv:HOME}/.onepub/onepub.yaml,type=bind,consistency=cached 
--mount type=volume,src=vscode,dst=/vscode -l devcontainer.local_folder=/home/bsutton/git/onepub/onepub 
--entrypoint /bin/sh vsc-onepub-7ff341664d5755895634c2f74983ff45-uid -c echo Container started

docker: Error response from daemon: 
invalid mount config for type "bind": invalid mount path: '${containerEnv:HOME}/.config/dart/pub-tokens.json' mount path must be absolute.

It would appear that vscode isn't expanding the the containerEnv.

If I replace containerEnv it with localEnv it does get expanded (but the wrong path).

i.e. the following works:

"mounts": [
        "source=${localEnv:HOME}/.config/dart/pub-tokens.json,target=${localEnv:HOME}/.config/dart/pub-tokens.json,type=bind,consistency=cached",
        
    ]

Here is the complete devcontainer.json

// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/ubuntu
{
    "name": "Ubuntu",
    "build": {
        "dockerfile": "Dockerfile",
        // Update 'VARIANT' to pick an Ubuntu version: jammy / ubuntu-22.04, focal / ubuntu-20.04, bionic /ubuntu-18.04
        // Use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon.
        "args": { "VARIANT": "ubuntu-22.04" }
    },

    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    // "forwardPorts": [],

    // Use 'postCreateCommand' to run commands after the container is created.
    // "postCreateCommand": "uname -a",

    // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
    "remoteUser": "vscode",
    "features": {
        "git": "latest",
        "github-cli": "latest"
    },
    "mounts": [
        "source=${localEnv:HOME}/.config/dart/pub-tokens.json,target=${containerEnv:HOME}/.config/dart/pub-tokens.json,type=bind,consistency=cached",
        "source=${localEnv:HOME}/.onepub/onepub.yaml,target=${containerEnv:HOME}/.onepub/onepub.yaml,type=bind,consistency=cached"
    ]
    
}

1 Answer 1

2

I also encountered this issue, but upon reflection, expanding a container environment variable before it is created is unsatisfiable, as per this comment:

The mount points are applied when the container is created, but container variables (containerEnv) are only replaced after the container is created. This is because only after the container was created can we inspect the container for its environment variables.

I was hoping things could at least fall back to the ENVs statically defined in the Dockerfile, but that doesn't seem to be the case here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.