Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode devcontainer.json mounts not working

Assuming I must be doing something wrong here, but I cannot seem to get VSCode to mount anything into the container with my very simple devcontainer.json file.

It currently looks as follows:

{
    "name": "Terraform",
    "dockerFile": "Dockerfile",
    "mounts": ["source=/home/paul,target=/host,type=bind,consistency=cached"]
}

I have also tried the example given in the docs:

["source=${localEnv:HOME}${localEnv:USERPROFILE},target=/host-home-folder,type=bind,consistency=cached"]

Neither seem to mount anything inside the container, and looking at the output of the "Dev Containers" console output, it doesn't even look like VSCode attempts to mount it. Do I need to switch something on for this to work?

Running VSCode 1.36.1 on Linux Mint. Docker CE 19.03.

like image 894
mehstg Avatar asked Jun 17 '26 14:06

mehstg


2 Answers

For me it worked the workspaceMount instead of mount

"workspaceMount": "source=${localWorkspaceFolder}/sub-folder,target=/workspace,type=bind,consistency=delegated",
"workspaceFolder": "/workspace"

as stated in https://code.visualstudio.com/docs/remote/containers-advanced

like image 180
capoloja Avatar answered Jun 20 '26 09:06

capoloja


Docker-in-Docker

Using docker-compose

When working with a Docker-in-Docker setup which utilizes the docker-compose functionality, the VSCode internal variable ${localWorkspaceFolder} can be used to get the actual path to the workspace on disk. This is needed because in a Docker-in-Docker environment, "normal" paths may not work as the first Docker instance is already running in a mounted environment.

In order to make those internal VSCode variables also accessible in the docker-compose.yaml, you first need to define an environment variable in your .devcontainer/devcontainer.json:

{
  "name": "DevContainer",
  "dockerFile": "Dockerfile", // this is a reference to the .devcontainer/Dockerfile, which is used by VSCode to build the DevContainer. This has nothing to do with the Dockerfile or docker-compose.yaml file your project is using.

  "remoteEnv": {
    // the original host directory which is needed for volume 
    // mount commands from inside the container (Docker in Docker)
    "HOST_PROJECT_PATH": "${localWorkspaceFolder}"
  }
}

Then, use the environment variable in your project's actual ./docker-compose.yaml:

services:
  webserver:
    image: nginx:mainline-alpine
    container_name: webserver
    ports:
      - 8080:80
    volumes:
       -  ${HOST_PROJECT_PATH}/webserver:/etc/nginx/conf.d
       -  ${HOST_PROJECT_PATH}/build:/var/www/html

VSCode provides even more internal variables which can be used for advanced setup and configuration of the DevContainer environment. Please have a look here.

Using Dockerfile

More information on how to use Docker-in-Docker with Dockerfiles can be found here.

like image 40
winklerrr Avatar answered Jun 20 '26 11:06

winklerrr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!