Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mount path based on env var #1132

Open
devhl-labs opened this issue Sep 27, 2024 · 4 comments
Open

Mount path based on env var #1132

devhl-labs opened this issue Sep 27, 2024 · 4 comments

Comments

@devhl-labs
Copy link

I am trying to mount ~/.azure into my devcontainer. The issue is that this directory is a symlink to my Windows path. I can get the mount to work if I mount both paths, the problem is that I don't want to commit my Windows path into git because it wont be the same for others. For others, it may not even be a symlink. Is it possible to mount my symlink using an environment variable if the user has it set?

@samruddhikhandale
Copy link
Member

Yes, you can use an environment variable in your devcontainer.json to mount the symlink. Define the variable in an .env file and reference it in the mounts section.

eg.

{
    "name": "My Dev Container",
    "image": "mcr.microsoft.com/vscode/devcontainers/base:0.202.6",
    "mounts": [
        "source=${AZURE_PATH},target=/root/.azure,type=bind,consistency=cached"
    ],
    "remoteEnv": {
        "AZURE_PATH": "${localEnv:AZURE_PATH}"
    }
}

@devhl-labs
Copy link
Author

This gives me an error Error response from daemon: ${AZURE_PATH} is not an absolute path.

    "mounts": [
        "type=bind,source=${localEnv:HOME}/.bash_history,target=${localEnv:HOME}/.bash_history",
        "type=bind,source=${localEnv:HOME}/.ssh,target=${localEnv:HOME}/.ssh,readonly",
        "type=bind,source=${localEnv:HOME}/.azure,target=${localEnv:HOME}/.azure",
        "source=${AZURE_PATH},target=/root/.azure,type=bind,consistency=cached"
    ],
    "runArgs": [
        "--env-file",
        ".devcontainer/devcontainer.env"
    ],
    "remoteEnv": {
        "AZURE_PATH": "${localEnv:AZURE_PATH}"
    }

@devhl-labs
Copy link
Author

devhl-labs commented Nov 26, 2024

Mounts and runArgs do not resolve the environment variables, so it seems there is no way to have a dynamic mount. There is a similar issue here microsoft/vscode-remote-release#3972 @samruddhikhandale Is there anything else I can try?

I even get the same error when I simplify it like this

    "mounts": [
        "type=bind,source=${AZURE_PATH},target=/mnt/c/Users/devhl/.azure"
    ],
    "remoteEnv": {
        "AZURE_PATH": "/mnt/c/Users/devhl/.azure"
    }

Error response from daemon: ${AZURE_PATH} is not an absolute path.

@devhl-labs
Copy link
Author

The only way I've been able to get dynamic mount paths is by having my .bashrc run the script

bash.rc

set -a
source /your/path/.env
set +a

.devcontainer/devcontainer.json

    "mounts": [
        "type=bind,source=${localEnv:FOO},target=${localEnv:FOO}"
    ],
    "runArgs": [
        "--env-file",
        ".env"
    ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants