-
Notifications
You must be signed in to change notification settings - Fork 368
Description
Hello,
I'm writing a feature which requires writing configuration files to the home directory of the user within the container.
Looking at the source code of existing implemented features as a reference, such as the anaconda feature, I encountered code such as the following, which has specific logic to determine the user (and thus the home directory location).
I wasn't able to find specific documentation in the proposal on a "contract" for feature developers; with details on a USERNAME environment variable being provided when executing the install.sh script. Furthermore, use of "magical" users such as automatic, auto, vscode, node, codespace or other (via /etc/passwd) to aid in the determination behaviour is something I wouldn't want to have to repeat in authoring my features.
Furthermore, the reliance on having one of these user exist in a given base image is tenuous and makes this logic quite fragile (and smelly).
Therefore I propose a better and surprisingly much simpler solution, inspired in part by how the vagrant shell provisioner works, as follows:
-
The process which is executing the
install.shscript, has access to the givendevcontainer.jsonfile, which defines aremoteUserand thus can be used to provide theUSERNAMEenvironment variable to the script; sort of like a docker build argument, which would remove the need to have to infer the correct user. -
Since the feature
install.shscript is typically executed asroot, unless the base image switches the user, files installed to the home directory need to bechowned to make the files accessible to the user when the dev container is running. This can be solved by having an optionaldevcontainer-feature.jsonproperty, such asinstallAs, which is an enum type with values ofrootorremoteUser. WhenremoteUseris provided,install.shis executed in the context of the user provided by theremoteUserproperty. This is similar in concept to how theprivilegedsetting of thevagrantshell provisioner alters the context. This does place a limitation on what theinstall.shscript can do, so authors should include an assertion for root if required for proper execution. -
To aid troubleshooting, since the onus is on the
devcontainer.jsonauthor to ensure alignment of the user(s) contained in the base image and theremoteUserprovided, a check should be performed before installing features, to assert that the givenremoteUserexists and a nice helpful message if it doesn't.