ruby-on-railsdockerwindows-subsystem-for-linuxvscode-devcontainer

Failed to save 'users_controller.rb': Unable to write file 'vscode-remote://dev-container


I am currently setting up a new rails project on my windows machine using docker. More specifically I am using Docker desktop, WSL2, and dev containers in VSCode.

Things are currently working when I use the root user, however, I want to set up a non root user. I've currently done the following:

devcontainer.json

{
  "name": "API",
  "dockerComposeFile": ["../compose.yaml"],
  "workspaceFolder": "/app",
  "service": "api",
  "remoteUser": "myUser",
  "extensions": [...],
  "forwardPorts": [3000]
}

compose.yaml

services:
  api:
    build: .
    depends_on:
      - db
    environment:
      RAILS_ENV: development
    ports: 
      - 3000:3000
    develop:
      watch:
        - path: ./app
          target: /app
          action: sync

  db:
    image: postgres
    environment:
      POSTGRES_USER: *
      POSTGRES_PASSWORD: *
      POSTGRES_DB: *
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

Dockerfile

ARG USERNAME=myUser
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN groupadd --system --gid $USER_GID $USERNAME && \
    useradd $USERNAME --uid $USER_UID --gid $USER_GID --create-home --shell /bin/bash && \
    chown -R $USERNAME:$USERNAME db log storage tmp
USER $USER_GID:$USER_UID

My containers build successfully, but when I make a change and attempt to save it, I am met with the error

Failed to save 'users_controller.rb': Unable to write file 'vscode-remote://dev-container...

I found a couple resources to work off of:

VSCode

Reddit

Github

From what I can understand dev containers tries to run this with the user "vscode", so when I create my custom non root user there is some sort of conflict with the IDs. Unfortunately I haven't been able to understand the documentation well enough to figure out what's going on.

Would anyone be able to explain to me what the problem is and how I can fix it?

Thanks!


Solution

  • This dockerfile was created for me when i ran the commands to start a new rails project, and it was intended for production only. So the dockerfile specifically only gave me access to certain files with

    chown -R $USERNAME:$USERNAME db log storage tmp
    

    Adding the directories I actually want to be developing in here is necessary, so in my case the app directory.