Joe Jankowiak
Joe Jankowiak

Reputation: 1159

VS Code dev container mounted directory is empty

I have a devcontainer compose project that requires mongo and a replica server. This requires a few mongosh commands to be run, which I'd like to do in a separate container as a bash script.

My issue is that when using "Clone repository into Container volume" my mounted directory is empty. This works fine when I first check the repo out locally and then build the container from that.

Here is a demo repository that shows the issue:
https://github.com/jrj2211/vscode-remote-try-node-mongo-compose

In this project, the compose file mounts the .devcontainer directory. The file I need is at the path: .devcontainer/scripts/mongosetup.sh.

volumes: 
      - ./scripts:/scripts

This produces the correct result locally but the folder is empty when in a docker volume.

What is the correct path to the folder location in the WSL2 volume? Is there a way to make this work both locally and cloned in a docker volume?

I tried to set an ENV variable from the devcontainer.json that pointed to ${workspaceFolder} but that ended up as an empty string in compose.

This documentation makes me believe this should work this way which is linked to from the 2nd link that talks about "Clone Repository in Container Volume":
https://code.visualstudio.com/remote/advancedcontainers/add-local-file-mount
https://code.visualstudio.com/remote/advancedcontainers/improve-performance

Upvotes: 1

Views: 2318

Answers (1)

Joe Jankowiak
Joe Jankowiak

Reputation: 1159

I was able to get this working through the use of @h4l brilliant code. This takes the containerWorkspaceFolder and localWorkspaceFolder and turns them into environment variables available in docker-compose. This has the added benefit of continuing to work both locally or in a container.

https://github.com/h4l/dev-container-docker-compose-volume-or-bind

Hopefully soon those variables become available in container mode directly so additional scripts arn't needed.

Upvotes: 2

Related Questions