dockercodespaces

How do I mount a Codespace's workspace in a Docker container?


When I try to mount the workspace directory in a Codespace in a Docker container (with -v), the mounted path is empty, e.g:

$ ls /workspaces/myrepo
[list of files]

$ docker run -it --rm -v /workspaces/myrepo:/app alpine:latest

/ # ls /app
[nothing]

Solution

  • I believe this is because the Codespace is itself running in Docker, and so the mount points are relative to the Docker host (of the Codespace), not to the container in which the Codespace is running.


    One solution is to use --volumes-from to remount the volumes for the Codespace (e.g. /workspaces/myrepo) inside the container you are starting. This requires knowing the container ID of the container in which the Codespace is running, which we can obtain with docker ps --filter "label=Type=codespaces".

    As a bonus --workdir can also be used to start the container in the current directory.

    Putting it together:

    $ docker run -it --rm --volumes-from $(docker ps --filter "label=Type=codespaces" -q) --workdir $(pwd) alpine:latest
    
    /workspaces/myrepo/ # ls .
    [list of files]