Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code DevContainer workspace ownership and permission

I want to create a devcontainer for a non root user, I want to set /workspace uid:gid to USER_UID and USER_GID, but it seems impossible.

I have created a dockerfile for a devcontainer image:

FROM debian:testing-slim

ARG USERNAME=myuser
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN apt-get update \
&& apt-get install --no-install-recommends --assume-yes \
mingw-w64 \
git

RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME

USER $USERNAME

a named volume is mounted on /workspace as "workspaceFolder" in .devcontainer.json

{
    "name": "Debian MinGW w64",
    "image": "shockagent/debian-mingw-w64:latest",
    "shutdownAction": "stopContainer",
    "workspaceMount": "source=vscode-workspace,target=/workspace,type=volume",
    "workspaceFolder": "/workspace"
}

from this point on I am stuck: I have tried to RUN mkdir /workspace && chown -R 1000:000 /workspace in dockerfile, I have also tried to set volume-opt=uid=1000,volume-opt=gid=1000 in workspaceMount, but nothing happened.

I see that "workspaceMount" Overrides the default local mount point for the workspace when the container is created, so i thought I would issue the chown command in devcontainer.json, but none of the following callbacks worked: onCreateCommand, postCreateCommand, postStartCommand.

For example, "postStartCommand": "whoami && chown -R 1000:1000 /workspace"

Running the postStartCommand from devcontainer.json...

[6046 ms] Start: Run in container: /bin/sh -c whoami && chown -R 1000:1000 /workspace
myuser
chown: changing ownership of '/workspace': Operation not permitted

The problem is that command are issued as myuser and not as root.

How can I create a devcontainer for a non root user and set ownership/permissions of the /workspace directory ?

like image 416
Shock Agent Avatar asked May 01 '26 17:05

Shock Agent


1 Answers

I also had the issue, then I set "remoteUser": "myuser", and containerUser": "myuser", in devcontainer.json which fixed it on my Linux host system.

However on my Windows host system, it still mounted the workspace as root. So I did these steps:

  1. I installed sudo (I used a UBI-based image) RUN dnf install -y sudo
  2. I added the user to wheel usermod -aG wheel $USERNAME
  3. Gave the user an empty password by adding -p "" to useradd
  4. Added "postAttachCommand": "sudo chown -R myuser /workspaces", to devcontainer.json

Instead of myuser I just called the user vscode

like image 98
tjaehnig Avatar answered May 06 '26 04:05

tjaehnig



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!